Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle spring security InternalAuthenticationServiceException thrown in Spring ProviderManager

ProviderManager is throwing InternalAuthenticationServiceException.class while retrieving users in DaoAuthenticationProvider.class,

 loadedUser = this.getUserDetailsService().loadUserByUsername(username);

I want to handle this exception and return my custom response to the client.

I don't want to handle this by writing custom ProviderManager.

For all other OAuth exceptions i am able to handle the exceptions using Custom WebResponseExceptionTranslator.

But I am unable to catch security exceptions like InternalAuthenticationServiceException.class.

I don't have option to use ErrorController with the /error path, it is breaking other flows.

like image 667
Harish Avatar asked Sep 30 '16 10:09

Harish


People also ask

Can we use Spring Security in Spring MVC?

To enable Spring Security integration with Spring MVC add the @EnableWebSecurity annotation to your configuration. Spring Security provides the configuration using Spring MVC's WebMvcConfigurer.

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 do you handle security in spring boot?

For adding a Spring Boot Security to your Spring Boot application, we need to add the Spring Boot Starter Security dependency in our build configuration file. Maven users can add the following dependency in the pom. xml file. Gradle users can add the following dependency in the build.


1 Answers

You can write a class which is annotated with @ControllerAdvice and have a @ExceptionHandler(value=InternalAuthenticationServiceException.class).

Ex:-

@ControllerAdvice
public class ExceptionHandler {

    @ExceptionHandler(InternalAuthenticationServiceException.class)
    public ResponseEntity<String> handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) {
        ResponseEntity<String> response = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        return response;
    }

}

UPDATE

If you don't have controllers and using @EnableAuthorizationServer then you need to extend from AuthorizationServerConfigurerAdapter and override configure(AuthorizationServerEndpointsConfigurer endpoints) as below. You can use AuthorizationServerEndpointsConfigurer.exceptionTranslator to handle your InternalAuthenticationServiceException.

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                // other endpoints
                .exceptionTranslator(e -> {
                    if (e instanceof InternalAuthenticationServiceException) {
                        InternalAuthenticationServiceException internalAuthenticationServiceException = (InternalAuthenticationServiceException) e;

                        // return a ResponseEntity or throw a custom Exception.
                    } 
                });
    }
like image 117
shazin Avatar answered Oct 11 '22 01:10

shazin