Interceptor의 역할은 요청을 받고, HandlerMapping에서 요청을 처리할 Controller을 찾으면
관련 내용을 컨트롤러에 전달하는 역할을 한다.
Interceptor는 스프링에서 관리하는 빈이고
Handler Mapping을 통해서 어느 컨트롤러에서 요청을 처리할지 정하면
요청에 대해서 인증, 권한 검사, 로깅, 예외처리와 같은 공통적인 작업을 처리한다.
이를 통해 공통 처리 로직을 중앙 집중적으로 모아서 관리함으로써
유지 보수에 용이해진다.
// 인터셉터를 통해 컨트롤러로 전달되는 정보를 컨트롤
// 핸들러 인터셉터는 Spring mvc 모델에서 http 요청을 처리하는 과정에서 특정 작업을 수행하도록 도와준다.
@Slf4j
@Component
public class OpenApiInterceptor implements HandlerInterceptor {
// 요청이 실제 컨트롤러에 전달되기 전에 실행
// 주로 인증, 권한 검사를 할 때 사용된다.
// true를 반환하면 컨트롤러로 요청이 전달된다.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//handler는 가야할 컨트롤러의 정보가 있다.
//
log.info("preHandle");
// return true이면 controller 전달 false 전달 x
var handlerMethod=(HandlerMethod)handler; // HandlerMethod 에서는 어노테이션과 클래스 레벨을 확인할 수 있다.
var methodLevel=handlerMethod.getMethodAnnotation(OpenApi.class); // method레벨에서 open aipi를 가지고 있는가?
if(methodLevel!=null){
log.info("method level");
return true;
}
var classLeve= handlerMethod.getBeanType().getAnnotation(OpenApi.class); //class에 open api가 달려있는가
if(classLeve!=null){
log.info("class level");
return true;
}
log.info("open api 아닙니다 : {}", request.getRequestURI());
return false;
}
// 컨트롤러가 요청을 처리한 후 DispatcherServlet 이 요청을 랜더링 하기 전에 호출된다.
// 요청에 대해 반환한 모델이나 뷰에 추가적인 수정 작업을 할 수 있다.
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.info("postHandle");
//HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
// 요청 처리가 완료된 후 즉 뷰가 랜더링되고 호출
// 요청 처리 중에 사용한 리소스 정리, 로그, 예외 처리
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.info("afterCompletion");
//HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
@OpenApi
@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserApiController {
@OpenApi
@PostMapping("")
public UserRequest register(
@RequestBody
UserRequest userRequest
//HttpEntity http
){
//log.info("{}", userRequest);
log.info("{}", userRequest);
return userRequest; //서버에서 클라이언트로 어떤 값을 내렸는지 정확히 알 수 없음
}
@GetMapping("/hello")
public void hello(){
log.info("hello");
}
}
외부에서 http 요청을 보내게 되면
1. preHandle메소드가 실행되고,
2. 해당 컨트롤러에 대해 메소드/ 클래스 레벨에서 권한이 있는 지 체크한다.
3. 요청을 처리한 다음
4. postHandle 메소드가 실행
5. afterCompletion 메소드가 실행된다.
'Springboot' 카테고리의 다른 글
[스프링 부트] AOP, Pointcut (0) | 2024.07.04 |
---|---|
[스프링 부트] Filter (0) | 2024.07.04 |
[스프링 부트] 페이지네이션 정보 응답 받기 (0) | 2024.07.01 |
[스프링 부트] Entity 매핑 무한루프 (0) | 2024.07.01 |
[스프링 부트] DispatcherSevlet의 역할 (0) | 2024.06.29 |