Filter
Shiro安全框架技术就是用Filter来实现的
Filter:过滤器 ,用来过滤网站的数据,符合条件放行,不符合条件不放行;
- 处理中文乱码
- 登录验证….
(比如用来过滤网上骂人的话,我***我自己 0-0)
过滤器就是Servlet。
Filter开发步骤:
- 导包
- 编写过滤器
- 导包不要错 (注意)
实现Filter接口,重写对应的方法即可
- 重写Filter的方法
package com.bin.filter;
import javax.servlet.*;
import java.io.IOException;
public class CharacterEncoding implements Filter {
//初始化,只要web服务器启动,就开始初始化,随时等待过滤对象的出现!
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CharacterEncoding初始化");
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=UTF-8");
//Chain:链
/*
1.过滤中的所有代码,在过滤特定请求的时候都会执行
2.必须要让过滤器继续通行
chain.doFilter(request,response);
*/
System.out.println("执行前");
//这一步千万不能少
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("执行后");
}
public void destroy() {
System.out.println("被销毁");
}
}
- 在web.xml中配置 Filter
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>com.bin.filter.CharacterEncoding</filter-class>
</filter>
<filter-mapping>
<!-- 只要是网址带上/servlet的任何请求,都会经过这个过滤器 -->
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
源码分析
- Filter源码
package javax.servlet;
import java.io.IOException;
public interface Filter {
default void init(FilterConfig filterConfig) throws ServletException {
}
void doFilter(ServletRequest var1, ServletResponse var2, FilterChain var3) throws IOException, ServletException; //var3的方法doFilter(request, response),它实现的功能就是继续执行下面的过滤器。
default void destroy() {
}
}
doFilter(ServletRequest,ServletResponse,FilterChain)
其中的参数:
ServletRequest/ServletResponse:每次在执行doFilter方法时 web容器负责创建一个request和一个response对象作为doFilter的参数传递进来。该request和该response就是在访问目标资源的service方法时的request和response。
FilterChain:过滤器链对象,通过该对象的doFilter方法可以继续执行其他过滤器。
- FilterChain
package javax.servlet;
import java.io.IOException;
public interface FilterChain {
void doFilter(ServletRequest var1, ServletResponse var2) throws IOException, ServletException;
}
FilterChain:servlet 容器为开发人员提供的对象,它提供了对某一资源的已过滤请求调用链的视图。过滤器使用 FilterChain 调用链中的下一个过滤器,如果调用的过滤器是链中的最后一个过滤器,则调用链末尾的资源。
实现一个Filter链
-
问题:怎样可以形成一个Filter链?
- 只要多个Filter对同一个资源进行拦截就可以形成Filter链
-
问题:怎样确定Filter的执行顺序?
- 由来确定,也就是xml配置文件的顺序。
过滤器一
package com.bin.filter;
import javax.servlet.*;
import java.io.IOException;
public class filterChainTest01 implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("我是过滤器2的请求");
chain.doFilter(request, response);//去执行下一个过滤器
System.out.println("我是过滤器2的响应");
}
public void destroy() {
}
}
过滤器二
package com.bin.filter;
import javax.servlet.*;
import java.io.IOException;
public class filterChainTest02 implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("我是过滤器3的请求");
chain.doFilter(request, response);//去执行下一个过滤器
System.out.println("我是过滤器3的响应");
}
public void destroy() {
}
}
web.xml
<filter>
<filter-name>chainTest1</filter-name>
<filter-class>com.bin.filter.filterChainTest01</filter-class>
</filter>
<filter-mapping>
<filter-name>chainTest1</filter-name>
<url-pattern>/show</url-pattern>
</filter-mapping>
<filter>
<filter-name>chainTest2</filter-name>
<filter-class>com.bin.filter.filterChainTest02</filter-class>
</filter>
<filter-mapping>
<filter-name>chainTest2</filter-name>
<url-pattern>/show</url-pattern>
</filter-mapping>
输出如下:
我是过滤器2的请求
我是过滤器3的请求
我是过滤器3的响应
我是过滤器2的响应
就像是栈的结构,先进后出。