有关JWT令牌和CORS的问题,并反应
我正在尝试创建使用令牌身份验证方法的Spring Boot应用程序。我想轻松一点.com/spring-boot-login-mysql/作为灵感。没有SQL问题。我的代码与那里的代码完全相同。
当我在Postman中提出要求时,一切都很好,没有错。 当我在前端执行请求时,我会发现我想念标头或某种的CORS错误。我通过在此时在项目中添加以下类来解决
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.allowCredentials(true)
.allowedHeaders("*")
.allowedOrigins("http://localhost:3000");
}
}
,我将获得具有正确值的set-cookie
标题,但未设置cookie。我还在AXIOS中的标题请求中添加了withCredentials:true
。有人可以向我解释发生了什么事,并使用React作为前端展示了该问题的解决方案?
非常感谢!
I am trying to create a spring boot application that uses a token authentication method. I wanted to go easy so I used this repo https://www.bezkoder.com/spring-boot-login-example-mysql/ as inspiration. No SQL problems. My code is exactly the same as that one there.
When I am doing requests in POSTMAN everything works fine and nothing is wrong.
When I am doing a request in the front end, I get a CORS error that I miss a header or some sort. I fixed that by adding the following class in the project
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.allowCredentials(true)
.allowedHeaders("*")
.allowedOrigins("http://localhost:3000");
}
}
At that point, I get the set-cookie
header with the correct value, but the cookie is not set. I have also added the withCredentials: true
in the header request in AXIOS. Can someone explain to me what is going on and show a solution to this problem using React as frontend?
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用此功能,看看它是否有效,是否确保在您的应用程序中创建另一个Java类。PropertiesWrite App.Client.URL =“ Local -Host Your URL”
@order(orded.highest_precedence)
公共类SimpereCorsFilter实施过滤{
}
use this and see whether it works or not make sure to create a different java class and in your application.properties write app.client.url="localhost your url"
@Order(Ordered.HIGHEST_PRECEDENCE)
public class SimpleCorsFilter implements Filter {
}