【问题标题】:Invalid CSRF Token in POST requestPOST 请求中的 CSRF 令牌无效
【发布时间】:2016-08-21 01:32:52
【问题描述】:

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

问题
问题是当任何 POST 请求提交到服务器时,都会引发“Invalid CSRF Token”异常。抛出异常的示例如下:

{
  "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"
}

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


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

【问题讨论】:

    标签: angularjs spring security post csrf


    【解决方案1】:

    查看该项目的安全配置,您会注意到在每个请求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 请求并打开cookies 标签。在那里你应该注意到一个名为 XSRF-TOKEN 的 cookie。
    • 获取该 cookie 的值并将其放入 X-XSRF-TOKEN 标头并执行 POST /test 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 2019-01-01
      • 2020-02-19
      • 2015-12-20
      • 2019-12-22
      • 2017-11-29
      相关资源
      最近更新 更多