SpringBoot-CORS:When allowCredentials is true, allowedOrigins cannot contain……value “*“

27 篇文章 0 订阅

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进行操作。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值