@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);
}
...
}
的异常处理程序(在@ControllerAdvice)
@ExceptionHandler(APIException.class)
public ResponseEntity<ErrorDTO> handleAPIException(APIException e) {
return new ResponseEntity<ErrorDTO>(new ErrorDTO(e.getMessage()),
e.getHttpStatus());
}
UPDATE
我的要求是具有弹性安全领域的全球异常处理程序过滤器。有什么办法可以做到吗?
这些错误由Spring Security处理,而不是由@ ExceptionHandler处理,因为这些错误仅适用于控制器,并且在DispatcherServlet过滤器内执行,因此绝不会将'@ ExceptionHandler'移动到另一个过滤器,基本上将dispatcherservlet复制到过滤器)可以处理这个(也不应该是imho)。你也知道你的自定义过滤器有缺陷吗? –
那么是否有办法在全球范围内处理弹簧安全异常?不,不是,缺点是什么? – Kaushik
Spring Security已经处理了这些异常,那有什么问题?您有一个过滤器,并且对于每个请求,您将结果存储在一个类级别属性中,'loginDTO'现在您认为10个请求进来后会发生什么...... –