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.
1
了解到在较新的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);
}
————————————————
版权声明:本文为CSDN博主「想望着太阳」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44540722/article/details/114021228

posted @ 2022-01-24 08:51  疯子110  阅读(2351)  评论(0编辑  收藏  举报