// HandlerInterceptor 구현
public class LogInterceptor implements HandlerInterceptor{
// 전역 스태틱 상수
public static final String LOG_ID = "logId";
// preHandle 에서 컨트롤러 호출 직전에 랜덤 uuid, requestURI 출력
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String requestURI = request.getRequestURI();
String uuid = UUID.randomUUID().toString();
request.setAttribute(LOG_ID, uuid);
System.out.println("[interceptor] uuid : " + uuid);
System.out.println("[interceptor] requestURI : " + requestURI);
return true; // false로 전환 시 진행하지 않음
}
// 컨트롤러 호출 후 뷰, 모델 출력
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("[interceptor] postHandle : " + modelAndView);
}
// 뷰 렌더링까지 끝난 후 요청에 담았던 logId, requestURI 출력
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
String requestURI = request.getRequestURI();
String logId = (String)request.getAttribute(LOG_ID);
System.out.println("[interceptor] logId : " + logId);
System.out.println("[interceptor] requestURI : " + requestURI);
}
}