Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Spring Security BadCredentialsException mapped to HTTP 401 code

When i throw a org.springframework.security.authentication.BadCredentialsException exception, in client it will display 401 as below,

{
  "timestamp": "2016-03-29T09:07:50.866+0000",
  "status": 401,
  "error": "Unauthorized",
  "message": "Some message",
  "path": "/test/service1/getAll"
}

I want to know where and how does the BadCredentialsException mapped to HTTP 401 Status code?

like image 984
Harshana Avatar asked Mar 29 '16 09:03

Harshana


People also ask

How does Spring Security authentication work internally?

The short answer: At its core, Spring Security is really just a bunch of servlet filters that help you add authentication and authorization to your web application. It also integrates well with frameworks like Spring Web MVC (or Spring Boot), as well as with standards like OAuth2 or SAML.

How does spring boot handle authentication exception?

Spring security exceptions can be directly handled by adding custom filters and constructing the response body. To handle these exceptions at a global level via @ExceptionHandler and @ControllerAdvice, we need a custom implementation of AuthenticationEntryPoint.

How security is achieved in spring?

We can configure spring security by editing web. xml or by extending the WebSecurityConfigurerAdapter implementation. In both the methods, we can define the providers for authentication and authorization and descriptions of application scopes that need authentication and/ or authorization.

How is Spring Security implemented?

Spring Security supports multiple ways to implement this type of authentication. The typical way to implement Remember Me authentication is by hashing the user details with a secret key that is on the server and encoding it along with the username and expiration time.


1 Answers

It's ExceptionTranslationFilter that handles exceptions thrown by the security interceptors and provides suitable HTTP responses:

The ExceptionTranslationFilter sits above the FilterSecurityInterceptor in the security filter stack. It doesn’t do any actual security enforcement itself, but handles exceptions thrown by the security interceptors and provides suitable and HTTP responses.

Check out the Spring Security documentation for more information here and here.

like image 96
Ali Dehghani Avatar answered Sep 27 '22 23:09

Ali Dehghani