簡介
過濾器是處在客戶端和服務器資源之間的一到過濾網,我們可以根據具體的需求來對請求頭和數據就行預處理,也可以對響應頭和和數據進行后處理。例如Jsp, Servlet, 靜態圖片文件或靜態 html 文件等進行攔截,從而實現一些特殊的功能。例如實現URL級別的權限訪問控制、過濾敏感詞匯、壓縮響應信息等一些高級功能
生命周期
filter的創建和銷毀都是有服務器負責的。web服務器在啟動時,創建filter實例對象,並調用其init方法,讀取web.xml的配置,完成對象的初始化工作,為后續的用戶請求做好攔截的准備工作。開發人員通過init方法的參數可以獲取代表當前filter信息的FilterConfig對象。
注意事項
- init,detroy方法都是執行一次。在服務器啟動時會執行init方法,初始化數據;在服務器停止前會執行detroy方法,釋放filter所占用的資源。
- 多個filter有執行順序,執行順序就是filter在web.xml中的配置順序
- 一個filter可以對應多個filter-mapping
使用場景
登錄權限,編碼設置,頁面緩存,響應數據壓縮
基本使用
- 先配置web.xml
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>com.action.login.SessionFilter</filter-class>
<init-param>
<param-name>logonStrings</param-name><!-- 對登錄頁面不進行過濾 -->
<param-value>/project/index.jsp;login.do</param-value>
</init-param>
<init-param>
<param-name>includeStrings</param-name><!-- 只對指定過濾參數后綴進行過濾 -->
<param-value>.do;.jsp</param-value>
</init-param>
<init-param>
<param-name>redirectPath</param-name><!-- 未通過跳轉到登錄界面 -->
<param-value>/index.jsp</param-value>
</init-param>
<init-param>
<param-name>disabletestfilter</param-name><!-- Y:過濾無效 -->
<param-value>N</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- filter類代碼
package com.action.login;
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;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* 判斷用戶是否登錄,未登錄則退出系統
*/
public class SessionFilter implements Filter {
public FilterConfig config;
public void destroy() {
this.config = null;
}
public static boolean isContains(String container, String[] regx) {
boolean result = false;
for (int i = 0; i < regx.length; i++) {
if (container.indexOf(regx[i]) != -1) {
return true;
}
}
return result;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest hrequest = (HttpServletRequest)request;
HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) response);
String logonStrings = config.getInitParameter("logonStrings"); // 登錄登陸頁面
String includeStrings = config.getInitParameter("includeStrings"); // 過濾資源后綴參數
String redirectPath = hrequest.getContextPath() + config.getInitParameter("redirectPath");// 沒有登陸轉向頁面
String disabletestfilter = config.getInitParameter("disabletestfilter");// 過濾器是否有效
if (disabletestfilter.toUpperCase().equals("Y")) { // 過濾無效
chain.doFilter(request, response);
return;
}
String[] logonList = logonStrings.split(";");
String[] includeList = includeStrings.split(";");
if (!this.isContains(hrequest.getRequestURI(), includeList)) {// 只對指定過濾參數后綴進行過濾
chain.doFilter(request, response);
return;
}
if (this.isContains(hrequest.getRequestURI(), logonList)) {// 對登錄頁面不進行過濾
chain.doFilter(request, response);
return;
}
String user = ( String ) hrequest.getSession().getAttribute("useronly");//判斷用戶是否登錄
if (user == null) {
wrapper.sendRedirect(redirectPath);
return;
}else {
chain.doFilter(request, response);
return;
}
}
public void init(FilterConfig filterConfig) throws ServletException {
config = filterConfig;
}
}
