POST请求中的CSRF令牌无效

时间:2016-04-27 00:30:19

标签: angularjs spring security post csrf

概述
我将使用API​​ Gateway作为基于Spring安全性的身份验证。我刚刚按照https://spring.io/guides/tutorials/spring-security-and-angular-js/链接中的步骤创建了一个基于https://github.com/spring-guides/tut-spring-security-and-angular-js.git的相应github项目的“pairs-double”模块的项目。

问题
问题在于,当任何POST请求被提交给服务器时,抛出“无效的CSRF令牌”异常。抛出异常的示例如下:

{
  "timestamp": 1461714933215,
  "status": 403,
  "error": "Forbidden",
  "message": "Invalid CSRF Token '1cdc44ad-43cb-44e6-b903-bec24fe903fd' was found on the request parameter '_csrf' or header 'X-XSRF-TOKEN'.",
  "path": "/ui/test"
}

我检查了一个重新检查的问题,但无济于事。我用postman测试了这个场景,并将'X-XSRF-TOKEN'设置为POST请求的标题,但什么也没发生。

所以,由于我是初学者使用Spring安全方法,如果有人能建议我解决方案,我将不胜感激。

1 个答案:

答案 0 :(得分:7)

查看该项目的安全配置,您会注意到每个请求using a filter中都添加了XSRF-TOKEN cookie。所以你需要做的就是获取该cookie的值并将其存储在X-XSRF-TOKEN标题中。我已经制作了一个具有类似安全配置的测试项目来测试这种情况,完整的代码如下所示:

@RestController
@SpringBootApplication
public class TestApplication extends WebSecurityConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/**")  // Disable authentication for all requests.
            .permitAll()
            .and()
            .csrf().csrfTokenRepository(csrfTokenRepository())
            .and()
            .addFilterAfter(csrfHeaderFilter(), SessionManagementFilter.class); // Register csrf filter.
    }

    private Filter csrfHeaderFilter() {
        return new OncePerRequestFilter() {

            @Override
            protected void doFilterInternal(HttpServletRequest request,
                                            HttpServletResponse response,
                                            FilterChain filterChain) throws ServletException, IOException {

                CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
                if (csrf != null) {
                    Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
                    String token = csrf.getToken();
                    if (cookie == null || token != null
                            && !token.equals(cookie.getValue())) {

                        // Token is being added to the XSRF-TOKEN cookie.
                        cookie = new Cookie("XSRF-TOKEN", token);
                        cookie.setPath("/");
                        response.addCookie(cookie);
                    }
                }
                filterChain.doFilter(request, response);
            }
        };
    }

    private CsrfTokenRepository csrfTokenRepository() {
        HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
        repository.setHeaderName("X-XSRF-TOKEN");
        return repository;
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String testGet() {
        return "hello";
    }

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String testPost() {
        return "works!";
    }
}

要与邮递员一起测试,请执行以下操作:

  • 启用interceptor以开始捕获Cookie。
  • 执行GET /test请求并打开Cookie标签。在那里你应该注意到一个名为XSRF-TOKEN的cookie。
  • 获取该Cookie的值并将其放入X-XSRF-TOKEN标头并执行POST /test请求。
相关问题