首页 文章

AngularJS SpringBoot没有认证csrf保护

提问于
浏览
0

我的系统使用AngularJS 1.6和Spring Boot2.0 . 我的前端只是一个客户可以用来购买活动门票的简单表格 . 当页面加载时,它将获取当前活动事件的详细信息,然后用户可以填写要发布的表单 . 没有登录/注册,所以它就像一个谷歌表格 . 我已将它们构建为单独的项目,因此它们将在不同的端口中运行 . 现在我想启用csrf保护,但我似乎无法使它工作 .

我尝试按照本教程但没有身份验证部分:https://spring.io/blog/2015/01/12/the-login-page-angular-js-and-spring-security-part-ii . 当我这样做时,我在GET事件详细信息部分遇到了"CORS header ‘Access-Control-Allow-Origin’ missing",所以我添加了一个CORS过滤器:

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
chain.doFilter(request, response);

添加之后,我的GET可以工作,但是当我发布客户的详细信息时,我遇到了同样的CORS错误 . 当我使用邮递员时,我已经可以在cookie中看到XSRF-TOKEN,但不知怎的,我的后端会阻止传入的POST . 根据我的理解,angular会自动使用收到的XSRF令牌,因此在实现spring security时我没有修改前端的任何内容 .

我也试过这个:https://sdqali.in/blog/2016/07/20/csrf-protection-with-spring-security-and-angular-js/ . 并且完全相同的结果,如果我只是按照教程,GET上的CORS错误然后当我添加简单的CORS过滤器,POST上的CORS错误 . 我的过滤器似乎在运行时混淆了 .

我已尝试在这些教程中使用这些代码,并在堆栈中对相关问题进行一些回答,但所有这些都不必处理CORS问题 .

1 回答

  • 0

    首先,您应该查看我对如何使用spring-boot和spring-security启用CORS的评论 .

    至于CSRF保护,具有 spring 安全性的spring-boot 2直接启用 . 但是,要将它与AngularJS一起使用,您需要遵循以下指南:https://docs.spring.io/spring-security/site/docs/current/reference/html5/#csrf-cookie

    我们还在开发一个带有前端AngularJS 1.6和后端spring-boot 2的项目,但是,由于我们没有将我们的前端和后端部署在同一个URL上,我们遇到了一个问题 .

    例:

    • https://project.company.com/frontend

    • https://project.company.com/backend

    服务器生成的cookie在域 https://project.company.com 上设置了上下文 /backend ,这是我们在AngularJS中的前端应用程序无法读取的 .

    为了解决这个问题,我们强制我们的服务器发送一个带有上下文 / 的cookie,以便可以从两个应用程序中读取它 .

    @Configuration
    @EnableWebSecurity
    public class LdapAuthenticationSecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            final CookieCsrfTokenRepository csrfTokenRepository = CookieCsrfTokenRepository.withHttpOnlyFalse();
            csrfTokenRepository.setCookiePath("/"); // Make sure that the cookie can be read from the backend and the frontend on the same domain.
            http.csrf().csrfTokenRepository(csrfTokenRepository);
            http.cors();
        }
    }
    

    如果您的应用程序部署在不同的域上,但具有相同的基础,如下所示:

    • https://frontend.company.com

    • https://backend.company.com

    我建议强制服务器使用像 .company.com 这样的域发送cookie . 但要做到这一点,您需要修改spring-security管理cookie创建的方式 .

相关问题