關注微信公眾號:程序猿的日常分享机错,定期更新分享卒煞。
1、在和axios聯(lián)調(diào)鑒權的時候發(fā)現(xiàn)行疏,在增加自定義header的時候會出現(xiàn)跨域問題
2匆光、因為是跨域請求,前端會首先發(fā)送一個options的嗅探請求酿联,一次真正的請求
所以會出現(xiàn)兩個403
3终息、網(wǎng)關之前設置的是全局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的項目里問作者了忙厌。
結(jié)果發(fā)現(xiàn)根源在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
關注微信公眾號:程序猿的日常分享,定期更新分享爹土。