Filter详解

参考网页:http://blog.csdn.net/zhaozheng7758/article/details/6105749

设置拦截器,需要做两步操作:

1)在web.xml中配置Filter

<filter>
  <display-name>SsoFilter</display-name>
  <filter-name>SsoFilter</filter-name>   //Filter的名字
  <filter-class>com.cares.asis.common.SsoFilter</filter-class>  //Filter的实现类
  <init-param>
    <param-name>userNotExistUrl</param-name>   //Filter中配置初始化参数userNotExistUrl
    <param-value>/common/user_notexist.jsp</param-value>   //参数值
  </init-param>
  <init-param>
    <param-name>errorUrl</param-name>          //Filter中配置初始化参数errorUrl
    <param-value>/common/error.jsp</param-value>    //参数值
  </init-param>
</filter>

<filter-mapping>
  <filter-name>SsoFilter</filter-name>    //Filter的名字
  <url-pattern>/*</url-pattern>         //Filter负责拦截的URL,这里*是拦截所有的地址
</filter-mapping>

2)创建Filter处理类

 

public class SsoFilter implements Filter {
private String userNotExistUrl;
private String errorUrl;
private UserService userService;
private MenuService menuService;
private SystemCacheService systemCacheService;
private RoleService roleService;
private static final String CONFIG_PROPERTIES_PATH = "/baseconfig.properties";
private static final String IS_SSO = "is.sso";
private static PropertiesUtil prop = new PropertiesUtil(CONFIG_PROPERTIES_PATH);

 

//destroy()用于Filter 销毁前,完成某些资源的回收

@Override
public void destroy() {
// TODO Auto-generated method stub

}

 

//doFilter()实现过滤功能,对每个请求及响应增加的额外处理

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String uri = req.getRequestURI();
if (exclude(uri)) {
  chain.doFilter(request, response);      // Filter 只是链式处理,请求依然转发到目的地址
} else {

// 跳转到“拒绝进入系统”提示页
request.getRequestDispatcher(errorUrl).forward(req, res);  //Filter可以改变请求和响应的内容
}
}
}

 

//init()用于完成Filter 的初始化

@Override
public void init(FilterConfig arg0) throws ServletException {

  //在web.xml中设置的初始化参数,通过FilterConfig中的getInitParameter(“param-name”)方法可以得到

  userNotExistUrl = arg0.getInitParameter("userNotExistUrl");  

  errorUrl = arg0.getInitParameter("errorUrl");
 // userService = (UserService) SystemBeanFactory.getBean("userService");
 // menuService = (MenuService) SystemBeanFactory.getBean("menuService");
 // systemCacheService = (SystemCacheService) SystemBeanFactory.getBean("systemCacheService");
 // roleService = (RoleService) SystemBeanFactory.getBean("roleService");
}

private boolean exclude(String uri) {
if (uri.endsWith("js")) {
return true;
}
if (uri.endsWith("doc")) {
return true;
}
if (uri.endsWith("docx")) {
return true;
}
if (uri.endsWith("zip")) {
return true;
}
if (uri.endsWith("jsp") || uri.endsWith("html")) {
return true;
}
if (uri.endsWith("css")) {
return true;
}
if (uri.endsWith("png") || uri.endsWith("jpg")) {
return true;
}
if (uri.indexOf("cs") != -1) {
return true;
}
if (uri.contains("login.jsp") || uri.contains("login.do")) {
return true;
}
if (!Boolean.parseBoolean(prop.getProperty(IS_SSO))) {
return true;
}
return false;
}
}

 

posted @ 2016-08-10 15:49  尼古丁在回归的路上  阅读(308)  评论(0编辑  收藏  举报