AdminAuthInterceptor를 생성하여 userId, userRole을 request.getAttribute()로 추출IllegalStateException 발생userId, 요청 URI, 요청 시간 로그 기록@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
Long userId = (Long) request.getAttribute("userId");
String userRole = (String) request.getAttribute("userRole");
if (userId == null || userRole == null) {
throw new IllegalStateException("인증 정보가 없습니다.");
}
if (!"ADMIN".equals(userRole)) {
throw new IllegalStateException("어드민만 접근할 수 있습니다.");
}
log.info("어드민 API 접근 - userId: {}, URI: {}, time: {}", userId, request.getRequestURI(), LocalDateTime.now());
return true;
}
개선된 점
/admin/comments/** 경로에만 Interceptor 적용@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(adminAuthInterceptor)
.addPathPatterns("/admin/comments/**");
}
개선된 점
TestJwtUtil을 따로 만들어 테스트에서 사용할 수 있는 토큰 발급JwtUtil.createToken(userId, email, UserRole.ADMIN) 방식 활용mockMvc.perform(delete("/admin/comments/1")
.header("Authorization", token))
.andExpect(status().isOk());
개선된 점
인터셉터 공부하고 다시오겠습니다