When allowCredentials is true, allowedOrigin cannot contain the special value 异常

1,609 阅读1分钟

1、问题描述 配置跨域类

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //允许跨域路径
        registry.addMapping("/**")
                //允许跨域请求域名
                .allowedOrigins("*")
                //允许cookie
                .allowCredentials(true)
                //允许的请求方式
                .allowedMethods("GET","POST","DE:ETE","PUT")
                //允许的header树形
                .allowedHeaders("*")
                //跨越允许时间 ms
                .maxAge(3600);
    }
}

启动出现以下错误

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.

2、问题原因 SpringBoot升级2.4.0之后,跨域配置中的.allowedOrigins不再可用

3、解决方式 //允许跨域请求域名 .allowedOrigins("*")

替换成

将配置中的.allowedOrigins替换成.allowedOriginPatterns即可。

参考:blog.csdn.net/weixin_4390…