在@ControllerAdvice课堂上我有一个@ExceptionHandler,这个处理程序可以很好地处理控制器错误,但是如果我有一个过滤器,它们就无法处理异常。我该如何处理这些异常?
过滤器是:-
public class AuthFilter extends UsernamePasswordAuthenticationFilter {
private LoginDTO loginDTO;
public AuthFilter() {
setRequiresAuthenticationRequestMatcher(
new AntPathRequestMatcher("/login", "POST"));
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request,
HttpServletResponse response) throws AuthenticationException {
try {
loginDTO = new ObjectMapper().readValue(request.getReader(), LoginDTO.class);
} catch (Exception e) {
throw new APIException(ExceptionMessages.INVALID_LOGIN_JSON,
HttpStatus.BAD_REQUEST);
}
return super.attemptAuthentication(request, response);
}
...
}
Run Code Online (Sandbox Code Playgroud)
异常处理程序是(在@ControllerAdvice 中)
@ExceptionHandler(APIException.class)
public ResponseEntity<ErrorDTO> handleAPIException(APIException e) {
return new ResponseEntity<ErrorDTO>(new ErrorDTO(e.getMessage()),
e.getHttpStatus());
}
Run Code Online (Sandbox Code Playgroud)
更新
我的要求是为 spring 安全过滤器提供一个全局异常处理程序。有什么办法吗?