깃허브 링크 : 클릭
servlet 패키지의 HandlerInterceptor interface를 구현하여 인터셉터를 구현할 수 있다.
@Slf4j
public class FirstFilter implements Filter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("First Interceptor preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.info("First Interceptor postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
설정 파일을 만들어 빈으로 등록한다.
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new FirstInterceptor())
.order(1)
.addPathPatterns("/*")
.excludePathPatterns("/second-interceptor");
}
}
컨트롤러를 등록하고 테스트 코드를 작성해서 확인해보자
@RestController
@Slf4j
public class HelloController {
@GetMapping("/")
public String hello() {
log.info("/ 진입");
return "Hello";
}
}
@SpringBootTest
@AutoConfigureMockMvc
class HelloControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void enter_root() throws Exception {
//when, then
mockMvc.perform(MockMvcRequestBuilders.get("/"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
컨트롤러, 인터셉터를 두 개씩 더 추가해서 테스트해보자
@Slf4j
public class SecondInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("Second Interceptor preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.info("Second Interceptor postHandle");
}
}
@Slf4j
public class ThirdInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("Third Interceptor preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.info("Third Interceptor postHandle");
}
}
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new FirstInterceptor())
...;
/* 추가 */
registry.addInterceptor(new SecondInterceptor())
.order(0)
.addPathPatterns("/", "/second-interceptor");
registry.addInterceptor(new ThirdInterceptor())
.order(2)
.addPathPatterns("/**")
.excludePathPatterns("/second-interceptor");
}
}
@GetMapping("/second-interceptor")
public String hello2() {
log.info("/second-interceptor 진입");
return "Hello";
}
@GetMapping("/third/interceptor")
public String hello3() {
log.info("/third-interceptor 진입");
return "Hello";
}
@SpringBootTest
@AutoConfigureMockMvc
class HelloControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void enter_root() throws Exception {
//when, then
mockMvc.perform(MockMvcRequestBuilders.get("/"))
.andExpect(MockMvcResultMatchers.status().isOk());
mockMvc.perform(MockMvcRequestBuilders.get("/second-interceptor"))
.andExpect(MockMvcResultMatchers.status().isOk());
mockMvc.perform(MockMvcRequestBuilders.get("/third/interceptor"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}
- | first | second | third |
---|---|---|---|
우선순위 | 1 | 0 | 2 |
url patterns | /* | /, /second-interceptor | /** |
exclude patterns | /second-interceptor | - | /second-interceptor |
/*, /** 차이
- /* : / 다음에 오는 모든 문자열을 포함하는 경로를 말함
- /** : / 를 포함하는 모든 문자열과 그 아래 하위 모든 경로도 포함함