【问题标题】:Springboot is not throwing the error to the clientSpring Boot 不会向客户端抛出错误
【发布时间】:2022-01-30 15:02:54
【问题描述】:

我希望当遇到错误时,用户会收到对他的 http 调用的异常响应。 错误确实被捕获,显示在 spring 日志中,但没有返回给用户。 为什么?

我的方法:

  @Override
  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
      FilterChain chain) {
    try {
      // Check for authorization header existence.
      String header = request.getHeader(JwtConstant.AUTHORIZATION_HEADER_STRING);
      if (header == null || !header.startsWith(JwtConstant.TOKEN_BEARER_PREFIX)) {
        chain.doFilter(request, response);
        return;
      }
      // Validate request..
      UsernamePasswordAuthenticationToken authorization = authorizeRequest(request);
      SecurityContextHolder.getContext().setAuthentication(authorization);
      chain.doFilter(request, response);
    } catch (Exception e) {
      SecurityContextHolder.clearContext();
      throw new InternalServerErrorException("Erreur sur le filtre interne -> " + e.toString());  // Should return this to the user
    }
  }

日志中的错误:

.common.application.exception.InternalServerErrorException: Erreur sur le filtre interne -> 
.common.application.exception.InternalServerErrorException: Erreur lors du authorizeRequest

错误处理程序:

@ControllerAdvice
public class ApiResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
    String typeDateFormat = "yyyy-MM-dd HH:mm:ss";
...

    @ExceptionHandler(InternalServerErrorException.class)
    public final ResponseEntity<ExceptionResponse> handleInternalServerErrorException(InternalServerErrorException ex,
            WebRequest request) {

        ExceptionResponse exceptionResponse = new ExceptionResponse(
                new SimpleDateFormat(typeDateFormat).format(new Date()), ex.getMessage(),
                request.getDescription(false), "500");
        return new ResponseEntity<>(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

InternalServerErrorException.java

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public class InternalServerErrorException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public InternalServerErrorException(String exception) {
        super(exception);
    }

}

ExceptionResponse.java

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ExceptionResponse {
    
  private String date;
  private String message;
  private String uri;
  private String status;
  
}

用户收到 403.. 我不知道为什么,但应该收到我抛出的错误,没有任何正文..

InternalServerException 在 try/catch 之外起作用..

【问题讨论】:

    标签: java spring spring-boot spring-mvc spring-security


    【解决方案1】:

    基本上@ControllerAdvice只有在@Controller/@RestContoller方法抛出异常时才会起作用。

    从技术上讲,spring-mvc 框架的东西(即@ControllerAdvice)只有在可以成功通过SecurityFilterChain 中的所有Filter (由spring-security 配置)和由DispatcherServlet 处理。但是现在看来您的异常是从SecurityFilterChain 中的Filter 之一引发的 在请求到达 DispatcherServlet 之前,因此不会调用 @ControllerAdvice 来处理此异常。

    您必须咨询该 spring-security 过滤器中是否有相关配置允许您自定义异常。如果没有,您仍然可以在该过滤器中手动执行此操作,因为您可以从那里访问 HttpServletResponse

    【讨论】:

    • 是的,我可以访问 serverlet 响应,但是这样做:} catch (Exception e) { SecurityContextHolder.clearContext(); response.sendError(500, e.getMessage());返回; } 不会将错误返回给客户端
    猜你喜欢
    • 2010-12-24
    • 2016-04-15
    • 1970-01-01
    • 2020-08-02
    • 2019-05-16
    • 2017-12-13
    • 2015-08-26
    • 2022-11-28
    • 1970-01-01
    相关资源
    最近更新 更多