一、简介
(一)概述
Filter,过滤器,用于在servlet之外对request 和response 进行修改。Filter 有一个 FilterChain 的概念,一个FilterChain 包括多个 Filter。客户端请求 request在抵达servlet 之前,会经过 FilterChain 里面所有的 Filter,服务器响应 response 从servlet抵达客户端浏览器之前也会经过 FilterChain 里面所有的 Filter 。
(二) Filter 的实现
1、实现自定义的 Filter 需要满足一下条件:
2、javax.servlet.Filter 接口
Filter 接口有三个方法:这三个方法反应了 Filter 的生命周期。
具体实现如下:
import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Logger; /** * * MyFilter.java * * @title 过滤器 * @description */ public class MyFilter implements Filter { private Logger logger = Logger.getLogger(this.getClass()); public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; String contextPath = request.getContextPath();//上下文路径 String servletPath = request.getServletPath();//得到访问的servlet或者jsp的路径 logger.debug("上下文路径:"+contextPath); logger.debug("访问的servlet或者jsp的路径 : "+servletPath); chain.doFilter(req, resp); } public void init(FilterConfig filterConfig) throws ServletException { String name = filterConfig.getInitParameter("name"); logger.debug("获取过滤器的初始化参数: " + name); } } |
3、 配置 Filter:每个过滤器需要配置在web.xml中才能生效,一个Filter需要配置
1)
2)
①
②
③
|
3)总结:一个Web程序可以配置多个Filter ,访问有先后顺序,
二、常用 Filter
(一)字符编码的 Filter
1、字符编码的 Filter 几乎每个项目都会用到。代码如下:
import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * * CharacterEncodingFilter.java * * @title 编码过滤器 */ public class CharacterEncodingFilter implements Filter { private String characterEncoding; private boolean enabled;//是否启用 public void init(FilterConfig config) throws ServletException { // 获取配置好的参数, characterEncoding = config.getInitParameter("characterEncoding");//配置好的字符编码 enabled = "true".equalsIgnoreCase(config.getInitParameter("enabled"));//是否启用 } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //设置字符编码 if (enabled && characterEncoding != null) { request.setCharacterEncoding(characterEncoding); response.setCharacterEncoding(characterEncoding); } chain.doFilter(request, response);//调用下一个过滤器 } public void destroy() { characterEncoding = null;//注销的时候,设为空 } } |
2、web.xml 配置如下:
|
(二)防盗链 Filter
1、防盗链需要使用到请求头 Referer ,该 Filter 的配置仅对 /images/ 和 /upload/images/ 下面的所有资源有效。代码如下:
import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * RefererFilter.java * * @title 责任链过滤器 */ public class RefererFilter implements Filter { public void init(FilterConfig config) throws ServletException { } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { // 必须的 HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; // 禁止缓存 response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragrma", "no-cache"); response.setDateHeader("Expires", 0); // 链接来源地址,通过获取请求头 referer 得到 String referer = request.getHeader("referer"); System.out.println("获取的来源--->: " + referer); if (referer == null || !referer.contains(request.getServerName())) {//本站点访问,则有效 /** * 如果 链接地址来自其他网站,则返回错误图片 */ request.getRequestDispatcher("/error.gif").forward(request, response); } else { /** * 图片正常显示 */ chain.doFilter(request, response); } } public void destroy() { } } |
2、web.xml 配置如下:
|
(三)权限校验 Filter
1、为了方便,权限配置在文件中:
import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class PrivilegeFilter implements Filter { private Properties pp = new Properties();//读取配置文件 public void init(FilterConfig config) throws ServletException { // 从 初始化参数 中获取权 限配置文件 的位置 String file = config.getInitParameter("file"); String realPath = config.getServletContext().getRealPath(file); try { pp.load(new FileInputStream(realPath)); } catch (Exception e) { config.getServletContext().log("读取权限控制文件失败。", e); } } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; // 获取访问的路径,例如:admin.jsp String requestURI = request.getRequestURI().replace( request.getContextPath() + "/", ""); // 获取 action 参数,例如:add String action = req.getParameter("action"); action = action == null ? "" : action; // 拼接成 URI。例如:log.do?action=list String uri = requestURI + "?action=" + action; // 从 session 中获取用户权限角色。 String role = (String) request.getSession(true).getAttribute("role"); role = role == null ? "guest" : role; boolean authentificated = false; // 开始检查该用户角色是否有权限访问 uri for (Object obj : pp.keySet()) { String key = ((String) obj); // 使用正则表达式验证 需要将 ? . 替换一下,并将通配符 * 处理一下 if (uri.matches(key.replace("?", "\\?").replace(".", "\\.") .replace("*", ".*"))) { // 如果 role 匹配 if (role.equals(pp.get(key))) { authentificated = true; break; } } } if (!authentificated) { System.out.println("您无权访问该页面。请以合适的身份登陆后查看。"); } // 继续运行 chain.doFilter(req, res); } public void destroy() { pp = null; } } |
2、web.xml配置如下:
servlet.filter.PrivilegeFilter |
3、权限配置如下:
# Privilege Settings admin.do?action\=* = administrator log.do?action\=* = administrator list.do?action\=add = member list.do?action\=delete = member list.do?action\=save = member list.do?action\=view = guest list.do?action\=list = guest |
(四)GZIP 压缩 Filter
1、使用servlet 的对响应内容进行压缩:
private void GZipTest(HttpServletResponse response) throws IOException { //实现压缩 String tDate = "准备被压缩的数据"; System.out.println("压缩前的数据大小: "+tDate.getBytes().length); ByteArrayOutputStream bout = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(bout); gout.write(tDate.getBytes()); gout.flush(); gout.finish(); gout.close();//写到字节数组流中 byte[] gzip = bout.toByteArray();//得到压缩后的数据 System.out.println("压缩后的数据大小: "+gzip.length); // 通知浏览器数据采用压缩格式 response.setHeader("Content-Encoding", "gzip");//压缩格式 response.setHeader("Content-Length",gzip.length+"" );//压缩数据的长度 response.getOutputStream().write(gzip); } |
2、使用过滤器代码:
1)GZIP 压缩的核心是 JDK 自带的压缩数据的类,GZIPOutputStream 。
2)响应头:Content-Encoding 和 Content-Length 。
3)GZipResponseWrapper 类为自定义的 Response 类,内部对输出的内容进行 GZIP 的压缩。
3、代码如下:
import java.io.IOException; |
package servlet.filter.gzip; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; /** * * GZipResponseWrapper.java * * @title 封装的Response ,不会真正输出到客户端 * 继承 HttpServletResponseWrapper,其实现了 HttpServletResponse 接口 * @description * @author SAM-SHO * @Date 2014-12-9 */ public class GZipResponseWrapper extends HttpServletResponseWrapper { // 默认的 response private HttpServletResponse response; // 自定义的 outputStream, 执行close()的时候对数据压缩,并输出 private GZipOutputStream gzipOutputStream; // 自定义 printWriter,将内容输出到 GZipOutputStream 中 private PrintWriter writer; public GZipResponseWrapper(HttpServletResponse response) throws IOException { super(response); this.response = response; } @Override public ServletOutputStream getOutputStream() throws IOException { if (gzipOutputStream == null) gzipOutputStream = new GZipOutputStream(response); return gzipOutputStream; } @Override public PrintWriter getWriter() throws IOException { if (writer == null) writer = new PrintWriter(new OutputStreamWriter( new GZipOutputStream(response), "UTF-8")); return writer; } // 压缩后数据长度会发生变化 因此将该方法内容置空 @Override public void setContentLength(int contentLength) { } @Override public void flushBuffer() throws IOException { gzipOutputStream.flush(); } public void finishResponse() throws IOException { if (gzipOutputStream != null) gzipOutputStream.close(); if (writer != null) writer.close(); } } |
package servlet.filter.gzip; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; /** * * GZipOutputStream.java * * @title 自定义的压缩流,内部调用JDK自带的压缩流 * @description * @author SAM-SHO * @Date 2014-12-9 */ public class GZipOutputStream extends ServletOutputStream { private HttpServletResponse response; // JDK 自带的压缩数据的类 private GZIPOutputStream gzipOutputStream; // 将压缩后的数据存放到 ByteArrayOutputStream 对象中 private ByteArrayOutputStream byteArrayOutputStream; public GZipOutputStream(HttpServletResponse response) throws IOException { this.response = response; byteArrayOutputStream = new ByteArrayOutputStream(); gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream); } @Override public void write(int b) throws IOException { gzipOutputStream.write(b); } @Override public void close() throws IOException { // 压缩完毕 一定要调用该方法 gzipOutputStream.finish(); // 将压缩后的数据输出到客户端 byte[] content = byteArrayOutputStream.toByteArray(); // 设定压缩方式为 GZIP, 客户端浏览器会自动将数据解压 response.addHeader("Content-Encoding", "gzip"); response.addHeader("Content-Length", Integer.toString(content.length)); // 输出 ServletOutputStream out = response.getOutputStream(); out.write(content); out.close(); } @Override public void flush() throws IOException { gzipOutputStream.flush(); } @Override public void write(byte[] b, int off, int len) throws IOException { gzipOutputStream.write(b, off, len); } @Override public void write(byte[] b) throws IOException { gzipOutputStream.write(b); } } |
4、web.xml配置如下:
|
(四)文件上传 Filter
1、上传文件,修改