关注微信公众号:程序猿的日常分享,定期更新分享。
1、在和axios联调鉴权的时候发现,在增加自定义header的时候会出现跨域问题
2、因为是跨域请求,前端会首先发送一个options的嗅探请求,一次真正的请求
所以会出现两个403
3、网关之前设置的是全局filter
package net.youqu.micro.service.filter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* description: 跨域访问
*
* @author wangpeng
* @date 2018/09/25
*/
@Component
public class CrossOriginFilter implements GlobalFilter, Ordered {
private static final String ALL = "*";
private static final String MAX_AGE = "18000L";
@Override
public Mono<Void> filter(ServerWebExchange serverWebExchange, GatewayFilterChain gatewayFilterChain) {
ServerHttpRequest request = serverWebExchange.getRequest();
// if (!CorsUtils.isCorsRequest(request)) {
// return gatewayFilterChain.filter(serverWebExchange);
// }
ServerHttpResponse response = serverWebExchange.getResponse();
HttpHeaders headers = response.getHeaders();
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "POST, GET, PUT, OPTIONS, DELETE, PATCH");
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "*");
headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, ALL);
headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
if (request.getMethod() == HttpMethod.OPTIONS) {
response.setStatusCode(HttpStatus.OK);
return Mono.empty();
}
return gatewayFilterChain.filter(serverWebExchange);
}
@Override
public int getOrder() {
return -300;
}
}
然而并没有什么卵用,这个是百度出来的配置,看起来没问题,但在前端请求的时候根本就没有进入到这里,就直接拒绝了。
之后又想了下是否是容器层给拒绝了,由于gateway确实用的人比较少,百度了半天都是互相抄的文章,半天有用的没有,只能采用老的办法了,去github上gateway的项目里问作者了。
结果发现根源在Webflux上边,由于gateway使用的是webflux,而不是springmvc,需要覆盖掉webflux默认的CORS处理配置,通过注册新的CorsWebFilter Bean来解决跨域问题
package net.youqu.micro.service.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;
/**
* description:
*
* @author wangpeng
* @date 2019/01/02
*/
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}
配置式:
spring:
cloud:
gateway:
discovery:
# 跨域
globalcors:
corsConfigurations:
'[/**]':
allowedHeaders: "*"
allowedOrigins: "*"
allowedMethods:
- GET
POST
DELETE
PUT
OPTION
关注微信公众号:程序猿的日常分享,定期更新分享。