From f6694ca2894e46af9c8e610f14994f3807a12da9 Mon Sep 17 00:00:00 2001 From: Trond Magnus Sevre Date: Mon, 2 Dec 2024 15:22:45 +0100 Subject: [PATCH] Log PreAuthenticatedCredentialsNotFoundException --- .../controller/GlobalExceptionHandler.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/no/fint/portal/customer/controller/GlobalExceptionHandler.java b/src/main/java/no/fint/portal/customer/controller/GlobalExceptionHandler.java index 5fa48f2..043a2ad 100644 --- a/src/main/java/no/fint/portal/customer/controller/GlobalExceptionHandler.java +++ b/src/main/java/no/fint/portal/customer/controller/GlobalExceptionHandler.java @@ -4,6 +4,7 @@ import no.fint.portal.model.ErrorResponse; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.security.web.authentication.preauth.PreAuthenticatedCredentialsNotFoundException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; @@ -15,14 +16,10 @@ @ControllerAdvice public class GlobalExceptionHandler { - @ExceptionHandler(Exception.class) - public ResponseEntity handleGlobalException(Exception ex, WebRequest request, HttpServletRequest httpRequest) { - - log.error("Unhandled exception on call to: {}", httpRequest.getRequestURL()); - log.error("Unhandled exception", ex); - - ErrorResponse errorResponse = new ErrorResponse("Internal Server Error! An unexpected error occurred. Please try again later."); - return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); + @ExceptionHandler(PreAuthenticatedCredentialsNotFoundException.class) + public ResponseEntity handlePreAuthenticatedCredentialsNotFoundException(PreAuthenticatedCredentialsNotFoundException ex, HttpServletRequest httpRequest) { + log.error("Missing pre-authenticated credentials: {} on request to: {}", ex.getMessage(), httpRequest.getRequestURL()); + return new ResponseEntity<>(new ErrorResponse("Authentication credentials were not found."), HttpStatus.UNAUTHORIZED); } @ExceptionHandler(NoHandlerFoundException.class) @@ -32,4 +29,14 @@ public ResponseEntity handleNoHandlerFoundException(NoHandlerFoun ErrorResponse errorResponse = new ErrorResponse("Internal Server Error! An unexpected error occurred. Please try again later. (2)"); return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleGlobalException(Exception ex, WebRequest request, HttpServletRequest httpRequest) { + + log.error("Unhandled exception on call to: {}", httpRequest.getRequestURL()); + log.error("Unhandled exception", ex); + + ErrorResponse errorResponse = new ErrorResponse("Internal Server Error! An unexpected error occurred. Please try again later."); + return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); + } }