跨域报错:When allowCredentials is true, allowedOrigins cannot contain the special value “*“

927 阅读1分钟

我一开始写的代码是这样的:

@CrossOrigin(origins = "*",allowCredentials="true",allowedHeaders = "*",methods = {})

跨域报错日志

org.springframework.beans.factory.BeanCreationException: 
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.

在这里插入图片描述

根据提示我把代码改成这样:

@CrossOrigin(originPatterns = "*",allowCredentials="true",allowedHeaders = "*",methods = {})

然后就成功了。

如果是配置类

将里面的 addAllowedOrigin 改成 allowedOriginPatterns 这样就中了


道阻且长,行则将至