spring-boot项目更新代码后,试运行,出现错误:
Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource
[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed;
nested exception is 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.
关键是开启debug,居然找不到是在哪个文件引起的错误。
找错过程:
1、修改、注释、删除 跨域过滤器。
@WebFilter(filterName = "CorsFilter")
@Configuration
依旧报错,说明不是这个文件的问题,恢复。
2、web配置,在配置类中增加
@Configuration
public class WebConfig implements WebMvcConfigurer{
......
/**
* 增加代码
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
// 设置允许跨域的路由
registry.addMapping("/api/**")
// 设置允许跨域请求的域名
.allowedOriginPatterns("*") // 注意此处
// 是否允许证书(cookies)
.allowCredentials(true)
// 设置允许的方法
.allowedMethods("*")
// 跨域允许时间
.maxAge(3600);
}
}
试运行,依旧报错,非问题所在,恢复。
3、没办法,根据报错,在报错的前后文件中打断点,各种步调。发现有个验证码的文件
估计就是这里面了,允许跨域请求验证码。
@CrossOrigin(originPatterns = "*", maxAge = 3600, allowCredentials = "true")
就是这个文件里面的*号,导致的问题,经网上查,修改为
@CrossOrigin(originPatterns = "/**", maxAge = 3600, allowCredentials = "true")
解决。