spring cloud gateway 配置cors
时间: 2023-09-19 10:07:39 浏览: 183
在Spring Cloud Gateway中配置CORS(跨域资源共享),您可以按照以下步骤进行:
1. 首先,添加一个全局的CorsGlobalFilter Bean,用于处理CORS跨域请求。创建一个新的类文件,比如CustomCorsGlobalFilter:
```java
import org.springframework.boot.web.reactive.filter.OrderedCorsFilter;
import org.springframework.core.Ordered;
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.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsUtils;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import reactor.core.publisher.Mono;
@Component
public class CustomCorsGlobalFilter implements WebFilter, Ordered {
private static final String ALLOWED_HEADERS = "x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN";
private static final String ALLOWED_METHODS = "GET, PUT, POST, DELETE, OPTIONS";
private static final String ALLOWED_ORIGIN = "*";
private static final String ALLOWED_EXPOSE = "Authorization";
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
if (CorsUtils.isCorsRequest(request)) {
ServerHttpResponse response = exchange.getResponse();
HttpHeaders headers = response.getHeaders();
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, ALLOWED_ORIGIN);
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, ALLOWED_HEADERS);
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, ALLOWED_METHODS);
headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, ALLOWED_EXPOSE);
headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "3600");
if (request.getMethod() == HttpMethod.OPTIONS) {
response.setStatusCode(HttpStatus.OK);
return Mono.empty();
}
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}
```
2. 然后,您需要将这个Filter注册到Spring Boot应用程序中。在您的Spring Boot主类中,添加`@EnableWebFlux`注解,并且创建一个名为`customCorsFilter`的Bean:
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.config.EnableWebFlux;
@SpringBootApplication
@EnableWebFlux
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
@Bean
public CustomCorsGlobalFilter customCorsFilter() {
return new CustomCorsGlobalFilter();
}
}
```
3. 最后,您可以在application.properties(或application.yml)文件中配置其他CORS属性,例如允许的源和方法:
```properties
spring:
cloud:
gateway:
globalcors:
corsConfigurations:
'[/**]':
allowedOrigins: "*"
allowedMethods: "GET, PUT, POST, DELETE, OPTIONS"
allowedHeaders: "x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN"
exposedHeaders: "Authorization"
maxAge: 3600
```
注意:以上只是一个示例配置,您可以根据您的实际需求进行调整。
这样,您就成功地配置了Spring Cloud Gateway的CORS支持。请记住,CORS是一个安全特性,您应该根据您的应用程序需求和安全性要求进行适当的配置。
阅读全文