title: Spring Boot Security: How to run authentication filter before CSRF in Spring Boot?
body: <p>I am always getting unauthorize on Login. </p>
  
  <p>On Login i need to authenticate user as well as generate CSRF token based on JWT token generated from user credentials.</p>
  
  <p>I have CsrfCookieGeneratorFilter but i need to pass JWT generated after sucessfull authentication. My current code always execute CsrfFilterfirst and after that run authentication thing.</p>
  
  <p>So Authentication First and then CsrfCookieGeneraterFilter. </p>
  
  <p>Could anyone guide how to achieve using following builder (I am quite new to this Spring security thing implementation)</p>
  
  <p>Following is code i am trying:</p>
  
  <pre><code>httpSecurity
              .authorizeRequests().antMatchers("/login").permitAll()            
              .anyRequest().authenticated()
              .and()
              .csrf()     
              .requireCsrfProtectionMatcher(csrfRequestMatcher)
              .csrfTokenRepository(customCsrfTokenRepository)
              .and()            
              .cors()
              .and()
              .sessionManagement()
              .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
              .and()
              .exceptionHandling()
              .authenticationEntryPoint(this.jwtAuthenticationEntryPoint);
  
  
  
         httpSecurity.addFilterAt(new CsrfCookieGeneratorFilter(customCsrfTokenRepository), CsrfFilter.class);
         httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
  </code></pre>