본문 바로가기

카테고리 없음

[Spring Framework] Configuration Class에 Interceptor 등록하기

Configuration Class에 Interceptor 등록하기

-Spring @Configuration Class 설정시 Interceptors 설정하는 방법에 대해서



:하단 링크 참조.

http://www.leveluplunch.com/blog/2014/04/29/using-spring-mvc-handler-interceptors/



InterceptorTest Class


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package org.prudy.gettingstart.config;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
 
public class InterceptorTest extends HandlerInterceptorAdapter {
    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("POST_HANDLE");
    }
 
    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("AFTER_COMPLETION");
    }
 
    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        System.out.println("AFTER_CONCURRENT_HANDLING_STARTED");
    }
 
    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        System.out.println("PRE_HANDLE");
        return false;
    }
}
cs


Configuration Class

1
2
3
4
5
6
7
8
@Configuration
@ComponentScan(basePackages = "org.prudy.gettingstart")
public class MvcConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new InterceptorTest()).addPathPatterns("/**").excludePathPatterns("");
    }
}
cs

RESULT


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
14:41:16.937 [http-nio-8091-exec-5] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/spring-no-xml/]
14:41:16.937 [http-nio-8091-exec-5] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /
14:41:16.937 [http-nio-8091-exec-5] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/]
14:41:16.937 [http-nio-8091-exec-5] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Mapping [/] to HandlerExecutionChain with handler [org.springframework.web.servlet.mvc.ParameterizableViewController@465bc8c4] and 1 interceptor
14:41:16.937 [http-nio-8091-exec-5] DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/spring-no-xml/] is: -1
PRE_HANDLE
POST_HANDLE
14:41:16.937 [http-nio-8091-exec-5] DEBUG o.s.web.servlet.DispatcherServlet - Rendering view [org.thymeleaf.spring3.view.ThymeleafView@3abe02d6] in DispatcherServlet with name 'dispatcher'
14:41:16.937 [http-nio-8091-exec-5] DEBUG org.thymeleaf.TemplateEngine - [THYMELEAF][http-nio-8091-exec-5] STARTING PROCESS OF TEMPLATE "Test" WITH LOCALE ko_KR
14:41:16.942 [http-nio-8091-exec-5] DEBUG org.thymeleaf.TemplateRepository - [THYMELEAF][http-nio-8091-exec-5] Template "Test" was correctly resolved as resource "/WEB-INF/templete/Test.html" in mode HTML5 with resource resolver "SERVLETCONTEXT"
14:41:16.942 [http-nio-8091-exec-5] DEBUG org.thymeleaf.TemplateEngine - [THYMELEAF][http-nio-8091-exec-5] Starting process on template "Test" using mode "HTML5"
14:41:16.942 [http-nio-8091-exec-5] DEBUG org.thymeleaf.TemplateEngine - [THYMELEAF][http-nio-8091-exec-5] Finished process on template "Test" using mode "HTML5"
14:41:16.942 [http-nio-8091-exec-5] DEBUG org.thymeleaf.TemplateEngine - [THYMELEAF][http-nio-8091-exec-5] FINISHED PROCESS AND OUTPUT OF TEMPLATE "Test" WITH LOCALE ko_KR
14:41:16.942 [http-nio-8091-exec-5] DEBUG org.thymeleaf.TemplateEngine.TIMER - [THYMELEAF][http-nio-8091-exec-5][Test][ko_KR][1236564][1] TEMPLATE "Test" WITH LOCALE ko_KR PROCESSED IN 1236564 nanoseconds (approx. 1ms)
AFTER_COMPLETION
14:41:16.942 [http-nio-8091-exec-5] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
cs


- 정리
 PRE_HANDLE : 컨트롤러 실행 전, 세션 값 확인(보안), 요청정보 가공 또는 추가 할 때 사용. 반환값이 있으며 True면 컨트롤러로 실행 이후 프로세스를 진행 False면 남은 인터셉터들이 실행되지 않음

 POST_HANDLE : PRE_HANDLE이 끝나고 -> 컨트롤러 동작이 끝난 이후 실행되는 값, 위 예제에는 컨트롤러가 없어 로그가 남지 않았지만 파라미터로 컨트롤러의 반환 값(ModelAndView)를 포함해 해당 값을가지고 참조, 조작이 가능하며 인터셉터의 진행은 간섭이 불가능

 AFTER_COMPLETION : 모든 인터셉터가 실행을 완료된 후 실행.