WEB应用中的过滤器截取从客户端进来的请求,并做出处理的回答。它可以说是外部进入 网站的第一道关。在这个头目里,可以验证客户是否来自枳的网络,可以对客户提交的数据进行 重新编码,可以系统里获得配置的信息,可以过滤掉客户的某些不应出现的语汇,可以验证客户是否已经 登录,可以验证客户端的浏览器是否支持当前应用 ,可以记录系统日志等。
可以为一个WEB应用组件 部署多个过滤器,这过滤器组成一个过滤链,每个过滤器只执行某个特定的操作或者检查。这样请求在达到被访问的目标之前,需要经过这个过滤链。如果由于安全的问题不能访问目标资源,那么过滤器就可以 把客户端的请求拦截。
Filter的开发
在应用开发中,我们经常使用的过滤功能就是对客户端的请求进行统一编码,对客户端进行认证,。下面我们做个例子,假设客户端要访问一个secutiry/index.jsp页面。这个页面 接收一些参数 ,这些参数 需要保存到数据库中,为了在数据库中使用相同的编码 ,可以开发一个过滤器对请求进行 统一编码 。secutiry目录下的所有页面 都是受限制的页面 ,只有通过授权 的用户才让访问,如果按照平常的方式,在每个页面 都对用户进行 授权 , 这样不权编程困难,而且代码 的修改也很困难 ,这里来开发一个进行 安全的检查过滤器。
package
eflylab;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
import
javax.servlet.FilterChain;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
java.io.IOException;
import
javax.servlet.Filter;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
javax.servlet.ServletException;
import
javax.servlet.FilterConfig;
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** */
/**
* 要开发一个Filter,必须直接或间接实现Filter接口。
* @author Administrator
*
*/
public
class
EncodingFilter
implements
Filter
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
protected FilterConfig filterConfig;
private String targetEncoding = "gb2312";
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
*初始化过滤器,和一般的Servlet一样,它也可以获得初始参数。
*/
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void init(FilterConfig config) throws ServletException
{
this.filterConfig = config;
this.targetEncoding = config.getInitParameter("encoding");//获取FilterConfig中的参数
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
*进行过滤处理,这个方法最重要,所有过滤处理的代码都在此实现。
*/
public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain chain)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws IOException, ServletException
{
System.out.println("使用以下方法对请求进行编码:encoding="+targetEncoding);
HttpServletRequest request = (HttpServletRequest)srequest;
request.setCharacterEncoding(targetEncoding);
// 把处理权发送到下一个
chain.doFilter(srequest,sresponse);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void setFilterConfig(final FilterConfig filterConfig)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.filterConfig=filterConfig;
}
//销毁过滤器
public void destroy()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.filterConfig=null;
}
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在
Servlet常用接口中知道doFilter是过滤器中最重要的方法,通过这个方法 进行 实际的业务处理,在foFilter方法中,首先获得HTTP的请求和响应对象,然后对HTTP的请求和响应对象,然后 对HTTP请求中的参数 进行 统一编码,这里采用的编码 方式是GB2312。进行 编码 后,就把控制权转让给下一个过滤器。如果在这个过滤链中没有下一个过滤器,那么 就会把请求直接发送到目标 。
配置
Filter描述
<
filter
>
<
filter-name
>
encoding
</
filter-name
>
<
filter-class
>
eflylab.EncodingFilter
</
filter-class
>
<
init-param
>
<
param-name
>
encoding
</
param-name
>
<
param-value
>
gb2312
</
param-value
>
</
init-param
>
</
filter
>
下面我们来看一另一个Filter的开发,这个就是前面说到的用于对用户的访问认证。如果认证通过,那么 允许访问 指定资源,否则不行。
package
eflylab;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
import
javax.servlet.FilterChain;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
java.io.IOException;
import
javax.servlet.Filter;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
javax.servlet.ServletException;
import
javax.servlet.FilterConfig;
import
javax.servlet.http.HttpSession;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
class
SignonFilter
implements
Filter
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
String LOGIN_PAGE="login_signon.jsp";
protected FilterConfig filterConfig;
//过滤处理的方法
public void doFilter(final ServletRequest req,final ServletResponse res,FilterChain chain)throws IOException,ServletException
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpServletRequest hreq = (HttpServletRequest)req;
HttpServletResponse hres = (HttpServletResponse)res;
HttpSession session = hreq.getSession();
String isLogin="";
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
isLogin=(String)session.getAttribute("isLogin");
if(isLogin.equals("true"))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.out.println("在SignonFilter中验证通过");
//验证成功,继续处理
chain.doFilter(req,res);
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//验证不成功,让用户登录。
hres.sendRedirect(LOGIN_PAGE);
System.out.println("被SignonFilter拦截一个未认证的请求");
}
}
catch(Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.printStackTrace();
}
}
public void setFilterConfig(final FilterConfig filterConfig)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.filterConfig=filterConfig;
}
//销毁过滤器
public void destroy()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.filterConfig=null;
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
*初始化过滤器,和一般的Servlet一样,它也可以获得初始参数。
*/
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void init(FilterConfig config) throws ServletException
{
this.filterConfig = config;
}
}
在上面的代码中,首先通过isLogin = (String)session.getAttribute("isLogin")获得用户是否登录 的属性。如果 为 真,则可以访问到目标 资源,否则 转向 login_singnon.jsp
配置
<?
xml version="1.0" encoding="UTF-8"
?>
<
web-app
version
="2.4"
xmlns
="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
listener
>
<
listener-class
>
eflylab.SessionListener
</
listener-class
>
</
listener
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
filter
>
<
filter-name
>
encoding
</
filter-name
>
<
filter-class
>
eflylab.EncodingFilter
</
filter-class
>
<
init-param
>
<
param-name
>
encoding
</
param-name
>
<
param-value
>
gb2312
</
param-value
>
</
init-param
>
</
filter
>
<
filter
>
<
filter-name
>
auth
</
filter-name
>
<
filter-class
>
eflylab.SignonFilter
</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
encoding
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
<!--
表示对于encoding 任何请求都会进行 该 过滤
-->
<
filter-mapping
>
<
filter-name
>
auth
</
filter-name
>
<
url-pattern
>
/security/*
</
url-pattern
>
</
filter-mapping
>
<
filter-mapping
>
<
filter-name
>
auth
</
filter-name
>
<
url-pattern
>
/admin/*
</
url-pattern
>
</
filter-mapping
>
</
web-app
>