From 21c3635a04e8ae4802b6541169b88296d08098ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Sun, 7 Aug 2022 13:23:20 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=A2=9E=E5=8A=A0=20=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E8=B7=A8=E5=9F=9F=E8=BF=87=E6=BB=A4=E5=99=A8=20=E5=A4=84?= =?UTF-8?q?=E7=90=86=E8=B7=A8=E5=9F=9F=E8=AF=B7=E6=B1=82=20=E9=80=82?= =?UTF-8?q?=E9=85=8D=E7=A7=BB=E5=8A=A8=E7=AB=AF=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gateway/filter/GlobalCorsFilter.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/GlobalCorsFilter.java diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/GlobalCorsFilter.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/GlobalCorsFilter.java new file mode 100644 index 00000000..45f9c5a1 --- /dev/null +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/GlobalCorsFilter.java @@ -0,0 +1,58 @@ +package com.ruoyi.gateway.filter; + +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.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; + + +/** + * 跨域配置 + * + * @author Lion Li + */ +@Component +public class GlobalCorsFilter implements WebFilter, Ordered { + + /** + * 这里为支持的请求头,如果有自定义的header字段请自己添加 + */ + private static final String ALLOWED_HEADERS = "X-Requested-With, Content-Type, Authorization, credential, X-XSRF-TOKEN, token, Admin-Token, App-Token"; + private static final String ALLOWED_METHODS = "GET,POST,PUT,DELETE,OPTIONS,HEAD"; + private static final String ALLOWED_ORIGIN = "*"; + private static final String ALLOWED_EXPOSE = "*"; + private static final String MAX_AGE = "18000L"; + + @Override + public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { + ServerHttpRequest request = exchange.getRequest(); + if (CorsUtils.isCorsRequest(request)) { + ServerHttpResponse response = exchange.getResponse(); + HttpHeaders headers = response.getHeaders(); + headers.add("Access-Control-Allow-Headers", ALLOWED_HEADERS); + headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS); + headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN); + headers.add("Access-Control-Expose-Headers", ALLOWED_EXPOSE); + headers.add("Access-Control-Max-Age", MAX_AGE); + headers.add("Access-Control-Allow-Credentials", "true"); + if (request.getMethod() == HttpMethod.OPTIONS) { + response.setStatusCode(HttpStatus.OK); + return Mono.empty(); + } + } + return chain.filter(exchange); + } + + @Override + public int getOrder() { + return Ordered.HIGHEST_PRECEDENCE; + } +}