最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Spring boot Global exception handler(ResponseEntityExceptionHandler) is not invoking Authentication failure exceptions - Stack O

programmeradmin2浏览0评论

I have written a exception class as below:

@ResponseStatus(HttpStatus.UNAUTHORIZED)
public class AuthenticationException extends  RuntimeException {

    String exception;
    public AuthenticationException(String exception) {
        super(exception);
        this.exception = exception;

    }
}

then global exception handler class:

@ControllerAdvice
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
 @ExceptionHandler(AuthenticationException.class)
    public ResponseEntity<ErrorResponse> handleAuthenticationException(AuthenticationException ex, HttpServletResponse response){
        ErrorDetails errorDetails = new ErrorDetails(HttpStatus.UNAUTHORIZED.toString(),
                HttpStatus.UNAUTHORIZED.value(), ex.getMessage(),
                ex.getMessage(),
                new Date(), null);
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        return new ResponseEntity<>(new ErrorResponse(List.of(errorDetails)), HttpStatus.UNAUTHORIZED);
    }
    }

then the class Auth entrypoint:

@Component
public class AuthEntryPoint  implements AuthenticationEntryPoint {

    @Autowired
    @Qualifier("handlerExceptionResolver")
    private HandlerExceptionResolver resolver;
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        resolver.resolveException(request, response, null, authException);
    }
}

However, when bearer token is invalid.. it did not invoke the global exception (CustomizedResponseEntityExceptionHandler) class for the authentication failure.If anyone has any idea why it is not invoking int he above code or anything i am missing here

I have tried with the Handle spring security authentication exceptions with @ExceptionHandler But it could not resolve the issue.

I am expecting my custom exception should be invoked when Authentication (bearer token is not valid/null) failed, Response should be some custom message with status code. I am using spring boot 3.2.7

I have written a exception class as below:

@ResponseStatus(HttpStatus.UNAUTHORIZED)
public class AuthenticationException extends  RuntimeException {

    String exception;
    public AuthenticationException(String exception) {
        super(exception);
        this.exception = exception;

    }
}

then global exception handler class:

@ControllerAdvice
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
 @ExceptionHandler(AuthenticationException.class)
    public ResponseEntity<ErrorResponse> handleAuthenticationException(AuthenticationException ex, HttpServletResponse response){
        ErrorDetails errorDetails = new ErrorDetails(HttpStatus.UNAUTHORIZED.toString(),
                HttpStatus.UNAUTHORIZED.value(), ex.getMessage(),
                ex.getMessage(),
                new Date(), null);
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        return new ResponseEntity<>(new ErrorResponse(List.of(errorDetails)), HttpStatus.UNAUTHORIZED);
    }
    }

then the class Auth entrypoint:

@Component
public class AuthEntryPoint  implements AuthenticationEntryPoint {

    @Autowired
    @Qualifier("handlerExceptionResolver")
    private HandlerExceptionResolver resolver;
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        resolver.resolveException(request, response, null, authException);
    }
}

However, when bearer token is invalid.. it did not invoke the global exception (CustomizedResponseEntityExceptionHandler) class for the authentication failure.If anyone has any idea why it is not invoking int he above code or anything i am missing here

I have tried with the Handle spring security authentication exceptions with @ExceptionHandler But it could not resolve the issue.

I am expecting my custom exception should be invoked when Authentication (bearer token is not valid/null) failed, Response should be some custom message with status code. I am using spring boot 3.2.7

Share Improve this question asked Feb 6 at 8:28 ArunArun 13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

JWT token was wrong, then your custom exception did not reach. It is correct behavior.

If you really want something reach your custom exception, first thing first, JWT token is correct, your you set end-point is public endpoint, exception happens when it go to inside public endpoint.

In case, you want JWT token wrong, then go to inside JWTErrorBlablaException, it is not g suitable behavior. Correct behavior: return HTTP code 403 for bidden , or you have not permission error message. It is HTTP code message, not custom exception.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论