|
|
@@ -0,0 +1,46 @@
|
|
|
+package com.jzg.config;
|
|
|
+
|
|
|
+import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+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.web.cors.CorsConfiguration;
|
|
|
+import org.springframework.web.cors.reactive.CorsUtils;
|
|
|
+import org.springframework.web.cors.reactive.CorsWebFilter;
|
|
|
+import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
|
|
|
+import org.springframework.web.server.ServerWebExchange;
|
|
|
+import org.springframework.web.server.WebFilter;
|
|
|
+import org.springframework.web.server.WebFilterChain;
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class CorsConfig {
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public CorsWebFilter corsWebFilter() {
|
|
|
+ CorsConfiguration corsConfiguration = new CorsConfiguration();
|
|
|
+ // 允许所有来源,你可以根据需要修改为具体的来源
|
|
|
+ corsConfiguration.addAllowedOrigin("http://192.168.5.41:9000");
|
|
|
+ // 允许的方法
|
|
|
+ corsConfiguration.addAllowedMethod("*");
|
|
|
+ // 允许的头信息
|
|
|
+ corsConfiguration.addAllowedHeader("*");
|
|
|
+ // 允许携带凭证,例如 Cookie 等
|
|
|
+ corsConfiguration.setAllowCredentials(true);
|
|
|
+ // 预检请求的缓存时间(秒)
|
|
|
+ corsConfiguration.setMaxAge(3600L);
|
|
|
+
|
|
|
+ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
|
|
+ // 应用到所有路径
|
|
|
+ source.registerCorsConfiguration("/**", corsConfiguration);
|
|
|
+
|
|
|
+ return new CorsWebFilter(source);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|