SpringBoot 2.4CORS配置出现问题:
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("*")
中的*号不能和.allowCredentials(true)
同时存在,
查看我的配置文件中跨域相关配置
@Configuration
public class corsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
//对那些请求路径进行跨域处理
registry.addMapping("/**")
// 允许访问的客户端域名
.allowedOrigins("*")
// 允许访问的方法名
.allowedMethods("*")
// 允许服务端访问的客户端请求头
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3000);
}
}
通过查看这篇博客Springboot升级至2.4.0中出现的跨域问题
简单的说,就是这个问题在org.springframework.web.cors.CorsConfiguration
public void validateAllowCredentials() {
if (this.allowCredentials == Boolean.TRUE && this.allowedOrigins != null && this.allowedOrigins.contains("*")) {
throw new 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.");
}
}
这题可以使用addAllowedOriginPattern("*")
代替addAllowedOrigin("*")
Access-Control-Allow-Origin的作用在于,允许(浏览器)什么域名访问我这个接口。如果值为 ‘http://xxx.com’,则表示只接受来自这个域名的请求,其他的一律拒绝。
假设服务器端设置了Access-Control-Allow-Origin为* 那意味这将cookie开放给了所有的网站。这就会引发大问题:B网站可以使用A网站在cookie中写入的凭证对A进行操作。