java三大器Spring Boot 过滤器的实现
Filter生命周期
- 程序启动调用Filter的init()方法(永远只调用一次);
- 程序停止调用Filter的destroy()方法(永远只调用一次);
- doFilter()方法每次的访问请求如果符合拦截条件都会调用(程序第一次运行,会在servlet调用init()方法以后调用;
- 不管第几次,都在调用doGet(),doPost()方法之前)。
一、注解方式实现(@WebFilter )
@WebFilter(urlPatterns = "/api/*", filterName = "myFilter")
@Order(1)
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("MyFilter,初始化了");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
String url = request.getRequestURI();
String method = request.getMethod();
System.out.println("请求的接口=" + url + "请求的方式=" + method);
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
System.out.println("MyFilter,销毁了");
}
}
filterChain.doFilter(servletRequest, servletResponse) 执行后当前过滤器执行完毕
@SpringBootApplication
@ServletComponentScan
public class SpringDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDemoApplication.class, args);
}
}
@RestController
@RequestMapping("/api")
public class ApiController {
@GetMapping("/filter")
public String getStr() {
return "我被myFilter过滤器监控了";
}
}
@WebFilter
@WebFilter 用于将一个类声明为过滤器,该注解将会在部署时被容器处理,容器将根据具体的属性配置将相应的类部署为过滤器。该注解具有下表给出的一些常用属性 ( 以下所有属性均为可选属性,但是 value、urlPatterns、servletNames 三者必需至少包含一个,且 value 和 urlPatterns 不能共存,如果同时指定,通常忽略 value 的取值 )
@Order(1)
标识当前过滤器的执行顺序,值越小越靠前执行;
@ServletComponentScan
只有在springboot 启动类添加该注解时@WebFilter 注解才会生效
扩展
SpringBootApplication 上使用 @ServletComponentScan 注解后 Servlet可以直接通过@WebServlet 注解自动注册 Filter可以直接通过@WebFilter 注解自动注册 Listener可以直接通过@WebListener 注解自动注册
二、直接注入到spring中
@Configuration
public class FilterConfig {
@Bean
public MyFilter myFilter() {
return new MyFilter();
}
@Bean
public FilterRegistrationBean getFilterRegistrationBean(MyFilter myFilter) {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(myFilter);
filterRegistrationBean.setOrder(1);
filterRegistrationBean.addUrlPatterns("/api/*");
filterRegistrationBean.setName("myFilter");
return filterRegistrationBean;
}
}
也可以这样写
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean<MyFilter> getFilterRegistrationBean() {
FilterRegistrationBean<MyFilter> filterRegistrationBean = new FilterRegistrationBean<MyFilter>();
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.setOrder(1);
filterRegistrationBean.addUrlPatterns("/api/*");
filterRegistrationBean.setName("myFilter");
return filterRegistrationBean;
}
}
过滤器中返回值
实际研发中我们可能通过过滤器来进行某些判断,条件不满足时需及时返回,我们可以这样做
doFilter 方法中添加如下代码返回信息,具体返回信息格式按实际需求而变;
servletResponse.setCharacterEncoding("UTF-8");
servletResponse.setContentType("application/json; charset=utf-8");
PrintWriter out = servletResponse.getWriter();
JSONObject res = new JSONObject();
res.put("msg", "错误");
res.put("success", "false");
out.append(res.toString());
|