SpringCloudGateway CORS方案看这篇就够了

本文介绍了SpringCloud Gateway中遇到的跨域问题及其解决方案。在前后端分离的项目中,Gateway作为API网关,需要处理跨域问题。文章详细解析了Spring WebFlux的DispatcherHandler处理请求的过程,以及Spring Cloud Gateway的CORS处理机制。提出了两种解决方案:利用DedupeResponseHeader配置或手动编写GlobalFilter来修改Response头,避免出现多个'Access-Control-Allow-Origin'头导致的错误。并给出了配置和自定义Filter的注意事项。
摘要由CSDN通过智能技术生成

问 题

在 SpringCloud 项目中,前后端分离目前很常见,在调试时,会遇到两种情况的跨域:

| 前端页面通过不同域名或IP访问微服务的后台

例如前端人员会在本地起HttpServer 直连后台开发本地起的服务,此时,如果不加任何配置,前端页面的请求会被浏览器跨域限制拦截,所以,业务服务常常会添加如下代码设置全局跨域:

@Bean
public CorsFilter corsFilter() {
    logger.debug("CORS限制打开");
    CorsConfiguration config = new CorsConfiguration();
    # 仅在开发环境设置为*
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    config.setAllowCredentials(true);
    UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
    configSource.registerCorsConfiguration("/**", config);
    return new CorsFilter(configSource);
}

| 前端页面通过不同域名或IP访问SpringCloud Gateway

例如前端人员在本地起HttpServer直连服务器的Gateway进行调试。此时,同样会遇到跨域。需要在Gateway的配置文件中增加:

spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
        # 仅在开发环境设置为*
          '[/**]':
            allowedOrigins: "*"
            allowedHeaders: "*"
            allowedMethods: "*"

那么,此时直连微服务和网关的跨域问题都解决了,是不是很完美?

No~ 问题来了,前端仍然会报错:“不允许有多个’Access-Control-Allow-Origin’ CORS头”

Access to XMLHttpRequest at 'http://192.168.2.137:8088/api/two' from origin 'http://localhost:3200' has been blocked by CORS policy: 
The 'Access-Control-Allow-Origin' header contains multiple values '*, http://localhost:3200', but only one is allowed.

仔细查看返回的响应头,里面包含了两份Access-Control-Allow-Origin头。

我们用客户端版的PostMan做一个模拟,在请求里设置头:Origin : * ,查看返回结果的头:

不能用Chrome插件版,由于浏览器的限制,插件版设置Origin的Header是无效的

发现问题了:Vary 和 Access-Control-Allow-Origin 两个头重复了两次,其中浏览器对后者有唯一性限制!

分 析

Spring Cloud Gateway是基于SpringWebFlux的,所有web请求首先是交给DispatcherHandler进行处理的,将HTTP请求交给具体注册的handler去处理。

我们知道Spring Cloud Gateway进行请求转发,是在配置文件里配置路由信息,一般都是用url predicates模式,对应的就是RoutePredicateHandlerMapping 。所以,DispatcherHandler会把请求交给 RoutePredicateHandlerMapping.

RoutePredicateHandlerMapping.getHandler(ServerWebExchange exchange) 方法,默认提供者是其父类 AbstractHandlerMapping :

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 GatewayCORS支持。请记住,CORS是一个安全特性,您应该根据您的应用程序需求和安全性要求进行适当的配置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值