|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+package com.ruoyi.gateway.config;
|
|
|
2
|
+
|
|
|
3
|
+import org.springframework.context.annotation.Bean;
|
|
|
4
|
+import org.springframework.context.annotation.Configuration;
|
|
|
5
|
+import org.springframework.http.HttpHeaders;
|
|
|
6
|
+import org.springframework.http.HttpMethod;
|
|
|
7
|
+import org.springframework.http.HttpStatus;
|
|
|
8
|
+import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
9
|
+import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
|
10
|
+import org.springframework.web.cors.reactive.CorsUtils;
|
|
|
11
|
+import org.springframework.web.server.ServerWebExchange;
|
|
|
12
|
+import org.springframework.web.server.WebFilter;
|
|
|
13
|
+import org.springframework.web.server.WebFilterChain;
|
|
|
14
|
+import reactor.core.publisher.Mono;
|
|
|
15
|
+
|
|
|
16
|
+/**
|
|
|
17
|
+ * 跨域配置
|
|
|
18
|
+ *
|
|
|
19
|
+ * @author ruoyi
|
|
|
20
|
+ */
|
|
|
21
|
+@Configuration
|
|
|
22
|
+public class CorsConfig
|
|
|
23
|
+{
|
|
|
24
|
+ /**
|
|
|
25
|
+ * 这里为支持的请求头,如果有自定义的header字段请自己添加
|
|
|
26
|
+ */
|
|
|
27
|
+ private static final String ALLOWED_HEADERS = "X-Requested-With, Content-Type, Authorization, credential, X-XSRF-TOKEN, token, Admin-Token, App-Token";
|
|
|
28
|
+ private static final String ALLOWED_METHODS = "GET,POST,PUT,DELETE,OPTIONS,HEAD";
|
|
|
29
|
+ private static final String ALLOWED_ORIGIN = "*";
|
|
|
30
|
+ private static final String ALLOWED_EXPOSE = "*";
|
|
|
31
|
+ private static final String MAX_AGE = "18000L";
|
|
|
32
|
+
|
|
|
33
|
+ @Bean
|
|
|
34
|
+ public WebFilter corsFilter()
|
|
|
35
|
+ {
|
|
|
36
|
+ return (ServerWebExchange ctx, WebFilterChain chain) -> {
|
|
|
37
|
+ ServerHttpRequest request = ctx.getRequest();
|
|
|
38
|
+ if (CorsUtils.isCorsRequest(request))
|
|
|
39
|
+ {
|
|
|
40
|
+ ServerHttpResponse response = ctx.getResponse();
|
|
|
41
|
+ HttpHeaders headers = response.getHeaders();
|
|
|
42
|
+ headers.add("Access-Control-Allow-Headers", ALLOWED_HEADERS);
|
|
|
43
|
+ headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS);
|
|
|
44
|
+ headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN);
|
|
|
45
|
+ headers.add("Access-Control-Expose-Headers", ALLOWED_EXPOSE);
|
|
|
46
|
+ headers.add("Access-Control-Max-Age", MAX_AGE);
|
|
|
47
|
+ headers.add("Access-Control-Allow-Credentials", "true");
|
|
|
48
|
+ if (request.getMethod() == HttpMethod.OPTIONS)
|
|
|
49
|
+ {
|
|
|
50
|
+ response.setStatusCode(HttpStatus.OK);
|
|
|
51
|
+ return Mono.empty();
|
|
|
52
|
+ }
|
|
|
53
|
+ }
|
|
|
54
|
+ return chain.filter(ctx);
|
|
|
55
|
+ };
|
|
|
56
|
+ }
|
|
|
57
|
+}
|