728x90
반응형
상황
- 스프링 컨트롤 실행전에 인터셉터를 추가 해야한다.
- 먼저 인터셉터를 만들고
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class YYNControllerInterceptor implements HandlerInterceptor {
private final static Logger LOGGER = LoggerFactory.getLogger(YYNControllerInterceptor.class);
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
Logger.info("hello~");
// 처리
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
}
- WebMvcConfigurerAdapter를 상속받은 클래스에 addInterceptors 함수를 override해서 위에서 생성한 인터셉터를 addInterceptor 해서 넣어줬는데.. add까지는 되는데 컨트롤러 호출시 hello가 안찍힌다.... 왜!??
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.MappedInterceptor;
...
@ComponentScan(basePackages = { "..." }, includeFilters = {
@Filter(type = FilterType.ANNOTATION, classes = { Controller.class }) }, excludeFilters = {
@Filter(type = FilterType.ANNOTATION, classes = { Service.class, Repository.class,
Configuration.class }) })
@Configuration
public class ..Servlet extends WebMvcConfigurerAdapter {
private final static Logger LOGGER = LoggerFactory.getLogger(..Servlet.class);
...
@Override
public void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
LOGGER.info("[addInterceptors] ******************** ");
registry.addInterceptor(new YYNControllerInterceptor()).addPathPatterns("/경로/~Controller");
}
}
처리방법
MappedInterceptor
- 참고한 내용 ( https://stackoverflow.com/a/52781681 )
- addInterceptors 함수를 override할 필요없이, 그냥 MappedInterceptor을 만들어서 @Bean으로 등록하면된다. ( 인터셉터가 더 필요하면 계속 MappedInterceptor 를 만들어서 Bean으로 등록하면된다)
- 더 좋은건 WebMvcConfigurerAdapter 를 상속받아서 구현할 필요도 없다. 그냥 어디서건 MappedInterceptor를 만들어 Bean으로 등록만 해주면 된다.
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.MappedInterceptor;
...
@ComponentScan(basePackages = { "..." },
includeFilters = {
@Filter(type = FilterType.ANNOTATION, classes = { Controller.class })
},
excludeFilters = {
@Filter(type = FilterType.ANNOTATION, classes = { Service.class, Repository.class,Configuration.class })
}
)
@Configuration
public class ..Servlet extends WebMvcConfigurerAdapter {
private final static Logger LOGGER = LoggerFactory.getLogger(..Servlet.class);
...
//@Override
//public void addInterceptors(InterceptorRegistry registry) {
// super.addInterceptors(registry);
// LOGGER.info("[addInterceptors] ******************** ");
// registry.addInterceptor(new YYNControllerInterceptor()).addPathPatterns("/경로/~Controller");
//}
@Bean
public YYNControllerInterceptor yynControllerInterceptor() {
return new YYNControllerInterceptor();
}
@Bean
public MappedInterceptor yynMappedInterceptor(YYNControllerInterceptor yynControllerInterceptor) {
String[] arr = new String[] {"/경로/~Controller"};
return new MappedInterceptor(arr, yynControllerInterceptor);
}
}
확인
- 잘된다.
- addInterceptors를 써서 등록했는데 안되는건.. 모르겠다. 근데 이제 그냥 MappedInterceptor 만들고 @Bean으로 등록하고 쓰는게 훨씬 편할꺼같다.
- 아 하루종일 삽질한게 허무하네..
728x90
반응형