<aside> 1️⃣ 토큰 에러 발생시 error.html로 오류페이지 이동을 안함

</aside>

{
	"message": "OTHER TOKEN ERROR",
	"code": "9999",
	"status": 401
}
// doFilterInternal 메서드에서 예외 처리하는 부분
catch (Exception e) {
    // Token 내에 Exception이 발생 하였을 경우 => 클라이언트에 응답값을 반환하고 종료합니다.
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/json");
    PrintWriter printWriter = response.getWriter();
    JSONObject jsonObject = jsonResponseWrapper(e);
    e.printStackTrace();
    printWriter.print(jsonObject);
    printWriter.flush();
    printWriter.close();
}

/**
 * 토큰 관련 Exception 발생 시 예외 응답값을 구성하는 부분
 */
private JSONObject jsonResponseWrapper(Exception e) {

    String resultMessage = "";
    // JWT 토큰 만료
    if (e instanceof ExpiredJwtException) {
        resultMessage = "TOKEN Expired";
    }
    // JWT 허용된 토큰이 아님
    else if (e instanceof SignatureException) {
        resultMessage = "TOKEN SignatureException Login";
    }
    // JWT 토큰내에서 오류 발생 시
    else if (e instanceof JwtException) {
        resultMessage = "TOKEN Parsing JwtException";
    }
    // 이외 JTW 토큰내에서 오류 발생
    else {
        resultMessage = "OTHER TOKEN ERROR";
    }

    HashMap<String, Object> jsonMap = new HashMap<>();
    jsonMap.put("status", 401);
    jsonMap.put("code", "9999");
    jsonMap.put("message", resultMessage);
    jsonMap.put("reason", e.getMessage());
    JSONObject jsonObject = new JSONObject(jsonMap);
    logger.error(resultMessage, e);
    return jsonObject;
}
package com.jinan.profile.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

/**
 * 중앙 집중 예외처리를 위한 GlobalControllerAdvice 작성
 */
@Slf4j
@ControllerAdvice
public class GlobalControllerAdvice {

    /**
     * ProfileApplicationException 발생했을 때 호출되는 메서드
     * 에러 페이지를 보여주고, 에러 메시지를 모델에 추가한다.
     * <p th:text="${errorMessage}"></p> 이런식으로 타임리프에 에러페이지를 만들고 가져다 사용한다.
     */
    @ExceptionHandler(ProfileApplicationException.class)
    public ModelAndView handleProfileApplicationException(ProfileApplicationException e) {
        log.error("Error occurs {}", e.toString());

        // Spring MVC는 알아서 'error'라는 이름의 뷰를 찾아서 렌더링한다.
        ModelAndView mav = new ModelAndView("error"); // 에러 페이지의 뷰 이름
        mav.addObject("errorMessage", e.getErrorCode().getMessage()); // 에러 메시지를 모델에 추가
        return mav;
    }

    /**
     * RuntimeException이 발생했을 때 호출되는 메서드
     * 에러 페이지를 보여주고, 에러 메시지를 모델에 추가한다.
     */
    @ExceptionHandler(RuntimeException.class)
    public ModelAndView handleRuntimeException(RuntimeException e) {
        log.error("Error occurs {}", e.toString());

        // Spring MVC는 알아서 'error'라는 이름의 뷰를 찾아서 렌더링한다.
        ModelAndView mav = new ModelAndView("error"); // 에러 페이지의 뷰 이름
        mav.addObject("errorMessage", e.getMessage()); // 에러 메시지를 모델에 추가
        return mav;
    }
}

<aside> 2️⃣ 문제발생 이유

</aside>

<aside> 3️⃣ 1차 해결시도

</aside>

  1. JwtAuthorizationFilter 클래스의 try-catch문의 catch부분을 변경함
catch (Exception e) {
    // Token 내에 Exception이 발생 하였을 경우 => 클라이언트에 응답값을 반환하고 종료합니다.
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/json");
    PrintWriter printWriter = response.getWriter();
    JSONObject jsonObject = jsonResponseWrapper(e);
    e.printStackTrace();
    printWriter.print(jsonObject);
    printWriter.flush();
    printWriter.close();
}