Obsługuj wyjątki uwierzytelniania bezpieczeństwa wiosennego za pomocą @ExceptionHandler

Używam Spring MVC@ControllerAdvice i@ExceptionHandler obsługiwać wyjątek REST Api. Działa dobrze w przypadku wyjątków zgłaszanych przez kontrolery WWW mvc, ale nie działa w przypadku wyjątków generowanych przez niestandardowe filtry bezpieczeństwa zabezpieczeń, ponieważ działają one przed wywołaniem metod kontrolera.

Mam niestandardowy wiosenny filtr bezpieczeństwa, który wykonuje uwierzytelnianie oparte na tokenach:

public class AegisAuthenticationFilter extends GenericFilterBean {

...

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        try {

            ...         
        } catch(AuthenticationException authenticationException) {

            SecurityContextHolder.clearContext();
            authenticationEntryPoint.commence(request, response, authenticationException);

        }

    }

}

Z tym niestandardowym punktem wejścia:

@Component("restAuthenticationEntryPoint")
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint{

    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authenticationException.getMessage());
    }

}

Za pomocą tej klasy można obsługiwać wyjątki globalnie:

@ControllerAdvice
public class RestEntityResponseExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler({ InvalidTokenException.class, AuthenticationException.class })
    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public RestError handleAuthenticationException(Exception ex) {

        int errorCode = AegisErrorCode.GenericAuthenticationError;
        if(ex instanceof AegisException) {
            errorCode = ((AegisException)ex).getCode();
        }

        RestError re = new RestError(
            HttpStatus.UNAUTHORIZED,
            errorCode, 
            "...",
            ex.getMessage());

        return re;
    }
}

To, co muszę zrobić, to zwrócić szczegółowy korpus JSON nawet na wiosenne zabezpieczenia AuthenticationException. Czy istnieje sposób, aby wiosną bezpieczeństwa AuthenticationEntryPoint i wiosna mvc @ExceptionHandler współpracują?

Używam zabezpieczenia sprężynowego 3.1.4 i sprężynowego mvc 3.2.4.

questionAnswers(8)

yourAnswerToTheQuestion