/** * A filter is an object that performs filtering tasks on either the request to * a resource (a servlet or static content), or on the response from a resource, * or both. <br> * <br> * Filters perform filtering in the <code>doFilter</code> method. Every Filter * has access to a FilterConfig object from which it can obtain its * initialization parameters, a reference to the ServletContext which it can * use, for example, to load resources needed for filtering tasks. * <p> * Filters are configured in the deployment descriptor of a web application * <p> * Examples that have been identified for this design are<br> * 1) Authentication Filters <br> * 2) Logging and Auditing Filters <br> * 3) Image conversion Filters <br> * 4) Data compression Filters <br> * 5) Encryption Filters <br> * 6) Tokenizing Filters <br> * 7) Filters that trigger resource access events <br> * 8) XSL/T filters <br> * 9) Mime-type chain Filter <br> * * @since Servlet 2.3 */ publicinterfaceFilter{ publicdefaultvoidinit(FilterConfig filterConfig)throws ServletException {}
/** * Interception point before the execution of a handler. Called after HandlerMapping determined an appropriate handler object, but before HandlerAdapter invokes the handler */ defaultbooleanpreHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
returntrue; }
/** * Interception point after successful execution of a handler. Called after HandlerAdapter actually invoked the handler, but before the DispatcherServlet renders the view. */ defaultvoidpostHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView)throws Exception { }
/** * Callback after completion of request processing, that is, after rendering * the view. Will be called on any outcome of handler execution, thus allows * for proper resource cleanup. */ defaultvoidafterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex)throws Exception { }
interface 설명에도 적혀있지만 세 method는 적용 시점이 다르다.
preHandle(…) : controller 요청 전 ( Called after HandlerMapping determined an appropriate handler object, but before HandlerAdapter invokes the handler )
postHandle(…) : controller 요청 이후 (Called after HandlerAdapter actually invoked the handler, but before the DispatcherServlet renders the view.)
afterCompletion(…) : 요청 완료 이후 (= 뷰 렌더링 이후 )
afterCompletion같은 경우에는 interface 설명에 보면 예외가 발생해도 실행됨으로, resource 정리에 활용될 수 있다고 한다. 반면 postHandle은 controller가 예외없이 성공적으로 실행됬을때만 실행된다.