From c354bfc386abb7df1c880a207afe2f2590056b40 Mon Sep 17 00:00:00 2001 From: qinsc Date: Thu, 18 Jun 2020 10:44:55 +0800 Subject: [PATCH] =?UTF-8?q?+=20=E7=BD=91=E5=85=B3=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=88=86=E7=A6=BB=E5=89=8D=E7=AB=AF=E7=9A=84=E8=B7=A8=E8=B6=8A?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/gateway/config/CrosConfig.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 ruoyi-gateway/src/main/java/com/ruoyi/gateway/config/CrosConfig.java diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/config/CrosConfig.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/config/CrosConfig.java new file mode 100644 index 00000000..e4e03f9b --- /dev/null +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/config/CrosConfig.java @@ -0,0 +1,39 @@ +package com.ruoyi.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; + + +@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); + }; + } +}