title: Spring Security + AngularJS: POST request
body: <p>I have a web application with the back using Java Spring and the front using AngularJS.</p>
  
  <p><strong>Spring Security Configuration (Java config)</strong>:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Override
  protected void configure(HttpSecurity http) throws Exception {
          http
          .authenticationProvider(authenticationProvider())
          .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
          .and()
          .formLogin().loginProcessingUrl("loginProcessingUrl")
                      .successHandler(authSuccessHandler)
                      .failureHandler(authFailureHandler)
                      .usernameParameter("username")
                      .passwordParameter("password")
          .and()
          .logout().permitAll().logoutSuccessHandler(logoutSuccessHandler)
          .and()
          .authorizeRequests().antMatchers("/demo/**","/postTest/**").permitAll()
          .anyRequest().authenticated()
          .and()
          .httpBasic()
          .and()
          .csrf().csrfTokenRepository(csrfTokenRepository())
          .and()
          .addFilterAfter(new CsrfCustomFilter(), CsrfFilter.class);
      }
  </code></pre>
  
  <p><strong>CSRF filter</strong>:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class CsrfCustomFilter extends 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 &amp;&amp; !token.equals(cookie.getValue())) {
          cookie = new Cookie("XSRF-TOKEN", token);
          cookie.setPath("/");
          response.addCookie(cookie);
        }
      }
      filterChain.doFilter(request, response);
  }
  }
  </code></pre>
  
  <p><strong>POST request from AngularJS</strong>:</p>
  
  <pre class="lang-js prettyprint-override"><code>$http({method:'POST',data:"celine",url:"http://localhost:8080/postTest",cache:true})
          .success(function(data) {
            console.log("success POST");
          })
          .error(function(data, status, headers, config, statusText) {
            console.log("status: " + status + " statusText: " + statusText);
          });
  </code></pre>
  
  <p><strong>-- UPDATED --</strong><br>
  I can use a GET request from the front to the back, but the POST request doesn't seem to work. I get a <code>403 Forbidden</code> error on the front side (in my log: "status: 403 statusText: undefined").</p>
  
  <p>For the POST request, I can correctly send a XSRF-TOKEN to Spring, but Spring returns <code>403 Forbidden</code>. So I guess that the problem is coming from my configuration of Spring Security and not from Angular.</p>
  
  <p><strong>My headers for the POST request</strong>:
  <img src="https://i.stack.imgur.com/Cvqed.png" alt="enter image description here"></p>
  
  <p>Thank you for your time.</p>