[转]When allowCredentials is true, allowedOrigins cannot contain the special value “*“

前言

  项目接口访问出现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

(1)修改前:

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

(2)修改后:

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

原文链接:When allowCredentials is true, allowedOrigins cannot contain the special value “*“

posted @ 2021-12-17 22:59  rainbow70626  阅读(697)  评论(0编辑  收藏  举报