反应 + springboot csrf [英] React + springboot csrf
问题描述
我在 springboot 项目中有一个 react 应用程序,react 应用程序使用 rest 调用来获取/设置内容.实际上,我已经在配置适配器 .csrf().disable()
中禁用了 csrf,但我想对此进行管理.我如何处理 react 和 springboot 之间的 csrf 令牌?
i've a react application inside a springboot project, the react application use rest calls for get/set stuff. Actually i've disabled csrf inside the configure adapter .csrf().disable()
but i'd like to menage this.
How can i handle csrf token between react and springboot?
我认为我应该通过我的 axios 调用传递令牌,但我如何得到它?
I think that i should pass the token through my axios call, but how i get it?
谢谢
推荐答案
您需要将 CSRF-TOKEN
保存到 cookie 并与请求头一起发送回来.
You need to save CSRF-TOKEN
to cookie and send it back with the request header.
SecurityConfig 类.
SecurityConfig class.
启用 csrftoken 存储库
Enable csrftokenrepsitory
.csrf().csrfTokenRepository(csrfTokenRepository()).and().addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class).addFilterAfter(new XSSFilter(), CsrfFilter.class);
添加 csrfTokenRepository
Add csrfTokenRepository
private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setHeaderName(X_CSRF_TOKEN);
return repository;
}
作为反应,您可以从 cookie 访问令牌.
In react, you can access token from the cookie.
csrfToken= cookies.get('XSRF-TOKEN');
在标题中按如下方式发送.
Send it as follows in the header.
headers: {
'X-XSRF-TOKEN': this.csrfToken,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
这篇关于反应 + springboot csrf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!