I have a spring boot application which i secured with spring security. now I want to protect it from CSRF vulnerability, so I added this line to my spring security configuration :
http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
For the client side, I'am using angularJS 1.6. I added these two lines to my app.js file :
$httpProvider.defaults.xsrfCookieName = 'XSRF-TOKEN';
$httpProvider.defaults.xsrfHeaderName = 'X-XSRF-TOKEN';
Now I'm getting unauthorised status when i try to do some request
I think i'am missing something, anyone could help me ?
---- More details : here is my spring security configuration :
@Override
protected void configure(HttpSecurity http) throws Exception {
System.out.println("Setting up Security configuration");
http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()/*.disable()*/
.addFilterBefore(new AlreadyConnectedFilter(),UsernamePasswordAuthenticationFilter.class)
.authenticationProvider(authProvider)
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.formLogin()
.permitAll()
.loginProcessingUrl("/login")
.usernameParameter("username")
.passwordParameter("password")
.successHandler(authSuccessHandler)
.failureHandler(authFailureHandler)
.and()
.logout()
.permitAll()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout", "DELETE"))
.logoutSuccessHandler(logoutSuccessHandler)
.and()
.sessionManagement()
.maximumSessions(-1)
.expiredSessionStrategy(new CustomSessionInformationExpiredStrategy())
.sessionRegistry(sessionRegistry());
http.httpBasic().and().authorizeRequests()
.anyRequest().permitAll();
http.exceptionHandling()
.accessDeniedHandler((request, response, accessDeniedException) -> {
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
Map<String, Object> contentToSend = new HashMap<>();
contentToSend.put("message", accessDeniedException.getMessage());
contentToSend.put("errors",new ArrayList<>());
contentToSend.put("status",response.getStatus());
PrintWriter writer = response.getWriter();
writer.write(new ObjectMapper().writeValueAsString(contentToSend));
writer.flush();
})
.authenticationEntryPoint((request, response, authException) -> {
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
Map<String, Object> contentToSend = new HashMap<>();
contentToSend.put("message", authException.getMessage());
contentToSend.put("errors", new ArrayList<>());
contentToSend.put("status", response.getStatus());
PrintWriter writer = response.getWriter();
writer.write(new ObjectMapper().writeValueAsString(contentToSend));
writer.flush();
});
}