我正在开发一个使用默认 Thymeleaf 并使用 REST API 访问数据库的 Spring Boot 项目。一切正常,直到渗透测试结果要求我将 cookie 设置为安全标记。我已阅读这些链接:
我已经尝试过这两种方法,安全标志存在。
安全标志 cookie 的屏幕截图
当我尝试登录应用程序时,问题出现了,登录失败。我在运行时调试了spring security源代码,我认为问题是从HTML表单发送的CSRF令牌和CsrfFilter中的tokenRepository不是匹配,结果
“ http://localhost:8081/login发现无效的 CSRF 令牌 ”
并抛出 MissingCsrfTokenException(CsrfFilter 中的 doFilterInternal)。
以下是我的配置:
WebSecurityConfig.java
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean("authenticationManager")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Autowired
private CustomAuthProvider customAuthProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthProvider);
}
@Bean
public CustomAuthenticationFailureHandler authenticationFailureHandler() {
return new CustomAuthenticationFailureHandler(); …Run Code Online (Sandbox Code Playgroud)