【发布时间】:2020-08-03 15:31:20
【问题描述】:
我正在尝试在我的 Spring Boot API 中实现 CSRF 令牌安全性以了解如何处理它。
我关注了this tutorial (server side part),这是我的安全配置:
private static final String[] CSRF_IGNORE = {"/api/login"};
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.ignoringAntMatchers(CSRF_IGNORE)
.csrfTokenRepository(csrfTokenRepository())
.and()
.addFilterAfter(new CustomCsrfFilter(), CsrfFilter.class)
.exceptionHandling()
.authenticationEntryPoint(new Http403ForbiddenEntryPoint() {
})
.and()
.authenticationProvider(getProvider())
.formLogin()
.loginProcessingUrl("/api/login")
.successHandler(new AuthentificationLoginSuccessHandler())
.failureHandler(new SimpleUrlAuthenticationFailureHandler())
.and()
.logout()
.logoutUrl("/api/logout")
.logoutSuccessHandler(new AuthentificationLogoutSuccessHandler())
.invalidateHttpSession(true)
.and()
.authorizeRequests()
.anyRequest().authenticated();
}
其他与教程相同。
我正在使用 Postman 进行测试。
当我在 CSRF_IGNORE 中添加我想要的端点时,我可以通过 logger/debug 看到存储的令牌,并且来自 cookie 的令牌是相同的,因为安全配置的部分 CustomCsrfFilter.java使用了 .addFilterAfter() 中的 ,但是当我从这个 CSRF_IGNORE 中删除端点时,我得到的是 403,以及 CustomCsrfFilter.java 没有使用,所以我认为没有比较令牌。
我想我错过了一些东西,我想了解一下。
【问题讨论】:
标签: java spring-boot csrf csrf-token