I'm trying to throw a custom exception in WebFlux during authentication, and handle it with a ControllerAdvice (@ExceptionHandler). Unfortunately, it doesn't get propagated, I'm getting either HTTP 500 if I throw the exception, or HTTP 401 if I return the exception as Mono.error()
@Override //in authentication service
public Mono<UserDetails> findByUsername(String username) {
//find user by username from database,
//if not enabled, throw a custom exception,
//if doesn't exist, throw UsernameNotFoundException,
//return org.springframework.security.core.userdetails.User otherwise.
}
@ExceptionHandler //in controller advice
public Mono<HttpStatus> handleException(MyCustomExceptionThrownFromFindByUsername ex) {
//implemented
}
Is there any way to help the exception to make it to the ExceptionHandler?
UserDetailswhich hasisEnabled... There is a constructor that takes a 4 additional booleans to indicate different user states. Default for all of them istrue. Use the proper constructor and let Spring Security handle the rest.Also regardless of the error Spring Security will issue 401. You don't want to provide more details from a security perspective... (If a hacker does a brute force and gets more information like account is disabled he knows that it is an existing account). - M. Deinum