`

使用过滤器和拦截器做访问权限限制

阅读更多

Struts2项目通过使用Struts的if标签进行了session判断,使得未登录的用户不能看到页面,但是这 种现仅仅在view层进行,如果未登录用户直接在地址栏输入登录用户才能访问的地址,那么相应的action还是会执行,仅仅是不让用户看到罢了。这样显然是不好的,所以研究了一下Struts2的权限验证。

权限最核心的是业务逻辑,具体用什么技术来实现就简单得多。 
通常:用户与角色建立多对多关系,角色与业务模块构成多对多关系,权限管理在后者关系中。 
对权限的拦截,如果系统请求量大,可以用Struts2拦截器来做,请求量小可以放在filter中。但一般单级拦截还不够,要做到更细粒度的权限控制,还需要多级拦截。

    不大理解filter(过滤器)和interceptor(拦截器)的区别,遂google之。博文中有介绍:

1、拦截器是基于java的反射机制的,而过滤器是基于函数回调 。 
2、过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 。 
3、拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求 起作用 。 
4、拦截器可以访问action上下文、值栈里的对象,而过滤器不能 。 
5、在action的生命周期中,拦截器可以多次被调用,而过滤器只能在容 器初始化时被调用一次 。

    为了学习决定把两种实现方式都试一下,然后再决定使用哪个。

权限验证的Filter实现:

web.xml代码片段

  

[html] view plaincopy
 
  1. <!-- authority filter 最好加在Struts2的Filter前面-->  
  2.   <filter>  
  3.     <filter-name>SessionInvalidate</filter-name>  
  4.     <filter-class>filter.SessionCheckFilter</filter-class>  
  5.     <init-param>  
  6.       <param-name>checkSessionKey</param-name>  
  7.       <param-value>loginName</param-value>  
  8.     </init-param>  
  9.     <init-param>  
  10.       <param-name>redirectURL</param-name>  
  11.       <param-value>/entpLogin.jsp</param-value>  
  12.     </init-param>  
  13.     <init-param>  
  14.       <param-name>notCheckURLList</param-name>  
  15.       <param-value>/entpLogin.jsp,/rois/loginEntp.action,/entpRegister.jsp,/test.jsp,/rois/registerEntp.action</param-value>  
  16.     </init-param>  
  17.   </filter>  
  18.   <!--过滤/rois命名空间下所有action  -->  
  19.   <filter-mapping>  
  20.     <filter-name>SessionInvalidate</filter-name>  
  21.     <url-pattern>/rois/*</url-pattern>  
  22.   </filter-mapping>  
  23.   <!--过滤/jsp文件夹下所有jsp  -->  
  24.   <filter-mapping>  
  25.     <filter-name>SessionInvalidate</filter-name>  
  26.     <url-pattern>/jsp/*</url-pattern>  
  27.   </filter-mapping>  



SessionCheckFilter.java代码

[java] view plaincopy
 
  1. package filter;  
  2. import java.io.IOException;  
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5. import javax.servlet.Filter;  
  6. import javax.servlet.FilterChain;  
  7. import javax.servlet.FilterConfig;  
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.ServletRequest;  
  10. import javax.servlet.ServletResponse;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13. import javax.servlet.http.HttpSession;  
  14. /** 
  15.  * 用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面 配置参数 checkSessionKey 需检查的在 Session 中保存的关键字 
  16.  * redirectURL 如果用户未登录,则重定向到指定的页面,URL不包括 ContextPath notCheckURLList 
  17.  * 不做检查的URL列表,以分号分开,并且 URL 中不包括 ContextPath 
  18.  */  
  19. public class SessionCheckFilter implements Filter {  
  20.   protected FilterConfig filterConfig = null;  
  21.   private String redirectURL = null;  
  22.   private Set<String> notCheckURLList = new HashSet<String>();  
  23.   private String sessionKey = null;  
  24.   @Override  
  25.   public void destroy() {  
  26.     notCheckURLList.clear();  
  27.   }  
  28.   @Override  
  29.   public void doFilter(ServletRequest servletRequest,  
  30.       ServletResponse servletResponse, FilterChain filterChain)  
  31.       throws IOException, ServletException {  
  32.     HttpServletRequest request = (HttpServletRequest) servletRequest;  
  33.     HttpServletResponse response = (HttpServletResponse) servletResponse;  
  34.     HttpSession session = request.getSession();  
  35.     if (sessionKey == null) {  
  36.       filterChain.doFilter(request, response);  
  37.       return;  
  38.     }  
  39.     if ((!checkRequestURIIntNotFilterList(request))  
  40.         && session.getAttribute(sessionKey) == null) {  
  41.       response.sendRedirect(request.getContextPath() + redirectURL);  
  42.       return;  
  43.     }  
  44.     filterChain.doFilter(servletRequest, servletResponse);  
  45.   }  
  46.   private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) {  
  47.     String uri = request.getServletPath()  
  48.         + (request.getPathInfo() == null ? "" : request.getPathInfo());  
  49.     String temp = request.getRequestURI();  
  50.     temp = temp.substring(request.getContextPath().length() + 1);  
  51.     // System.out.println("是否包括:"+uri+";"+notCheckURLList+"=="+notCheckURLList.contains(uri));  
  52.     return notCheckURLList.contains(uri);  
  53.   }  
  54.   @Override  
  55.   public void init(FilterConfig filterConfig) throws ServletException {  
  56.     this.filterConfig = filterConfig;  
  57.     redirectURL = filterConfig.getInitParameter("redirectURL");  
  58.     sessionKey = filterConfig.getInitParameter("checkSessionKey");  
  59.     String notCheckURLListStr = filterConfig  
  60.         .getInitParameter("notCheckURLList");  
  61.     if (notCheckURLListStr != null) {  
  62.       System.out.println(notCheckURLListStr);  
  63.       String[] params = notCheckURLListStr.split(",");  
  64.       for (int i = 0; i < params.length; i++) {  
  65.         notCheckURLList.add(params[i].trim());  
  66.       }  
  67.     }  
  68.   }  
  69. }  


权限验证的Interceptor实现:

   使用Interceptor不需要更改web.xml,只需要对struts.xml进行配置

struts.xml片段

[html] view plaincopy
 
  1. <!-- 用户拦截器定义在该元素下 -->  
  2.     <interceptors>  
  3.       <!-- 定义了一个名为authority的拦截器 -->  
  4.       <interceptor name="authenticationInterceptor" class="interceptor.AuthInterceptor" />  
  5.       <interceptor-stack name="defualtSecurityStackWithAuthentication">  
  6.         <interceptor-ref name="defaultStack" />  
  7.         <interceptor-ref name="authenticationInterceptor" />  
  8.       </interceptor-stack>  
  9.     </interceptors>  
  10.     <default-interceptor-ref name="defualtSecurityStackWithAuthentication" />  
  11.     <!-- 全局Result -->  
  12.     <global-results>  
  13.       <result name="error">/error.jsp</result>  
  14.       <result name="login">/Login.jsp</result>  
  15.     </global-results>  
  16.     <action name="login" class="action.LoginAction">  
  17.       <param name="withoutAuthentication">true</param>  
  18.       <result name="success">/WEB-INF/jsp/welcome.jsp</result>  
  19.       <result name="input">/Login.jsp</result>  
  20.     </action>  
  21.     <action name="viewBook" class="action.ViewBookAction">  
  22.         <result name="sucess">/WEB-INF/viewBook.jsp</result>  
  23.     </action>  


AuthInterceptor.java代码

[java] view plaincopy
 
  1. package interceptor;  
  2. import java.util.Map;  
  3. import com.opensymphony.xwork2.Action;  
  4. import com.opensymphony.xwork2.ActionContext;  
  5. import com.opensymphony.xwork2.ActionInvocation;  
  6. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  7. public class AuthInterceptor extends AbstractInterceptor {  
  8.   private static final long serialVersionUID = -5114658085937727056L;  
  9.   private String sessionKey="loginName";  
  10.   private String parmKey="withoutAuthentication";  
  11.   private boolean excluded;  
  12.   @Override  
  13.   public String intercept(ActionInvocation invocation) throws Exception {  
  14.       
  15.     ActionContext ac=invocation.getInvocationContext();  
  16.     Map<?, ?> session =ac.getSession();  
  17.     String parm=(String) ac.getParameters().get(parmKey);  
  18.       
  19.     if(parm!=null){  
  20.       excluded=parm.toUpperCase().equals("TRUE");  
  21.     }  
  22.       
  23.     String user=(String)session.get(sessionKey);  
  24.     if(excluded || user!=null){  
  25.       return invocation.invoke();  
  26.     }  
  27.     ac.put("tip""您还没有登录!");  
  28.     //直接返回 login 的逻辑视图    
  29.         return Action.LOGIN;   
  30.   }  
  31. }  

 

 

使用自定义的default-interceptor的话有需要注意几点:

1.一定要引用一下Sturts2自带defaultStack。否则会用不了Struts2自带的拦截器。

2.一旦在某个包下定义了上面的默认拦截器栈,在该包下的所有 Action 都会自动增加权限检查功能。所以有可能会出现永远登录不了的情况。

解决方案:

1.像上面的代码一样,在action里面增加一个参数表明不需要验证,然后在interceptor实现类里面检查是否不需要验证

2.将那些不需要使用权限控制的 Action 定义在另一个包中,这个新的包中依然使用 Struts 2 原有的默认拦截器栈,将不会有权限控制功能。

3.Interceptor是针对action的拦截,如果知道jsp地址的话在URL栏直接输入JSP的地址,那么权限验证是没有效果滴!

解决方案:把所有page代码(jsp)放到WEB-INF下面,这个目录下的东西是“看不见”的

分享到:
评论

相关推荐

    spring security 参考手册中文版

    13.5与其他基于过滤器的框架一起使用 118 13.6高级命名空间配置 118 14.核心安全筛选器 119 14.1 FilterSecurityInterceptor 119 14.2 ExceptionTranslationFilter 121 14.2.1 AuthenticationEntryPoint 122 14.2.2 ...

    令人难以置信的StartPage -高效的开始页面。「Incredible StartPage - Productive Start Page」-crx插件

    权限用于搜索适合的图像作为背景已知问题和局限性-某些小书签可能无法在StartPage内运行-如果使用广告拦截器,则可能需要在StartPage中将其禁用(将“ newtab”和“ ncdfeghkpohnalmpblddmnppfooljekh”添加到白名单...

    计算机网络安全问题分析.doc

    防火墙技术作为一种根本的应用,在过滤 网络平安攻击方面起到了非常积极的作用,但是由于技术本身的局限性,防火墙的使用 ,不仅给网络带来一些不便,例如限制一些有用的网络效劳,在并发请求多、流量大的 情况下...

    网路岗7.03.35官方原版破解

    Windows XP 和 Windows Me 中的“Internet 连接共享”及许多 Internet 网关设备都使用 NAT,尤其是在通过 DSL 或电缆调制解调器连接宽带网的情况下。 NAT 对于解决 IPv4 地址耗费问题(在 IPv6 部署中却没必要)...

    ASP ISchool随机抽题考试系统

    2.导出内容改为先生成文件后导出,完美解决迅雷拦截问题 3.增加重考次数限制功能 4.修正考生无法修改的问题 V3.6.2(20110809) 1.增加试题导入题干和选项换行符“[换行]” 2.增加试题导入题干结束符“|题干结束|”...

    Java学习笔记-个人整理的

    {2.10}访问控制}{60}{section.2.10} {2.10.1}类的属性}{60}{subsection.2.10.1} {2.10.2}类的方法}{61}{subsection.2.10.2} {2.10.3}静态代码块}{62}{subsection.2.10.3} {2.11}\ttfamily final}{63}{section....

    易语言 茶凉专用模块

    参数 窗口句柄, 整数型, 可空, 要显示/隐藏的窗口句柄(可空:则显示/隐藏上次的窗口,如果为初次使用则为当前窗口) .子程序 窗口置父, 整数型, 公开, 指定一个窗口的新父(返回前一个父窗口的句柄) .参数 窗口句柄, ...

Global site tag (gtag.js) - Google Analytics