When allowCredentials is true, allowedOrigins cannot contain the special value “*“ since that canno

错误产生环境

SpringBoot实现 WebMvcConfigurer 接口解决跨域问题时产生

错误描述

Caused by: 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.

错误原因

解决办法

.allowedOrigins("*") 改为 .allowedOriginPatterns("*")

修改前

    @Override
    public void addCorsMappings(CorsRegistry corsRegistry){
    
    
        /**
         * 所有请求都允许跨域,使用这种配置就不需要
         * 在interceptor中配置header了
         */
        corsRegistry.addMapping("/**")
                //是否发送Cookie
                .allowCredentials(true)
                //放行哪些原始域
                .allowedOrigins("*")
                .allowedMethods(new String[]{
    
    "GET", "POST", "PUT", "DELETE"})
                .allowedHeaders("*");
    }

修改后

@Override
    public void addCorsMappings(CorsRegistry corsRegistry){
    
    
        /**
         * 所有请求都允许跨域,使用这种配置就不需要
         * 在interceptor中配置header了
         */
        corsRegistry.addMapping("/**")
                //是否发送Cookie
                .allowCredentials(true)
                //放行哪些原始域
                .allowedOriginPatterns("*")
                .allowedMethods(new String[]{
    
    "GET", "POST", "PUT", "DELETE"})
                .allowedHeaders("*");
    }

猜你喜欢

转载自blog.csdn.net/qq_42025798/article/details/124282813