异常处理程序中的HandlerExceptionResolver不起作用

问题描述 投票:0回答:1

感谢您的宝贵时间。 我使用Spring boot 3.4.0,我想在过滤器中抛出异常,但它不起作用......

根据断点和日志,一切都是正确的,但我的响应正文是空的。

过滤器:

@Component
public class JwtAuthenticationFilter extends OncePerRequestFilter {

    @Autowired
    @Qualifier("handlerExceptionResolver")
    private HandlerExceptionResolver resolver;

    @Override
    protected void doFilterInternal(
            HttpServletRequest request, 
            HttpServletResponse response, 
            FilterChain filterChain) throws ServletException, IOException {
        
        String token = request.getHeader("Authorization");
        
        try {
            
            if (token.startsWith("Basic ")) {

                String codeId = request.getParameter("codeId");
                String text = request.getParameter("text");

                CaptchaCode captchaCode = new CaptchaCode(codeId, text, null);

                captchaCodeApplicationService.isCoptchaCodeValid(captchaCode);

            }
            ...
        
        } catch (ValidationException e) {
            resolver.resolveException(request, response, null, e);
        }
        
    }

}

建议:

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ValidationException.class)
    public ResponseEntity<?> handlerValidationException(ValidationException e, WebRequest request) {
        
        ExceptionDetails details = new ExceptionDetails(
                LocalDateTime.now(),
                HttpStatus.BAD_REQUEST.name(),
                request.getDescription(false),
                e.getMessage()
                );
        
        return new ResponseEntity<>(details, HttpStatus.BAD_REQUEST);
    }
    
}

以及抛出异常的方法:

public boolean isCoptchaCodeValid(CaptchaCode captchaCode) throws ValidationException {
    ...
}

回应: response next photo from the response

spring-boot filter spring-security
1个回答
0
投票

我用下面的代码快速测试,给了我这个主体:

{"now":"2024-12-20T14:53:02.339526733","name":"BAD_REQUEST","description":"uri=/backend-spring-boot/graphiql","message":"Invalid token"}

异常详情

import java.time.LocalDateTime;

public record ExceptionDetails(
        LocalDateTime now,
        String name,
        String description,
        String message) {
}

JwtAuthenticationFilter

import jakarta.servlet.FilterChain;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.HandlerExceptionResolver;

@Component
public class JwtAuthenticationFilter extends OncePerRequestFilter {

    @Autowired
    @Qualifier("handlerExceptionResolver")
    private HandlerExceptionResolver resolver;

    @Override
    protected void doFilterInternal(
            @NonNull HttpServletRequest request,
            @NonNull HttpServletResponse response,
            @NonNull FilterChain filterChain) {

        try {
            throw new ValidationException("Invalid token");
        } catch (ValidationException e) {
            resolver.resolveException(request, response, null, e);
        }
    }
}

验证异常

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.BAD_REQUEST)
public class ValidationException extends RuntimeException {
    public ValidationException(String message) {
        super(message);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.