zoukankan      html  css  js  c++  java
  • 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;
    }
    }

  • 相关阅读:
    P1613 跑路
    数据挖掘-聚类分析(Python实现K-Means算法)
    使用scikit-learn 估计器分类
    数据挖掘-集成学习
    数据挖掘-关联分析 Apriori算法和FP-growth 算法
    scikit-learn 中常用的评估模型
    数据挖掘---支持向量机(SVM)
    数据挖掘-KNN-K最近邻算法
    数据挖掘-决策树
    数据挖掘-逻辑Logistic回归
  • 原文地址:https://www.cnblogs.com/ccEmily/p/5757062.html
Copyright © 2011-2022 走看看