Filter过滤器总结
最近做cas登录,cas-client使用了Filter,感觉挺实用,所以把Filter做个总结,方便以后查阅。
1、基本介绍
1、Filter 过滤器它是 JavaWeb 的三大组件之一。三大组件分别是:Servlet 程序、Listener 监听器、Filter 过滤器
2、Filter 过滤器它是 JavaEE 的规范。也就是接口
3、Filter 过滤器它的作用是:拦截请求,过滤响应拦截请求常见的应用场景有: 1、权限检查 2、日记操作 3、事务管理……
2、使用步骤
Filter 过滤器的使用步骤:
1、编写一个类去实现 Filter 接口
2、实现过滤方法 doFilter()
3、到 web.xml 中去配置 Filter 的拦截路径(springboot不用配)
使用springboot一个配置类(加上@Configuration
),实现Filter接口(javax.servlet包下,别引错包),实现3个方法,
init、doFilter、destroy。分别是初始化,过滤拦截,销毁
3、Filter的生命周期
Filter 的生命周期包含几个方法
1、构造器方法
2、init 初始化方法 在第 1,2 步,在 web 工程启动的时候执行(Filter 已经创建)
3、doFilter 过滤方法 每次拦截到请求,就会执行
4、destroy 销毁 第 4 步,停止 web 工程的时候,就会执行(停止 web 工程,也会销毁 Filter 过滤器)
4、FilterConfig 的含义
在重写init方法后
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
这里的参数FilterConfig 类,它是 Filter 过滤器的配置文件类,Tomcat 每次创建 Filter的时候,也会同时创建一个 FilterConfig 类,这里包含了 Filter 配置文件的配置信息。
FilterConfig 类的作用是获取 filter 过滤器的配置内容
1、获取 Filter 的名称 filter-name 的内容(在web.xml中的filter-name里指定,springboot是在@WebFilter(filterName = "myFilter")
2、获取在 Filter 中配置的 init-param 初始化参数 (web.xml是在/init-param指定,springboot是在initParams = @WebInitParam(name = “key”,value = “中国”)指定)
3、获取 ServletContext 对象
以下是springboot使用Filter并获取初始数据的示例:
@WebFilter(filterName = "myFilter",urlPatterns = "/**",initParams = @WebInitParam(name = "key",value = "中国"))
public class IdsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String filterName = filterConfig.getFilterName();
String initParameter = filterConfig.getInitParameter("key");
System.out.println("初始化………………");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("doFilter………………");
chain.doFilter(request,response);
}
@Override
public void destroy() {
System.out.println("销毁………………");
}
}
注意这样指定必须在启动类上加上扫描范围:@ServletComponentScan
@SpringBootApplication
@ServletComponentScan(basePackages = "com.atpingan.idsparent.config")
public class IdsParentApplication {
public static void main(String[] args) {
SpringApplication.run(IdsParentApplication.class, args);
}
}
如果不加@ServletComponentScan,那就在配置类使用@Configuration,不传参。cas-client就是这个套路,初始化用来加载cas-config.properties文件内容。
@WebFilter注解详解
@WebFilter注解用于将一个类声明为过滤器,该注解在部署时被容器处理,容器根据具体的配置将相应的类部署为过滤器。
常见参数:
filterName:指定过滤器的名称,等价于web.xml中的<filter-name>
元素,如果没有显示指定,那么,使用Filter的完全限定名作为名称。
urlPatterns:指定一组过滤器的URL匹配模式,等价于web.xml中的<url-pattern>
value:等价于urlPatterns。这两个属性不能同时使用
servletNames:指定过滤器用于哪些Servlet,等价于web.xml中的<servlet-name>
initParams:指定一组过滤器参数(每一个参数用@WenInitParam标识),等价于web.xml中的<init-param>
例如:
@WebFilter( filterName="authorityFilter", urlPatterns={"/*"}, initParams={ @WebInitParam(name="encoding",value="UTF-8"), @WebInitParam(name="loginPage",value="login.jsp") } )
5、FilterChain 过滤器链
过滤器链的工作流程:
chain.doFilter(request,response)的作用是:
1、执行下一个Filter过滤器(如果有下一个Filter)
2、执行目标资源(没有下一个Filter),如访问接口、图片,html等。可理解为度过了拦截器
特点:
1、所有filter和目标资源默认都执行在同一线程
2、多个request共同执行时,他们都使用同一个request对象
6、Filter的拦截路径
spring项目:
–精确匹配:在 url-pattern中配置,例如:target.jsp,表示以上配置的路径,表示请求地址必须为:http://ip:port/工程路径/target.jsp
–目录匹配:在 url-pattern中配置,例如:/admin/* ,表示请求地址必须为:http://ip:port/工程路径/admin/*
–后缀名匹配:在 url-pattern中配置,例如:*.html,表示请求地址必须以.html 结尾才会拦截到
对于springboot如果要配置的话就只能使用
@WebFilter(filterName = "myFilter",urlPatterns = "/**",initParams = @WebInitParam(name = "key",value = "中国"))
这里的urlPatterns 就是拦截路径
如果使用@Configuration表示所有都拦截。
一个简单的使用Filter做权限校验:
@Configuration
public class ManagerFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
//从session获取是否有user如果有则度过拦截,表示已登录,否则跳转到登录页面
Object user = httpServletRequest.getSession().getAttribute("user");
if (user == null) {
httpServletRequest.getRequestDispatcher("/pages/user/login.jsp").forward(servletRequest,servletRes ponse);
} else {
filterChain.doFilter(servletRequest,servletResponse);
}
}
@Override
public void destroy() {
}
}