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

最近新写springboot,配置跨域配置文件后出现的问题。

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is 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.

了解到在较新的springboot版本中,跨域配置需要将 .allowedOrigins 替换成 .allowedOriginPatterns

原配置:

@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
                .maxAge(3600);
    }

替换后:

@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
                .maxAge(3600);
    }

版权声明:本文为weixin_44540722原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。