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;
    }
    }

  • 相关阅读:
    git命令
    基于babel实现react核心功能(初始化,fiber,hook)
    Vue组件化原理-Xmind版
    访问后台 出现 俩次请求拼接情况 例如 https://localhost:4431/api/auth/jwt/token+https://localhost:4431/api/auth/jwt/token
    spring mvc 拦截器和过滤器
    前后端分离,session登录实例,jquery版本必须大于1.5,否则withCredentials不起作用
    kafka batches 数据结构是自定义map
    数据库blob中文乱码,如何查看
    先更新数据库 后删缓存
    高老师好
  • 原文地址:https://www.cnblogs.com/ccEmily/p/5757062.html
Copyright © 2011-2022 走看看