过滤器Filter作为Java三大器之一,在Java Web的使用中有很高的地位。所谓过滤器,就是实现了javax.servlet.Filter接口的服务器端程序。
Filter有如下几个用处:
- 在HttpServletRequest到达Servlet之前,拦截客户的HttpServletRequest。
- 根据需要检查HttpServletRequest,也可以修改HttpServletRequest头和数据。
- 在HttpServletResponse到达客户端之前,拦截HttpServletResponse。
- 根据需要检查HttpServletResponse,也可以修改HttpServletResponse头和数据。
Filter有如下几个方法:
- void init(FilterConfig filterConfig) 用于完成过滤器的初始化。
- void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) 实现过滤功能,该方法对每个请求增加额外的处理。
- void destroy() 用于过滤器销毁前,完成某些资源的回收。
通过@WebFilter实现
通过@WebFilter和@ServletComponentScan可实现Filter。其中,@WebFilter标注在实现Filter接口的过滤器类,并标注相应参数;@ServletComponentScan标注在SpringBoot启动类,标注后Servlet、Filter(过滤器)、Listener(监听器)可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码!
具体示例代码如下:
TestFilter.java
package com.example.demo.filter;import org.springframework.core.annotation.Order;import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;/*** @description:* @time: 2021/8/3 16:12*/
@WebFilter(initParams = {@WebInitParam(name="testFilter",value = "/test")})
@Order(1)
public class TestFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {Filter.super.init(filterConfig);System.out.println("-----Filter初始化完成-----");}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,FilterChain filterChain) throws IOException, ServletException {System.out.println("-----调用服务前执行代码-----");filterChain.doFilter(servletRequest,servletResponse);System.out.println("-----调用服务后执行代码-----");}@Overridepublic void destroy() {Filter.super.destroy();System.out.println("Filter已销毁");}
}
TestController.java
package com.example.demo.controller;import com.example.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;/*** @description:* @time: 2021/8/3 16:04*/
@RestController
public class TestController {@AutowiredTestService testService;@RequestMapping(value = "/test",method = RequestMethod.GET)public Object test(){testService.printTest();return "test";}}
Demo1Application.java
package com.example.demo;@SpringBootApplication
@ServletComponentScan
public class Demo1Application {public static void main(String[] args) {SpringApplication.run(Demo1Application.class, args);}
}
运行结果如下:
通过@Bean实现
此处为了测试@order的效果,使用了两个过滤器测试。
WebConfig.java
package com.example.demo.config;import com.example.demo.filter.FirstFilter;
import com.example.demo.filter.SecondFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.servlet.Filter;/*** @description:* @time: 2021/8/4 10:11*/
@Configuration
public class WebConfig {@Beanpublic FilterRegistrationBean firstFilterRegistrationBean(){FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();filterFilterRegistrationBean.addUrlPatterns("/hello");// 设置优先级filterFilterRegistrationBean.setOrder(1);// 绑定过滤器FirstFilter filter = new FirstFilter();filterFilterRegistrationBean.setFilter(filter);return filterFilterRegistrationBean;}@Beanpublic FilterRegistrationBean secondFilterRegistrationBean(){FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();filterFilterRegistrationBean.addUrlPatterns("/hello");// 设置优先级filterFilterRegistrationBean.setOrder(2);// 绑定过滤器SecondFilter filter = new SecondFilter();filterFilterRegistrationBean.setFilter(filter);return filterFilterRegistrationBean;}
}
FirstFilter.java
package com.example.demo.filter;import javax.servlet.*;
import java.io.IOException;/*** @description:* @time: 2021/8/4 10:09*/
public class FirstFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {Filter.super.init(filterConfig);System.out.println("-----FirstFilter初始化完成-----");}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,FilterChain filterChain) throws IOException, ServletException {System.out.println("-----服务前执行代码FirstFilter-----");filterChain.doFilter(servletRequest,servletResponse);System.out.println("-----服务后执行代码FirstFilter-----");}@Overridepublic void destroy() {Filter.super.destroy();System.out.println("FirstFilter已销毁");}
}
SecondFilter.java
package com.example.demo.filter;import javax.servlet.*;
import java.io.IOException;/*** @description:* @time: 2021/8/4 10:30*/
public class SecondFilter implements Filter{@Overridepublic void init(FilterConfig filterConfig) throws ServletException {Filter.super.init(filterConfig);System.out.println("-----SecondFilter初始化完成-----");}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,FilterChain filterChain) throws IOException, ServletException {System.out.println("-----服务前执行代码SecondFilter-----");filterChain.doFilter(servletRequest,servletResponse);System.out.println("-----服务后执行代码SecondFilter-----");}@Overridepublic void destroy() {Filter.super.destroy();System.out.println("SecondFilter已销毁");}
}
执行效果如下:
通过结果可见,filter的初始化服务时在启动Spring Boot项目的时候就已经完成。而当用户访问服务时,执行的顺序按照order的优先级(数字越小越优先)执行。