天天看點

java過濾器的原理_Java 三大器之過濾器(Filter)工作原理

Filter 簡介

filter稱為過濾器,可以對web 伺服器管理的所有資源: Jsp, Servlet,靜态圖檔檔案或靜态html檔案等進行攔截,進而實作一些特殊的功能。例如實作URL級别的權限通路控制,過濾敏感詞彙,壓縮響應資訊等進階功能。

public class LogCostFilter implements Filter {

public abstract void init(FilterConfig paramFilterConfig) throws ServletException;

public abstract void doFilter(ServletRequest paramServletRequest, ServletResponse paramServletResponse, FilterChain

paramFilterChain) throws IOException, ServletException;

public abstract void destroy();

}

工作原理

doFilter方法中,配置對哪個web 資源進行攔截後,WEB伺服器每次在調用web資源service方法之前,都會先調用一下filter的doFilter方法,是以,在該方法内編寫代碼可以達到此目的:

調用目标資源之前,讓一段代碼執行

是否調用目标資源(即是否讓使用者通路web 資源)

調用目标資源之後,讓一段代碼執行(filterChian)

Demo

LogCostFilter

public class LogCostFilter implements Filter {

@Override

public void init(FilterConfig filterConfig) throws ServletException {

System.out.println("---Filter初始化---");

}

@Override

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

servletRequest.setCharacterEncoding("UTF-8");

servletResponse.setContentType("text/html;charset=UTF-8");

long start = System.currentTimeMillis();

System.out.println("---調用service之前執行的一段代碼---");

filterChain.doFilter(servletRequest, servletResponse);

System.out.println("Execute cost="+(System.currentTimeMillis()-start));

}

@Override

public void destroy() {

System.out.println("---Filter銷毀---");

}

}

FilterConfig

@Configuration

public class FilterConfig {

@Bean

public FilterRegistrationBean registFilter() {

FilterRegistrationBean registrationBean = new FilterRegistrationBean();

registrationBean.setFilter(new LogCostFilter());

registrationBean.addUrlPatterns("/*");

registrationBean.setName("LogCostFilter");

registrationBean.setOrder(1);

return registrationBean;

}

}

過濾字元串标準代碼

public class CharacterEncodingFilter extends OncePerRequestFilter{

private static final boolean responseSetCharacterEncodingAvailable = ClassUtils.hasMethod(

HttpServletResponse.class, "setCharacterEncoding", new Class[] { String.class });

// 需要設定的編碼方式,為了支援可配置,Spring把編碼方式設定成了一個變量

private String encoding;

// 是否強制使用統一編碼,也是為了支援可配置

private boolean forceEncoding;

// 構造器,在這裡,Spring把forceEncoding的值預設設定成了false

public CharacterEncodingFilter(){

this.forceEncoding = false;

}

// encoding/forceEncoding的setter方法

public void setEncoding(String encoding){

this.encoding = encoding;

}

public void setForceEncoding(boolean forceEncoding){

this.forceEncoding = forceEncoding;

}

// Spring通過GenericFilterBean抽象類,對Filter接口進行了整合,

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)

throws ServletException, IOException{

if ((this.encoding != null) && (((this.forceEncoding) || (request.getCharacterEncoding() == null)))) {

request.setCharacterEncoding(this.encoding);

if ((this.forceEncoding) && (responseSetCharacterEncodingAvailable)) {

response.setCharacterEncoding(this.encoding);

}

}

filterChain.doFilter(request, response);

}

}

上一篇: 使用斷言