SpringBoot-bug解决

SpringBoot-BUG解决

跨域配置失效(SpringBoot版本问题)

@Configuration
public class CrosConfig implements WebMvcConfigurer {

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

采用SpringBoot 2.4.0版本下,配置跨域,报错

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

错误为:当设置了allowCredentials=true的时候,服务器端响应的Access-Control-Allow-Origin头,它的值不能是*,必须要明确的指定出客户端的origin

.allowedOrigins("http://localhost")

指明具体客户端origin后,虽无报错,但浏览器访问仍然显示跨域失败。

最终方法:将SpringBoot版本切换为2.2.x,成功解决

…..尚未完结,持续更新


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!