小编ari*_*gar的帖子

将 cookie 设置为安全时 Spring Boot 无法登录

我正在开发一个使用默认 Thymeleaf 并使用 REST API 访问数据库的 Spring Boot 项目。一切正常,直到渗透测试结果要求我将 cookie 设置为安全标记。我已阅读这些链接:

  1. 在 spring 中自动向 JSESSIONID cookie 添加安全标志
  2. https://javadeveloperzone.com/spring-boot/spring-boot-secure-session-cookies/

我已经尝试过这两种方法,安全标志存在。

安全标志 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)

cookies spring csrf spring-security spring-boot

2
推荐指数
1
解决办法
8332
查看次数

标签 统计

cookies ×1

csrf ×1

spring ×1

spring-boot ×1

spring-security ×1