spingboot升级之后之前的访问页面和接口出现这样的错误:
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.
之前的版本是2.1.5.RELASE 跨域是这样设置的
@Configuration
public class AbsWebMvcConfigurerAdapter extends WebMvcConfigurationSupport {
@Override
public void addCorsMappings(CorsRegistry registry) {
// 添加映射路径
registry.addMapping("/**")
// 是否发送Cookie
.allowCredentials(true)
//设置放行哪些原始域 SpringBoot2.4.4下低版本使用.allowedOrigins("*")
.allowedOrigins("*")
//放行哪些请求方式
.allowedMethods(new String[] { "GET", "POST", "PUT", "DELETE" })
//.allowedMethods("*")
//或者放行全部
// 放行哪些原始请求头部信息
.allowedHeaders("*");
}
}
升级到2.4.0之后的版本
@Configuration
public class AbsWebMvcConfigurerAdapter extends WebMvcConfigurationSupport {
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
// 是否允许请求带有验证信息
config.setAllowCredentials(true);
// 允许访问的客户端域名
// (springboot2.4以上的加入这一段可解决 allowedOrigins cannot contain the special value
// "*"问题)
List<String> allowedOriginPatterns = new ArrayList<>();
allowedOriginPatterns.add("*");
config.setAllowedOriginPatterns(allowedOriginPatterns);
// 设置访问源地址
// config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
希望对你有所帮助