Home Ask Login Register

Developers Planet

Your answer is one click away!

billc.cn February 2016

Make @ExceptionHandler handle every exception except AccessDeniedException?

I wrote an @ExceptionHandler to send the exception in JSON for REST requests. However, due to the way Spring Security works, AccessDeniedExceptions must not be handled.

If I simply rethrow an AccessDeniedException in the handler, I get an ERROR level logging saying:

Failed to invoke @ExceptionHandler method: SomeMethod() throws java.lang.Exception
org.springframework.security.access.AccessDeniedException: Access is denied

I cannot safely disable ERROR level logging for the class logging it (ExceptionHandlerExceptionResolver), but having these stacktrace is quite confusing for operations people.

Is there anyway to make @ExceptionHandler not handle this specific exception?


Lemon February 2016

I think you could just use global exception handler instead, so you don't manually handle this AccessDeniedExceptions, this global exception will be thrown for every kinds of exceptions (except for the exception handlers that you handle manually):

public class GlobalHandler {

    public ModelAndView handleException() {
        return new ModelAndView("errorGlobal");

billc.cn February 2016

Issue raised: https://jira.spring.io/browse/SPR-13932

May get fixed in Spring 5.

Post Status

Asked in February 2016
Viewed 3,371 times
Voted 10
Answered 2 times


Leave an answer

Quote of the day: live life