반응형
Filter를 구현하고 @Component 어노테이션을 붙여 등록한다.
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
System.out.println("Request URI is: " + req.getRequestURI());
chain.doFilter(request, response);
System.out.println("Response Status Code is: " + res.getStatus());
}
}
Filter를 구현한 추상 클래스를 구현하고 @Component 를 붙여 등록한다. OncePerRequestFilter는 서블릿 당 한 번만 수행하도록 보장하는 필터 클래스이다.
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class MyFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
FilterChain filterChain)
throws ServletException, IOException {
System.out.println("Request URI is: " + httpServletRequest.getRequestURI());
filterChain.doFilter(httpServletRequest, httpServletResponse);
System.out.println("Response Status Code is: " + httpServletResponse.getStatus());
}
}
FilterRegistrationBean 유형의 @Bean을 등록한다. urlPattern을 등록하면서 특정 요청에만 필터를 사용하도록 할 수 있다.
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
System.out.println("Request URI is: " + req.getRequestURI());
chain.doFilter(request, response);
System.out.println("Response Status Code is: " + res.getStatus());
}
@Bean
public FilterRegistrationBean<MyFilter> filter()
{
FilterRegistrationBean<MyFilter> bean = new FilterRegistrationBean<>();
bean.setFilter(new MyFilter());
bean.addUrlPatterns("/execute/*"); // 또는 `setUrlPatterns()`를 사용합니다.
return bean;
}
}
@WebFilter 어노테이션을 붙여 등록한다. 이를 위해서는 @SpringbootApplication 어노테이션을 붙인 main에 @ServletComponentScan 어노테이션을 추가한다. 어노테이션의 옵션 중 urlPattern에 값을 넣어 특정 url 요청에만 필터가 동작하도록 할 수 있다.
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(urlPatterns = "/execute/*")
public class FirstFilter implements Filter {
@Override
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
System.out.println("Request URI is: " + req.getRequestURI());
chain.doFilter(request, response);
System.out.println("Response Status Code is: " + res.getStatus());
}
}
반응형
댓글