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?
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.
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.
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.
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.
It's ExceptionTranslationFilter
that handles exceptions thrown by the security interceptors and provides suitable HTTP responses:
The
ExceptionTranslationFilter
sits above theFilterSecurityInterceptor
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With