美文网首页
Springboot 跨域配置

Springboot 跨域配置

作者: 阿群1986 | 来源:发表于2021-12-31 15:57 被阅读0次

    访问后端接口遇到错误提示:

    When allowCredentials is true,  allowedOrigins cannot contain the special value "*"
    

    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.

    修改方式

    替换allowedOriginsallowedOriginPatterns
    修改前:

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

    修改后:

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*") // <--这里已替换为 allowedOriginPatterns
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
    

    allowedOriginPatterns

    相关文章

      网友评论

          本文标题:Springboot 跨域配置

          本文链接:https://www.haomeiwen.com/subject/ntogqrtx.html