如何在Spring Boot中获得CSRF令牌
我从请求中面临NULL CSRF令牌的问题。
//获取CSRF代币
object obj = request.getSession()。getAttribute(websesserservercsrftokenrepository.class.getname().concat(“。csrf_token”));
我们已经实现了以下CSRF生成的代码。
//用于生成CSRF代币
@Bean
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
http.csrf()
.csrfTokenRepository(new WebSessionServerCsrfTokenRepository())
.requireCsrfProtectionMatcher(getURLsForDisabledCSRF()).and()
.authorizeExchange()
.pathMatchers(ALLOWED_PATHS).permitAll()
.pathMatchers(ALLOWED_METHODS).permitAll()
.anyExchange()
.authenticated().and()
.securityContextRepository(NoOpServerSecurityContextRepository.getInstance()).formLogin().disable()
.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint())
.and().csrf(csrf -> csrf.csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse()));
return http.build();
//.httpBasic().disable().formLogin().disable().build();
}
Please help here I am stuck on this issue last 2 weeks.
提前感谢
I am facing an issue with Null CSRF token from the request.
//For getting CSRF token
Object obj=request.getSession().getAttribute(WebSessionServerCsrfTokenRepository.class.getName() .concat(".CSRF_TOKEN"));
We have implemented below code for CSRF generating.
//For generating CSRF token
@Bean
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
http.csrf()
.csrfTokenRepository(new WebSessionServerCsrfTokenRepository())
.requireCsrfProtectionMatcher(getURLsForDisabledCSRF()).and()
.authorizeExchange()
.pathMatchers(ALLOWED_PATHS).permitAll()
.pathMatchers(ALLOWED_METHODS).permitAll()
.anyExchange()
.authenticated().and()
.securityContextRepository(NoOpServerSecurityContextRepository.getInstance()).formLogin().disable()
.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint())
.and().csrf(csrf -> csrf.csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse()));
return http.build();
//.httpBasic().disable().formLogin().disable().build();
}
Please help here I am stuck on this issue last 2 weeks.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们找到了从cookie读取令牌的解决方案。
csrftoken obj = new CookiecsrftokenRepository()。loadToken(request);
我们还在cookie中实现了XSRF-Token设置的WebFilter。
We have find the solution for the reading the token from the cookie.
CsrfToken obj=new CookieCsrfTokenRepository().loadToken(request);
We have also implement the WebFilter for the XSRF-TOKEN setting in the cookie.