【发布时间】:2020-03-28 09:33:47
【问题描述】:
我尝试通过 Axios POST 请求从 Springboot 服务器获取 jwt 令牌,但出现以下错误:
xhr.js:166 OPTIONS url net::ERR_ABORTED 403
Access to XMLHttpRequest at 'url' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
它似乎无法通过带有“Access-Control-Allow-Origin”标头的预检请求。所以我在标题上配置了“Access-Control-Allow-Origin”,但不知何故它仍然无法正常工作。
代码如下:
反应:
return Axios({
method: 'post',
url: 'url',
data: qs.stringify(json),
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
'authorization': 'Basic token',
},
});
Spring Boot:
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration conf = new CorsConfiguration();
conf.setAllowedOrigins(Arrays.asList("*"));
conf.setAllowedMethods(Arrays.asList("POST", "GET", "OPTIONS", "DELETE", "PUT"));
conf.setAllowedHeaders(Arrays.asList("Content-Type", "X-Requested-With", "accept,Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", "Authorization", "Cache-Control", "Access-Control-Allow-Origin"));
conf.setAllowCredentials(true);
conf.setMaxAge(3600L);
...
}
【问题讨论】:
-
我认为您的服务器配置错误。在这种情况下,客户无事可做
标签: spring spring-boot cors axios