우선 작업을 진행중 생성할 클래스들이 위치한 패키지를 context 파일에 component-scan에 지정했는지 확인.
<context:component-scan base-package="com.proj" />
package com.proj.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; @Component public class CommonInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("success interceptor"); return true; } }
인터셉터가 정상적으로 작동하는지 확인하기 위해 간단한 내용을 콘솔에 출력 해본다.
package com.proj.interceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Autowired private CommonInterceptor commonInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(commonInterceptor).addPathPatterns("/**").excludePathPatterns("/"); } }
인터셉터을 설정해주고 적용할 요청 경로와 제외할 요청경로를 지정해준다.
12월 28, 2018 12:15:23 오전 org.apache.coyote.AbstractProtocol start 정보: Starting ProtocolHandler ["http-nio-8080"] 12월 28, 2018 12:15:23 오전 org.apache.coyote.AbstractProtocol start 정보: Starting ProtocolHandler ["ajp-nio-8009"] 12월 28, 2018 12:15:23 오전 org.apache.catalina.startup.Catalina start 정보: Server startup in 8758 ms success interceptor
인터셉터에서 콘솔에 출력한 내용이 정상적으로 나오는지 확인.
도움이 되셨다면 공감이나 좋아요 부탁드립니다~♡
광고를 클릭해주시면 더욱 감사하구요~♡v♡/
혹시 더 궁금하신 점이나 부족한 부분, 잘못된 내용이 있다면 댓글 부탁드립니다~~
[Spring] Security CSRF multipart/form-data (0) | 2019.07.21 |
---|---|
[Spring] 스프링 시큐리티 로그아웃 설정(Security Logout) (0) | 2019.01.23 |
[eGovFrame : Eclipse] 전자정부 프레임워크 개발환경 설치 (6) | 2019.01.13 |
스프링(Spring)에서 Ajax 요청 / 응답 (0) | 2019.01.07 |
[Spring]스프링 인터셉터 설정 (0) | 2018.12.28 |
스프링(spring/egov) 타일즈(tiles) 설정 (0) | 2018.12.05 |
댓글 영역