Spring 异常处理 - @ControllerAdvice 无法处理 HttpServletResponse#sendError()

use*_*858 5 java spring spring-mvc

我正在使用@ControllerAdvice来实现一个全局异常处理程序,但我在使用HttpServletResponse#sendError()方法时遇到了一些问题。 @ExceptionHandler可以捕获各种异常,但不能捕获HttpServletResponse#sendError()调用。我知道这HttpServletResponse#sendError()不是例外,但我需要处理它,然后重定向到通用错误页面。

我使用 Spring Security 进行身份验证,在失败的处理程序中,我将状态设置401为响应:

@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {

    String contentType = request.getContentType();
    logger.info(contentType);
    response.sendError( HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized" );      
}
Run Code Online (Sandbox Code Playgroud)

然后在@ControllerAdvice,我尝试使用@ExceptionHandler@ResponseStatus捕获401但它不起作用:

@ResponseStatus (value=HttpStatus.UNAUTHORIZED, reason="You don't have access right on this page")//401
@ResponseBody
@ExceptionHandler(DataIntegrityViolationException.class)
public String handleHttpStatus(DataIntegrityViolationException e){
    return "genericerror";
}
Run Code Online (Sandbox Code Playgroud)

@ExceptionHandler方法可以处理HttpServletResponse#sendError()调用吗?

Ale*_*xey 1

Spring Security 是一个独立于 Spring MVC 的框架。Spring Security 是一个 Servlet 过滤器(请查看您的web.xml),它会在请求到达 Spring MVC 之前拦截请求。您无法处理在 Spring Security 级别发生的异常@ControllerAdvice(作为@ControllerAdviceSpring MVC 的一部分)。

正如您自己所说,HttpServletResponse#sendError()不会引发异常。根据这份文件,它sends an error response to the client using the specified status code and clears the buffer.

在您的web.xml文件中,您可以定义静态网页 (1) 错误响应代码和 (2) 异常。例如:

<error-page>
    <error-code>401</error-code>
    <location>/errors/genericerror</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)