概述
Spring Cloud Gateway Cors跨域问题的解决
CORS
前后端分离就会碰到跨域问题,原本我们在后端采用 CORS 解决,现在利用网关,可以放在网关解决。
版本:
spring-cloud:Hoxton.SR9
spring-cloud-gateway:2.3.2.RELEASE
一共两个文件
CorsConfiguration.java
/**
* 配置跨域
*/
@Configuration
public class CorsConfiguration {
@Bean
public CorsResponseHeaderFilter corsResponseHeaderFilter() {
return new CorsResponseHeaderFilter();
}
@Bean
public CorsWebFilter corsWebFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setMaxAge(600L);
source.registerCorsConfiguration("/**", corsConfiguration);
return new CorsWebFilter(source);
}
}
CorsResponseHeaderFilter.java
/**
* 跨域请求头重复处理过滤器
*/
public class CorsResponseHeaderFilter implements GlobalFilter, Ordered {
@Override
public int getOrder() {
// 指定此过滤器位于NettyWriteResponseFilter之后
// 即待处理完响应体后接着处理响应头
return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER + 1;
}
@Override
@SuppressWarnings("serial")
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return chain.filter(exchange).then(Mono.defer(() -> {
exchange.getResponse().getHeaders().entrySet().stream()
.filter(kv -> (kv.getValue() != null && kv.getValue().size() > 1))
.filter(kv -> (kv.getKey().equals(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)
|| kv.getKey().equals(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS)))
.forEach(kv ->
{
kv.setValue(new ArrayList<String>() {{
add(kv.getValue().get(0));
}});
});
return chain.filter(exchange);
}));
}
}
Springboot升级至2.4.0中出现的跨域问题。
ERROR 7576 --- [ctor-http-nio-5] a.w.r.e.AbstractErrorWebExceptionHandler : [28bc5558-2] 500 Server Error for HTTP OPTIONS "/api/login"
java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*"since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
at org.springframework.web.cors.CorsConfiguration.validateAllowCredentials(CorsConfiguration.java:457) ~[spring-web-5.3.1.jar:5.3.1]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
很明显还是跨域
When allowCredentials is true, allowedOrigins cannot contain the special value "*"since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
意思就是不能在 configuration.setAllowCredentials(true)的时候对addAllowedOrigin设置为"*",而要使用 allowedOriginPatterns这个字段来设置origin。
对于spring boot 2.4.0出现的跨域异常问题,直接给出解决方案。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;
import java.util.ArrayList;
import java.util.List;
/**
* @Description: 跨域问题
*/
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsWebFilter() {
UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource(new PathPatternParser());
CorsConfiguration configuration = new CorsConfiguration();
// 配置跨域
configuration.setAllowCredentials(true);
List<String> allowedOriginPatterns = new ArrayList<>();
allowedOriginPatterns.add(CorsConfiguration.ALL);
configuration.setAllowedOriginPatterns(allowedOriginPatterns);
configuration.addAllowedHeader(CorsConfiguration.ALL);
configuration.addAllowedMethod(CorsConfiguration.ALL);
configurationSource.registerCorsConfiguration("/**", configuration);
return new CorsWebFilter(configurationSource);
}
}
使用 configuration.setAllowedOriginPatterns(allowedOriginPatterns)
就可以解决问题。
最后
以上就是愤怒方盒为你收集整理的SpringCloud Alibaba 2021微服务实战二十一 Gateway -- Cors解决跨域问题Spring Cloud Gateway Cors跨域问题的解决的全部内容,希望文章能够帮你解决SpringCloud Alibaba 2021微服务实战二十一 Gateway -- Cors解决跨域问题Spring Cloud Gateway Cors跨域问题的解决所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复