【发布时间】:2019-05-27 08:00:27
【问题描述】:
我阅读了很多关于这个问题的线程和解决方案(包括this SO solution),但是我在发送预检请求时仍然遇到 403 错误。
我正在使用 Spring Data Rest,只要没有发送 OPTIONS,我就可以很好地使用我的存储库。我还没有使用 Spring Security,但我计划很快对其进行配置。这是我目前的配置:
@Configuration
public class GlobalRepositoryRestConfigurer extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.getCorsRegistry().addMapping("/**").allowedOrigins("*").allowedHeaders(
"*").exposedHeaders("Location").allowedMethods("GET", "PUT", "POST", "DELETE",
"OPTIONS");
}
@Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServlet dispatcherServlet() {
DispatcherServlet dispatcher = new DispatcherServlet();
dispatcher.setDispatchOptionsRequest(true);
return dispatcher;
}
}
我也尝试了 application.properties 选项,并将我的 allowedMethods 设置为 "*" ,但无论如何我最终都会得到 403。以下是我从 OPTIONS 请求中获得的请求/响应标头。
请求标头
Accept text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Access-Control-Request-Headers content-type
Access-Control-Request-Method POST
Connection keep-alive
Host localhost:8080
Origin http://localhost:4000
Referer http://localhost:4000/
User-Agent Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/64.0
响应标头
Allow GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH
Content-Length 20
Date Sun, 30 Dec 2018 08:49:00 GMT
你有什么问题或者我可以尝试的其他方法吗?
【问题讨论】:
-
我认为你的 corsconfiguration 在你的应用程序的其他地方被覆盖了,你能验证一下吗?
-
没有其他地方明确定义了 CORS 配置,您认为它可能是一些默认的 Spring 配置吗?我该如何检查?无论如何,我已经验证了
allowMethods是否有效(我发布的 conf 和响应标头之间存在不匹配,但我认为我复制了一个使用我以前的 conf 发出的请求,该请求允许更多方法),以及 @987654329 @.
标签: spring spring-boot cors preflight