|
@@ -1,3 +1,4 @@
|
|
|
+/*
|
|
|
package com.boman.gateway.config;
|
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
@@ -7,10 +8,12 @@ import org.springframework.web.cors.reactive.CorsWebFilter;
|
|
|
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
|
|
|
import org.springframework.web.util.pattern.PathPatternParser;
|
|
|
|
|
|
-/** 跨域请求
|
|
|
- * @author shiqian
|
|
|
- * @date 2021年06月04日 14:28
|
|
|
- **/
|
|
|
+//* 跨域请求
|
|
|
+// * @author shiqian
|
|
|
+// * @date 2021年06月04日 14:28
|
|
|
+// *
|
|
|
+
|
|
|
+
|
|
|
@Configuration
|
|
|
public class CorsConfig {
|
|
|
|
|
@@ -59,3 +62,46 @@ public class CorsConfig {
|
|
|
// }
|
|
|
|
|
|
}
|
|
|
+*/
|
|
|
+
|
|
|
+package com.boman.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.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;
|
|
|
+
|
|
|
+/** 跨域请求
|
|
|
+ * @author shiqian
|
|
|
+ * @date 2021年06月04日 14:28
|
|
|
+ **/
|
|
|
+@Configuration
|
|
|
+public class CrosConfig {
|
|
|
+
|
|
|
+ @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.add("Access-Control-Allow-Origin", "*");
|
|
|
+ headers.add("Access-Control-Allow-Methods", "*");
|
|
|
+ headers.add("Access-Control-Allow-Headers", "*");
|
|
|
+ headers.add("Access-Control-Allow-Credentials", "true");
|
|
|
+ if (request.getMethod() == HttpMethod.OPTIONS) {
|
|
|
+ response.setStatusCode(HttpStatus.OK);
|
|
|
+ return Mono.empty();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return chain.filter(ctx);
|
|
|
+ };
|
|
|
+ }
|
|
|
+}
|