【发布时间】:2017-03-07 05:20:20
【问题描述】:
在我的 Spring Boot/AngularJS 应用程序中,我有以下 CSRF- 配置:
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.csrf().csrfTokenRepository(csrfTokenRepository()); http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
final String[] restEndpointsToSecure = WebSecurityConfig.restEndpointsToSecure;
for (final String endpoint : restEndpointsToSecure) {
http.authorizeRequests().antMatchers("/" + endpoint + "/**").hasRole(UserRoleEnum.USER.toString());
}
http.addFilterAfter(csrfTokenResponseHeaderBindingFilter(), CsrfFilter.class);
xAuthTokenConfigurer.setDetailsService(userDetailsServiceBean());
final SecurityConfigurer<DefaultSecurityFilterChain, HttpSecurity> securityConfigurerAdapter = xAuthTokenConfigurer;
http.apply(securityConfigurerAdapter);
}
CSRF- Token Filter 如下所示:
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, javax.servlet.FilterChain filterChain)
throws ServletException, IOException {
final CsrfToken csrf = (CsrfToken)request.getAttribute(CsrfToken.class.getName());
if (csrf != null) {
Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
final String token = csrf.getToken();
if (cookie == null || token != null && !token.equals(cookie.getValue())) {
cookie = new Cookie("XSRF-TOKEN", token);
cookie.setPath("/");
response.addCookie(cookie);
}
}
filterChain.doFilter(request, response);
}
通常它工作正常,请求头属性 X-XSRF-TOKEN 随每个请求一起发送。但我有一个奇怪的行为。 我将在应用程序中更新我的用户配置文件。第一次工作正常,第二次,我得到一个 HTTP 403 Forbidden,实际上我真的不知道为什么。 我在这两个更新之间什么都不做(在这两个更新之间没有导航到其他页面或其他)。
在底部的图片中,左边的请求有效,右边的请求失败。唯一不同的是,右侧的属性 Set-Cookie 和 X-Application-context 在响应标头中丢失。请求头是相等的。
有谁知道我在这里做错了什么。这对我来说有点神秘。
【问题讨论】:
-
你能发布你的角度代码吗?特别是您的角度路线代码。
-
这是对任何资源的任何第二次尝试的常见行为还是仅用于配置文件更新?
标签: angularjs spring-boot csrf csrf-protection