본문 바로가기
  • Adillete
【Spring】

[Interceptor]

by 아딜렛 2025. 9. 15.

오류를 해결할때 서버의 오류 내용을 읽고 프론트에서 나오는 에러를 검사-> 네트워크에서 확인한다.

@Slf4j
@Component
public class LoginCheckInterceptor implements HandlerInterceptor{

  @Autowired
  private JwtService jwtService;
  
  //전처리(컨트롤러 실행 전)
  @Override 
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    //log.info("전처리 싫앵");

    if("OPTIONS".equals(request.getMethod())){
      return true;
    }

    if(!(handler instanceof HandlerMethod)){
      return true;
    }



    //1.요청 매핑 메소드에 @Login 이 붙어 있는지 확인한다.
    HandlerMethod handlerMethod = (HandlerMethod) handler;

오류문 

Caused by: java.lang.ClassCastException: class org.springframework.web.servlet.handler.AbstractHandlerMapping$PreFlightHttpRequestHandler cannot be cast to class org.springframework.web.method.HandlerMethod (org.springframework.web.servlet.handler.AbstractHandlerMapping$PreFlightHttpRequestHandler and org.springframework.web.method.HandlerMethod are in unnamed module of loader 'app')
        at com.example.demo.interceptor.LoginCheckInterceptor.preHandle(LoginCheckInterceptor.java:26)
        at org.springframework.web.servlet.HandlerExecutionChain.applyPreHandle(HandlerExecutionChain.java:146)

 

실행 순서

브라우저 -> options 요청을 전송

Spring dispatcherServelt 이 요청을 받고

cors config 적용 option 허용

⭐인터셉터 실행(prehandle 호출) 여기에서 에러 발생

실제 핸들러 실행

cors 헤더 응답에 추가

인터셉터는 허용된 oprtions 요청도 가로챌수 있다.