springboot 跨域配置报特殊符号错误

1,118 阅读1分钟

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.

当allowCredentials为true时,allowedOrigins不能包含特殊值“ *”,因为无法在“ Access-Control-Allow-Origin”响应标头上设置。要允许凭据为一组来源,请明确列出它们或考虑使用“ allowedOriginPatterns” 代替。

将跨域设置放在gateway模块中:

image.png

解决办法:跨域配置报错,将.allowedOrigins替换成.allowedOriginPatterns即可。

/**
 * 配置跨域
 * @return
 */
@Bean
public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();

        config.setAllowCredentials(Boolean.TRUE);
        config.addAllowedMethod("*");
        config.addAllowedOriginPattern("*");
        config.addAllowedHeader("*");
        config.setMaxAge(3600L);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
}