原文链接:java过滤器(Filter – 编程屋
目录
1 过滤器简介
2 Filter详细介绍
3 Filter的用法
3.1 用法1
3.2 用法2
filter也称之为过滤器,它是javaWeb三大组件之一(Servlet程序、Listener监听器、Filter过滤器)
作用:既可以对请求进行拦截,也可以对响应进行处理。
常见场景:权限检查,日记操作、拦截请求、过滤操作、对请求字符设置编码。
要想介绍filter,就必须介绍Filter中的三个方法。
/**
* web应用启动时,web服务器将创建Filter的实例对象,并调用init方法,读取web.xml的配置,完成对象的初始化功能,
* 从而为后续的用户请求做好拦截的准备工作(filter对象只会创建一次,init方法也只会执行一次,开发人员通过init的参数,
* 可或得代表当前filter配置信息的FilterConfig对象)
* @param filterConfig
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
/**
* 这个方法完成实际的过滤操作,当客户请求访问与过滤器相关联的URL的时候,Servlet过滤器将先执行doFilter方法,FilterChain参数用于访问后续过滤器
* @param servletRequest
* @param servletResponse
* @param filterChain
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
}
/**
* filter创建后会保存在内存中,当web应用移除或者服务器停止时才销毁,该方法在Filter的生命周期中仅执行一次,在这个方法中,可以释放过滤器使用的资源
*/
@Override
public void destroy() {
}
1)自定义一个过滤器实现Filter接口、配置@WebFilter注解,配置拦截路径(也可通过web.xml配置)
@WebFilter(urlPatterns = "/*")
public class MyFilterOne implements Filter {
/**
* web应用启动时,web服务器将创建Filter的实例对象,并调用init方法,读取web.xml的配置,完成对象的初始化功能,
* 从而为后续的用户请求做好拦截的准备工作(filter对象只会创建一次,init方法也只会执行一次,开发人员通过init的参数,
* 可或得代表当前filter配置信息的FilterConfig对象)
* @param filterConfig
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
/**
* 这个方法完成实际的过滤操作,当客户请求访问与过滤器相关联的URL的时候,Servlet过滤器将先执行doFilter方法,FilterChain参数用于访问后续过滤器
* @param servletRequest
* @param servletResponse
* @param filterChain
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("我是过滤器,我进来了");
}
/**
* filter创建后会保存在内存中,当web应用移除或者服务器停止时才销毁,该方法在Filter的生命周期中仅执行一次,在这个方法中,可以释放过滤器使用的资源
*/
@Override
public void destroy() {
}
}
2)在启动类上加上@ServletComponentScan注解
@SpringBootApplication
@ServletComponentScan
public class SpringbootInterceptorApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootInterceptorApplication.class, args);
}
}
3)controller:
@RestController
public class LoginController {
@GetMapping("/test/filter")
public String testFilter(){
return "该请求被拦截了,但是在过滤器中已经放行了";
}
}
直接浏览器访问:
控制台输出:发现该请求已经被过滤去拦截
1)自定义一个过滤器实现Filter接口、配置拦截路径(也可通过web.xml配置)
public class MyFilterOne implements Filter {
/**
* web应用启动时,web服务器将创建Filter的实例对象,并调用init方法,读取web.xml的配置,完成对象的初始化功能,
* 从而为后续的用户请求做好拦截的准备工作(filter对象只会创建一次,init方法也只会执行一次,开发人员通过init的参数,
* 可或得代表当前filter配置信息的FilterConfig对象)
* @param filterConfig
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
/**
* 这个方法完成实际的过滤操作,当客户请求访问与过滤器相关联的URL的时候,Servlet过滤器将先执行doFilter方法,FilterChain参数用于访问后续过滤器
* @param servletRequest
* @param servletResponse
* @param filterChain
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("我是过滤器,我进来了");
filterChain.doFilter(servletRequest, servletResponse);
}
/**
* filter创建后会保存在内存中,当web应用移除或者服务器停止时才销毁,该方法在Filter的生命周期中仅执行一次,在这个方法中,可以释放过滤器使用的资源
*/
@Override
public void destroy() {
}
}
2)在启动类上注册
@SpringBootApplication
public class SpringbootInterceptorApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootInterceptorApplication.class, args);
}
/**
* 注册Filter
*/
@Bean
public FilterRegistrationBean getFilterRegistrationBean(){
FilterRegistrationBean bean = new FilterRegistrationBean(new MyFilterOne());
//bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
bean.addUrlPatterns("/*");
return bean;
}
}
测试结果与3.1相同。
总结:
要想调用配置一个过滤器:
1)启动类上增加注解@ServletComponentScan
2)新建一个类使其实现Filter接口,并实现里面的三个方法
3)在新建类上加上@WebFilter()注解,配置需要拦截的规则
4)在doFilter方法中控制filterChain.doFilter(servletRequest, servletResponse)调用
同样,如果前后端交互有遇到跨域问题的,也可以通过Filter解决(亲测有效),如:
以上只是部分内容,为了维护方便,本文已迁移到新地址:java过滤器(Filter – 编程屋