SpringFrameWork/SpringSecurity
[SpringSecurity #12] Security Custom Filter 만들기
계단
2020. 10. 4. 10:26
# Security Custom Filter 만들기
public class LoggingFilter extends GenericFilter {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
chain.doFilter(request, response);
stopWatch.stop();
logger.info(stopWatch.prettyPrint());
}
}
먼저 Filter 클래스를 만들어 준다. 이때 GenericFilter 를 상속받으면 스프링에 친화적인 필터를 만들 수 있다.
override 를 해서 doFilter 만 재정의를 해본다.
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterBefore(new LoggingFilter(), WebAsyncManagerIntegrationFilter.class);
}
SecurityConfig 클래스에 들어가서 재정의한 필터를 어떤 위치에서 실행될 것인지를 설정해준다.