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

Filter vs. Interceptor #37

Open
SuyeonChoi opened this issue Nov 13, 2022 · 0 comments
Open

Filter vs. Interceptor #37

SuyeonChoi opened this issue Nov 13, 2022 · 0 comments
Labels

Comments

@SuyeonChoi
Copy link
Owner

SuyeonChoi commented Nov 13, 2022

테코톡 블로그 정리: [10분 테코톡] 조시, 쿤의 서블릿 필터 & 스프링 인터셉터 정리

Filter

  • DispatcherServlet 요청 전/후에 부가작업 처리
  • Spring과 무관. 에러는 ErrorController에서 처리
  • 보안, 문자열 인코딩, 로깅 및 감사, 데이터 변환 등에 사용
  • 제공하는 메서드
    • init() : 필터 생성될 때 단 한번 호출
    • doFilter() : 요청이 들어올때마다 호출. 부가작업 처리
    • destroy() : 필터가 소멸될 때 단 한번 호출

Interceptor

  • DispatcherServlet이 핸들러(주로 컨트롤러)를 호출하기 전/후에 요청에 대한 부가작업 처리
  • Spring에서 제공하는 기술. 스프링 컨텍스트에서 동작
    • 예외를 @ControllerAdvice가 처리 가능
  • 제공하는 메서드
    • preHandle() : Handler 실행 전 수행되는 메서드. 부가 로직 처리 가능
    • postHandle() : Handler 실행 후 수행되는 메서드. ModelAndView를 통해 추가적인 작업 가능. 최근에는 JSON 형태로 데이터를 제공하는 REST API 기반의 컨트롤러가 사용되면서 잘 사용하지 않음
    • afterCompletion() : Handler 실행 후 수행되는 메서드. Exception에 대해 추가적인 작업 가능. 리소스 정리(반환) 가능

AOP와 차이

파라미터. 필터와 인터셉터는 HttpServletRequest, HttpServletResponse를 파라미터로 사용한다. 이에 비해 AOP를 적용할 때는 메서드와 파라미터 타입이 제각각이므로 이에 대한 부가 작업이 필요하다.


Ref.
[BackTony 블로그] 면접 시리즈2-Spring, Java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant