码迷,mamicode.com
首页 > 编程语言 > 详细

springcloud gateway解决跨域问题

时间:2020-01-16 12:34:40      阅读:611      评论:0      收藏:0      [点我收藏+]

标签:跨域   gate   request   set   跨域问题   ref   hang   turn   mono   

/**
 * 跨域允许
 */
@Configuration
public class CorsConfig {
 
    @Bean
    public WebFilter corsFilter() {
        return (ServerWebExchange ctx, WebFilterChain chain) -> {
            ServerHttpRequest request = ctx.getRequest();
            if (CorsUtils.isCorsRequest(request)) {
                ServerHttpResponse response = ctx.getResponse();
                HttpHeaders headers = response.getHeaders();
                headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
                headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "*");
                headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "");
                headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
                headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
                headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "3600");
                if (request.getMethod() == HttpMethod.OPTIONS) {
                    response.setStatusCode(HttpStatus.OK);
                    return Mono.empty();
                }
            }
            return chain.filter(ctx);
        };
    }
}

参考博客:https://blog.csdn.net/a294634473/article/details/90715903


springcloud gateway解决跨域问题

标签:跨域   gate   request   set   跨域问题   ref   hang   turn   mono   

原文地址:https://www.cnblogs.com/TheoryDance/p/12200421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!