【发布时间】:2018-07-16 16:31:42
【问题描述】:
我想在为我的 springboot 应用程序进行集成测试时禁用 csrf。我已经尝试过 security.enable-csrf=false 但这似乎没有任何效果。我也尝试通过 SecurityMockMvcRequestPostProcessors.csrf 方法在我的测试中传递 csrf 令牌,但无济于事。
在我的应用程序中,有一个自定义的 SecurityConfig 扩展了 WebSecurityConfigurerAdapter 并具有如下代码:
http
.csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and()
.authorizeRequests()
.antMatchers(LOGIN_URL).permitAll()
.antMatchers("/api/v1/forgotpwd").permitAll()
.antMatchers("/api/v1/changepwd").permitAll()
.antMatchers("/api/v1/isLoggedIn").permitAll()
.antMatchers("/api/**").authenticated()
.and()
.logout().logoutUrl("/api/v1/logout").deleteCookies("JSESSIONID").invalidateHttpSession(true)
.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler())
.and()
.addFilter(jsonUsernamePasswordAuthenticationFilter())
.addFilterAfter(new MDCEmailSetterFilter(),JsonUsernamePasswordAuthenticationFilter.class);
如果我设置了 http.csrf().disable(),我的测试工作并且不会发生 csrf 令牌身份验证失败,否则它会抛出 403 并显示无法验证 csrf 令牌的消息。
有什么帮助吗?
【问题讨论】:
标签: spring spring-mvc spring-boot spring-security spring-boot-test