Spring Cloud Gateway系列(二):CORS跨域配置

        对于云Paas平台,对外提供的API需要满足客户多种场景的调用需求,其中就包括WEB端的对接。由于浏览器同源策略的限制,Spring Cloud Gateway接收到的客户WEB场景下请求必然存在跨域问题,浏览器将CORS请求分成两类:简单请求(simple request)和非简单请求(not-so-simple request)。这篇文章对CORS进行了详细的介绍,拜读之后受益匪浅。https://www.ruanyifeng.com/blog/2016/04/cors.html

 SpringBoot自动装配的核心就是在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,使自动配置类生效,帮我们进行自动配置工作。 

SimpleUrlHandlerMappingGlobalCorsAutoConfiguration类 

@Configuration
@ConditionalOnClass(SimpleUrlHandlerMapping.class)
@ConditionalOnProperty(name = "spring.cloud.gateway.globalcors.add-to-simple-url-handler-mapping", matchIfMissing = false)
public class SimpleUrlHandlerMappingGlobalCorsAutoConfiguration {

	@Autowired
	private GlobalCorsProperties globalCorsProperties;

	@Autowired
	private SimpleUrlHandlerMapping simpleUrlHandlerMapping;

	@PostConstruct
	void config() {
       //设置到在application.properties文件中定义的CORS相关的属性
		simpleUrlHandlerMapping
				.setCorsConfigurations(globalCorsProperties.getCorsConfigurations());
	}

}

SimpleUrlHandlerMapping类是AbstractHandlerMapping的子类,实际进入的是AbstractHandlerMapping的setCorsConfigurations方法。 

 Gateway对CORS处理的核心方法是AbstractHandlerMapping的getHandler(exchange)方法,我们在配置文件中指定的全局跨域规则就是在这个方法里生效的!

/*application.properties文件中指定的跨域配置就是在这里生效的
*/
public void setCorsConfigurations(Map<String, CorsConfiguration> corsConfigurations) {
		Assert.notNull(corsConfigurations, "corsConfigurations must not be null");
		this.corsConfigurationSource = new UrlBasedCorsConfigurationSource(this.patternParser);
		((UrlBasedCorsConfigurationSource) this.corsConfigurationSource).setCorsConfigurations(corsConfigurations);
	}

@Override
public Mono<Object> getHandler(ServerWebExchange exchange) {
               /*getHandlerInternal方法是一个抽象方法
                 由AbstractHandlerMapping的子类实现该方法
                */
		return getHandlerInternal(exchange).map(handler -> {
			if (logger.isDebugEnabled()) {
				logger.debug(exchange.getLogPrefix() + "Mapped to " + handler);
			}
            /* REQUEST_HANDLED_HANDLER是一个函数式接口 exhange->Mono.empty()
               如果客户端请求是跨域请求,就会去读区配置文件
               判断当前请求是否允许跨域         
            */
			if (CorsUtils.isCorsRequest(exchange.getRequest())) {
                /*从上面方法可以看到
                 实例化的是UrlBasedCorsConfigurationSource*/
				CorsConfiguration configA = this.corsConfigurationSource.getCorsConfiguration(exchange);
				CorsConfiguration configB = getCorsConfiguration(handler, exchange);
				CorsConfiguration config = (configA != null ? configA.combine(configB) : configB);
                /*
                  CORS的核心 使用的是Spring5特有的PathPattern
                  这里先给出跨域配置样例,后续会专门分析这个方法
spring.cloud.gateway.globalcors.corsConfigurations.[/*/*/users/*/message_roaming/*].allowedOrigins=*
spring.cloud.gateway.globalcors.corsConfigurations.[/*/*/users/*/message_roaming/*].allowedHeaders=*
spring.cloud.gateway.globalcors.corsConfigurations.[/*/*/users/*/message_roaming/*].allowedMethods=*
spring.cloud.gateway.globalcors.corsConfigurations.[/*/*/users/*/message_roaming/*].allowCredentials=true
spring.cloud.gateway.globalcors.corsConfigurations.[/*/*/users/*/message_roaming/*].maxAge=1728000
                  需要特别注意的就是,如果客户端请求是跨域请求
                  如果存在配置属性与之匹配,就会放行
                  如果不存在,则会拒绝该请求,设置ResponseHeader,返回REQUEST_HANDLED_HANDLER,
                */
				if (!getCorsProcessor().process(config, exchange) ||
						CorsUtils.isPreFlightRequest(exchange.getRequest())) {
					return REQUEST_HANDLED_HANDLER;
				}
			}
			return handler;
		});
}
UrlBasedCorsConfigurationSource类的getCorsConfiguration(exchange)方法简单明了,Spring5使用了全新的路径解析器PathPattern,抛弃了原先的AntPathMatcher。新的路径匹配器围绕着PathPattern拥有一套体系,在设计上更具模块化、更加面向对象,从而拥有了更好的可读性和可扩展性。
/**
 * Provide a per reactive request {@link CorsConfiguration} instance based on a
 * collection of {@link CorsConfiguration} mapped on path patterns.
 *
 * <p>Exact path mapping URIs (such as {@code "/admin"}) are supported
 * as well as Ant-style path patterns (such as {@code "/admin/**"}).
 *
 * @author Sebastien Deleuze
 * @author Brian Clozel
 * @since 5.0
 */
public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource {

	private final Map<PathPattern, CorsConfiguration> corsConfigurations;

	@Override
	@Nullable
	public CorsConfiguration getCorsConfiguration(ServerWebExchange exchange) {
		PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication();
		return this.corsConfigurations.entrySet().stream()
				.filter(entry -> entry.getKey().matches(lookupPath))
				.map(Map.Entry::getValue)
				.findFirst()
				.orElse(null);
	}

}

如果CORS请求能够通过PathPattern与application.properties文件指定的配置相匹配,就能得到CorsConfiguration类,接着就是AbstractHandlerMapping中的getCorsProcessor().process(config, exchange)方法调用,对跨域请求Response请求HttpHeader的设置,通俗易懂,有兴趣的话可以分析一下。

CorsUtils工具类

/**
 * Utility class for CORS reactive request handling based on the
 * <a href="https://www.w3.org/TR/cors/">CORS W3C recommendation</a>.
 *
 * @author Sebastien Deleuze
 * @since 5.0
 */
public abstract class CorsUtils {

	/**通过HttpHeader是否含有请求头Origin判断请求是否为跨域请求
	 * Returns {@code true} if the request is a valid CORS one.
	 */
	public static boolean isCorsRequest(ServerHttpRequest request) {
		return (request.getHeaders().get(HttpHeaders.ORIGIN) != null);
	}

	/**
     *对于CORS非简单请求 浏览器会首先发起HttpMethod为OPITION的预检请求
	 * Returns {@code true} if the request is a valid CORS pre-flight one.
	 */
	public static boolean isPreFlightRequest(ServerHttpRequest request) {
		return (request.getMethod() == HttpMethod.OPTIONS && isCorsRequest(request) &&
				request.getHeaders().get(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD) != null);
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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是一个安全特性,您应该根据您的应用程序需求和安全性要求进行适当的配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值