package cn.huge.gateway.config;
|
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.http.codec.ServerCodecConfigurer;
|
import org.springframework.http.codec.support.DefaultServerCodecConfigurer;
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
import org.springframework.web.cors.reactive.CorsUtils;
|
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.WebFilter;
|
import org.springframework.web.server.WebFilterChain;
|
import reactor.core.publisher.Mono;
|
|
/**
|
* @title: 网关cors配置
|
* @description: 网关cors配置
|
* @company: hugeinfo
|
* @author: liyj
|
* @time: 2021-11-05 16:51:48
|
* @version: 1.0.0
|
*/
|
@Configuration
|
public class CorsConfig {
|
|
private static final String MAX_AGE = "18000L";
|
|
@Bean
|
public WebFilter corsFilter() {
|
return (ServerWebExchange ctx, WebFilterChain chain) -> {
|
ServerHttpRequest request = ctx.getRequest();
|
if (CorsUtils.isCorsRequest(request)) {
|
HttpHeaders requestHeaders = request.getHeaders();
|
ServerHttpResponse response = ctx.getResponse();
|
HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
|
HttpHeaders headers = response.getHeaders();
|
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
|
headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders
|
.getAccessControlRequestHeaders());
|
if(requestMethod != null){
|
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
|
}
|
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
|
headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
|
headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
|
if (request.getMethod() == HttpMethod.OPTIONS) {
|
response.setStatusCode(HttpStatus.OK);
|
return Mono.empty();
|
}
|
|
}
|
return chain.filter(ctx);
|
};
|
}
|
|
@Bean
|
public ServerCodecConfigurer serverCodecConfigurer() {
|
return new DefaultServerCodecConfigurer();
|
}
|
}
|
/**
|
* -------------------_ooOoo_-------------------
|
* ------------------o8888888o------------------
|
* ------------------88" . "88------------------
|
* ------------------(| -_- |)------------------
|
* ------------------O\ = /O------------------
|
* ---------------____/`---'\____---------------
|
* -------------.' \\| |// `.-------------
|
* ------------/ \\||| : |||// \------------
|
* -----------/ _||||| -:- |||||- \-----------
|
* -----------| | \\\ - /// | |-----------
|
* -----------| \_| ''\---/'' | |-----------
|
* -----------\ .-\__ `-` ___/-. /-----------
|
* ---------___`. .' /--.--\ `. . __----------
|
* ------."" '< `.___\_<|>_/___.' >'"".-------
|
* -----| | : `- \`.;`\ _ /`;.`/ - ` : | |-----
|
* -----\ \ `-. \_ __\ /__ _/ .-` / /-----
|
* ======`-.____`-.___\_____/___.-`____.-'======
|
* -------------------`=---='
|
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
* ---------佛祖保佑---hugeinfo---永无BUG----------
|
*/
|