CORS即使在配置中启用了我的请求,也会阻止我的请求
嘿,我正在React建立一个网站,我尝试从春季构建的后端服务器中获取数据,但它行不通。我将CorsConfigurationsource添加到我的安全配置中,但它仍然无法使用。 我的CORS安全配置:
@Bean
CorsConfigurationSource corsConfigurationSource(){
CorsConfiguration corsConfiguration=new CorsConfiguration();
corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:3000"));
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowedMethods(Arrays.asList("GET","POST"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return source;
}
我最终遇到的错误: https://i.sstatic.net /olgcd.png \ 我的提取请求(我正在发布用户凭据):
fetch("http://localhost:8080/login", {
method: "POST",
mode:'cors',
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: new URLSearchParams(tempLoginCredentials),
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Google Chrome中的CORS在
localhost
上有点特别。阅读有关它的更多信息。
最重要的是,港口确实在CORS中很重要。请注意,您在弹簧配置中启用了端口3000,但正在获取8080。
CORS in Google Chrome is a bit special on
localhost
.Read more about it here.
On top of that, PORT does matter in CORS. Note that you enabled port 3000 in your Spring config, but are fetching 8080.
我认为您需要更多的标题和选项方法。
I think you need more headers and OPTIONS method.