Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HandlerInterceptor #28

Open
NewWisdom opened this issue May 18, 2021 · 0 comments
Open

HandlerInterceptor #28

NewWisdom opened this issue May 18, 2021 · 0 comments
Labels
atdd-subway-path 지하철 경로조회 / 로그인

Comments

@NewWisdom
Copy link
Owner

NewWisdom commented May 18, 2021

ArgumentResolver와 Interceptor

ArgumentResolver

Strategy interface for resolving method parameters into argument values in the context of a given request.

  • 매개변수의 리졸빙
  • 컨트롤러에서 파라미터를 바인딩 해주는 역할

AuthInterceptor

Workflow interface that allows for customized handler execution chains. Applications can register any number of existing or custom interceptors for certain groups of handlers, to add common preprocessing behavior without needing to modify each handler implementation.
A HandlerInterceptor gets called before the appropriate HandlerAdapter triggers the execution of the handler itself. This mechanism can be used for a large field of preprocessing aspects, e.g. for authorization checks, or common handler behavior like locale or theme changes. Its main purpose is to allow for factoring out repetitive handler code.

  • 권한 확인 또는 로케일 또는 테마 변경과 같은 일반적인 핸들러 동작과 같은 전처리 측면의 넓은 분야에 사용
  • 인증 / 인가에 대한 검사
  • 주요 목적은 반복적 인 핸들러 코드를 제거하는 것
  • Spring doc에서 공식적으로 authorization checks를 하는 애라고 지정해준 존재
  • 공식문서

HandlerInterceptor의 메서드

preHandle(request, response, handler)

  • 컨트롤러에 요청을 보내기 전 작업을 수행하는 데 사용.
  • 반환 값이 true일 경우 정상적으로 진행이 되고, false일 경우 실행이 멈춘다.

postHandle(request, response, handler, modelAndView)

  • 컨트롤러의 동작 이후 클라이언트에 응답을 보내기 전에 작업을 수행하는 데 사용.
  • Dispatcher Servlet이 화면을 처리하기 전에 동작
  • 전달인자의 modelAndView을 통해 화면 단에 들어가는 데이터 등의 조작이 가능.

afterCompletion(request, reponse, handler, exception)

  • 요청 및 응답 완료 후 작업을 수행하는 데 사용.
  • Dispatcher Servlet의 화면 처리가 완료된 이후 동작

Servlet Filter란?

  • 객체의 형태로 존재
  • 클라이언트로부터 오는 요청(request)과 최종자원(서블릿/JSP/기타 문서) 사이에 위치하며 클라이언트의 요청 정보를 알맞게 변경할 수 있다.
  • 최종 자원과 클라이언트로 가는 응답(response) 사이에 위치하여 최종 자원의 요청 결과를 알맞게 변경할 수 있다.
  • 서블릿 실행 전, 후에 어떤 작업을 하고자 할때 사용

HandlerInterceptor VS Servlet Filter

image
Servlet Filter

  • Dispatcher Servlet의 앞단에서 들어오는 요청 정보를 처리.
  • J2EE의 표준 스펙에 정의가 되어있음.
  • 디스패처 서블릿 전에 호출이 되므로 Spring 프레임워크와 무관

Interceptor

  • Dispatcher Servlet의 뒤의 Handler 영역에서 요청 정보를 처리.
  • Spring 프레임워크에서 자체적으로 제공하는 기능.

흐름 순서

사용자 요청(Request) -> Servlet Filter -> Dispatcher Servlet -> Interceptor -> Controller

참고자료

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
atdd-subway-path 지하철 경로조회 / 로그인
Projects
None yet
Development

No branches or pull requests

1 participant