filter过滤器的使用
程序员文章站
2024-02-09 15:58:16
...
Filter理解:
对sevlet服务前后做些处理,主要针对request和response做一些过滤,鉴权,日志等操作:
Filter类
Filter过滤器实现的是javax.servlet.Filter接口的类,而在javax.servlet.Filter中定义了以下三个方法:
public interface Filter {
//初始化过滤器后执行的操作
default void init(FilterConfig filterConfig) throws ServletException {
}
// 对请求进行过滤
void doFilter(ServletRequest var1, ServletResponse var2, FilterChain var3) throws IOException, ServletException;
// 销毁过滤器后执行的操作,主要用户对某些资源的回收
default void destroy() {
}
}
自定Filter用config方式配置
/** 自定义过滤器使用config方式配置过滤器
* @author ding
* @version V1.0
* @date 2019/11/6 16:24
*/
@Component
public class MyFilter implements Filter {
private static final Logger logger = LoggerFactory.getLogger(MyFilter.class);
// 初始化过滤器操作
@Override
public void init(FilterConfig filterConfig) throws ServletException {
logger.info("初始化过滤器: "+filterConfig.getFilterName());
}
// 具体过滤业务逻辑
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//对请求进行预处理
logger.info("过滤器开始对请求进行预处理:");
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String requestURI = httpServletRequest.getRequestURI();
logger.info("请求的接口为: "+requestURI);
long startTime = System.currentTimeMillis();
//通过 doFilter 方法实现过滤功能
filterChain.doFilter(servletRequest, servletResponse);
// 上面的 doFilter 方法执行结束后用户的请求已经返回
long endTime = System.currentTimeMillis();
System.out.println("该用户的请求已经处理完毕,请求花费的时间为:" + (endTime - startTime));
}
// 销毁过滤器后执行的操作,主要用户对某些资源的回收
@Override
public void destroy() {
logger.info("销毁过滤器");
}
}
/**
* 在配置中注册自定义的过滤器
*
* 在配置中注册自定义的过滤器,通过FilterRegistrationBean 的setOrder 方法可以决定 Filter 的执行顺序。
* @author ding
* @version V1.0
* @date 2019/11/6 16:32
*/
@Configuration
public class MyFilterConfig {
@Autowired
MyFilter myFilter;
@Bean
public FilterRegistrationBean<MyFilter> thirdFilter(){
FilterRegistrationBean<MyFilter> filterRegistrationBean = new FilterRegistrationBean<>();
filterRegistrationBean.setFilter(myFilter);//设置自定义的过滤类
filterRegistrationBean.setUrlPatterns(new ArrayList<>(Arrays.asList("/api/*"))); // 设置过滤匹配url模式
return filterRegistrationBean;
}
}
自定Filter用注解方式配置
/**
* 1.自定义过滤器使用注解方式配置过滤器
* 2.该@WebFilter申明该类是一个 过滤器 filterName指定该过滤器的名称 urlPatterns指定过滤器的匹配模式
* 3.另外,为了能让 Spring 找到它,你需要在启动类上加上 @ServletComponentScan 注解。
* @author ding
* @version V1.0
* @date 2019/11/6 16:52
*/
@WebFilter(filterName = "MyFilterWithAnnotation", urlPatterns = "/api/annotation/*")
public class MyFilterWithAnnotation implements Filter {
private static final Logger logger = LoggerFactory.getLogger(MyFilterWithAnnotation.class);
@Override
public void init(FilterConfig filterConfig) {
logger.info("初始化过滤器");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//对请求进行预处理
logger.info("过滤器开始对请求进行预处理:");
HttpServletRequest request = (HttpServletRequest) servletRequest;
String requestUri = request.getRequestURI();
System.out.println("请求的接口为:" + requestUri);
long startTime = System.currentTimeMillis();
//通过 doFilter 方法实现过滤功能
filterChain.doFilter(servletRequest, servletResponse);
// 上面的 doFilter 方法执行结束后用户的请求已经返回
long endTime = System.currentTimeMillis();
System.out.println("该用户的请求已经处理完毕,请求花费的时间为:" + (endTime - startTime));
}
@Override
public void destroy() {
logger.info("销毁过滤器");
}
}
多个过滤器执行顺序设置
再添加一个过滤器
@Component
public class MyFilter2 implements Filter {
private static final Logger logger = LoggerFactory.getLogger(MyFilter2.class);
// 初始化过滤器操作
@Override
public void init(FilterConfig filterConfig) throws ServletException {
logger.info("初始化过滤器2: "+filterConfig.getFilterName());
}
// 具体过滤业务逻辑
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//对请求进行预处理
logger.info("过滤器开始对请求进行预处理2:");
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String requestURI = httpServletRequest.getRequestURI();
logger.info("请求的接口为2: "+requestURI);
long startTime = System.currentTimeMillis();
//通过 doFilter 方法实现过滤功能
filterChain.doFilter(servletRequest, servletResponse);
// 上面的 doFilter 方法执行结束后用户的请求已经返回
long endTime = System.currentTimeMillis();
System.out.println("该用户的请求已经处理完毕,请求花费的时间为2:" + (endTime - startTime));
}
// 销毁过滤器后执行的操作,主要用户对某些资源的回收
@Override
public void destroy() {
logger.info("销毁过滤器2");
}
}
配置执行顺序
@Bean
public FilterRegistrationBean<MyFilter> thirdFilter(){
FilterRegistrationBean<MyFilter> filterRegistrationBean = new FilterRegistrationBean<>();
filterRegistrationBean.setFilter(myFilter);
filterRegistrationBean.setOrder(2); //执行顺序
filterRegistrationBean.setUrlPatterns(new ArrayList<>(Arrays.asList("/api/*")));
return filterRegistrationBean;
}
@Bean
public FilterRegistrationBean<MyFilter2> thirdFilter2(){
FilterRegistrationBean<MyFilter2> filterRegistrationBean = new FilterRegistrationBean<>();
filterRegistrationBean.setOrder(1);
filterRegistrationBean.setFilter(myFilter2);// 执行顺序
filterRegistrationBean.setUrlPatterns(new ArrayList<>(Arrays.asList("/api/*")));
return filterRegistrationBean;
}
顺道验证
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/ha")
public String gethahah() throws InterruptedException {
return "哈哈哈哈哈";
}
}
上一篇: JAVAWEB过滤器Filter的使用
下一篇: 过滤器Filter的使用