如何解决跨域的问题
我们在使用Spring boot+vue开发前后端分离的项目时,会提示Access-Control-Allow-Origin跨域问题。
Caused by: 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.
解决方案一
@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("*");
}
}
解决方案二
@Configuration
public class CrosConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(false)
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.allowedOrigins("*");
}
};
}
}