zoukankan      html  css  js  c++  java
  • 关于filter用户授权的例子

    在jsp中权限的控制是通过Filter过滤器来实现的,所有的开发框架中都集成有Filter,如果不适用开发框架则有如下实现方法:

    LoginFilter.java

    复制代码 代码如下:

    public class LoginFilter implements Filter { 

        private String permitUrls[] = null; 

        private String gotoUrl = null; 

        public void destroy() { 

            // TODO Auto-generated method stub 

            permitUrls = null; 

            gotoUrl = null; 

        } 

        public void doFilter(ServletRequest request, ServletResponse response, 

                FilterChain chain) throws IOException, ServletException { 

            // TODO Auto-generated method stub 

            HttpServletRequest res=(HttpServletRequest) request; 

            HttpServletResponse resp=(HttpServletResponse)response; 

            if(!isPermitUrl(request)){ 

                if(filterCurrUrl(request)){ 

                    System.out.println("--->请登录"); 

                    resp.sendRedirect(res.getContextPath()+gotoUrl); 

                    return; 

                } 

            } 

            System.out.println("--->允许访问"); 

            chain.doFilter(request, response); 

        } 

        public boolean filterCurrUrl(ServletRequest request){ 

            boolean filter=false; 

            HttpServletRequest res=(HttpServletRequest) request; 

            User user =(User) res.getSession().getAttribute("user"); 

            if(null==user) 

                filter=true; 

            return filter; 

        }       

        public boolean isPermitUrl(ServletRequest request) { 

            boolean isPermit = false; 

            String currentUrl = currentUrl(request); 

            if (permitUrls != null && permitUrls.length > 0) { 

                for (int i = 0; i < permitUrls.length; i++) { 

                    if (permitUrls[i].equals(currentUrl)) { 

                        isPermit = true; 

                        break; 

                    } 

                } 

            } 

            return isPermit; 

        }        

        //请求地址 

        public String currentUrl(ServletRequest request) {   

            HttpServletRequest res = (HttpServletRequest) request; 

            String task = request.getParameter("task"); 

            String path = res.getContextPath(); 

            String uri = res.getRequestURI(); 

            if (task != null) {// uri格式 xx/ser 

                uri = uri.substring(path.length(), uri.length()) + "?" + "task="

                        + task; 

            } else { 

                uri = uri.substring(path.length(), uri.length()); 

            } 

            System.out.println("当前请求地址:" + uri); 

            return uri; 

        } 

        public void init(FilterConfig filterConfig) throws ServletException { 

            // TODO Auto-generated method stub 

            String permitUrls = filterConfig.getInitParameter("permitUrls"); 

            String gotoUrl = filterConfig.getInitParameter("gotoUrl"); 

      

            this.gotoUrl = gotoUrl; 

      

            if (permitUrls != null && permitUrls.length() > 0) { 

                this.permitUrls = permitUrls.split(","); 

            } 

        } 

    }


    Web.xml

    复制代码 代码如下:


    <filter> 

        <filter-name>loginFilter</filter-name> 

        <filter-class>filter.LoginFilter</filter-class> 

      

        <init-param> 

            <param-name>ignore</param-name> 

            <param-value>false</param-value> 

        </init-param> 

        <init-param> 

            <param-name>permitUrls</param-name> 

            <param-value>/,/servlet/Loginservlet?task=login,/public.jsp,/login.jsp</param-value> 

        </init-param> 

        <init-param> 

            <param-name>gotoUrl</param-name> 

            <param-value>/login.jsp</param-value> 

        </init-param> 

    </filter> 

    <filter-mapping> 

        <filter-name>loginFilter</filter-name> 

        <url-pattern>/*</url-pattern> 

    </filter-mapping>

  • 相关阅读:
    log4j 配置详解
    log4j2单独的配置与使用&log4j2+slf4j的结合的配置与使用
    jdk时区相差8小时
    javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/el/ELResolver错误解决办法
    SqlServer 统计用户量实例(按年,月,日分组)
    sqlserver with as 双向递归
    eclipse启动无响应,停留在Loading workbench状态
    JS制作闪动的图片
    查询数据库中表名和扩展属性
    sql 查询除某字段的其他字段的记录
  • 原文地址:https://www.cnblogs.com/Quire/p/8645895.html
Copyright © 2011-2022 走看看