跨域问题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 credential...

错误信息:

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.

在访问时出现500错误
解决办法:
将springcloudgateway中的配置中用于跨域配置的类进行修改,具体修改如下:

@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*");//允许跨域访问任何请求方式:post get put delete
//        config.addAllowedOrigin("*");//springboot2.4之前的使用
        config.addAllowedOriginPattern("*");//允许什么样的请求头(springboot2.4之后使用)
        config.addAllowedHeader("*");//允许那种请求来源
        config.setAllowCredentials(true);//设置是否允许cookie进行跨域
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}

根据自己使用的版本进行配置即可

posted @ 2022-12-24 15:07  just1t  阅读(179)  评论(0编辑  收藏  举报