使用Struts 拦截namespace进行权限控制
来源:互联网 发布:引流网站源码 编辑:程序博客网 时间:2024/06/05 09:08
有时候我需要在几个包下都需要进行同一个权限控制。如在购物网站中,我们需要进入个人中心、下订单、评价商品等等都需要进行登录权限控制,但是这几个模块并不是位于同一个package下。Struts提供的拦截器,我们可以实现action下拦截,我们虽然可以在每一个package都配置这个拦截器,但是是相当的麻烦。这个时候我们可以利用拦截器实现拦击package。将需要进行权限控制package放入拦截器中就可以实现了。
首先我们需要在struts.xml下进行拦截器的配置。
1 <package name="main" extends="struts-default"> 2 <interceptors> 3 <interceptor name="authorizationInterceptor" class="syxh.common.aop.SystemInterceptor"></interceptor> 4 <interceptor-stack name="jwzhptStack"> 5 <interceptor-ref name="defaultStack"></interceptor-ref> 6 <interceptor-ref name="authorizationInterceptor"></interceptor-ref> 7 </interceptor-stack> 8 </interceptors> 9 10 <default-interceptor-ref name="jwzhptStack" />11 12 <global-results>13 <result name="loginfailure" type="redirectAction">14 <param name="namespace">/index</param>15 <param name="actionName">index</param>16 </result>17 </global-results>18 19 <global-exception-mappings>20 <exception-mapping result="input" exception="*">/login.jsp</exception-mapping>21 </global-exception-mappings>22 </package>
上面配置的main ,所以的package都要继承main,即:extends=”main”,否则实现不了。
在struts.xml中,使用了拦截器栈,里面包含两个拦截器,一个是默认的defaultStack,一个是进行权限控制的authorizationInterceptor。
拦截器实现类:SystemInterceptor.java
1 public class SystemInterceptor extends AbstractInterceptor{ 2 3 private static final long serialVersionUID = -1819593755738908387L; 4 private static final String WITHOUT = "/index, /author, /common, /indexzp"; //不需要进行权限控制的namespace 5 6 public void destroy(){ 7 } 8 9 public void init(){10 }11 12 public String intercept(ActionInvocation invocation) throws Exception{13 String namespace = invocation.getProxy().getNamespace(); //获取namespace14 if (WITHOUT.indexOf(namespace) >= 0){15 return invocation.invoke();16 }17 18 Map<?, ?> session = invocation.getInvocationContext().getSession();19 UserBean user = (UserBean) session.get("currentUser");20 if (user == null){ //没有登录21 return "loginfailure";22 } 23 return invocation.invoke(); //已登录24 }25 }
上面的拦截器实现类,指定了几个namespace是不需要进行权限控制的,除此之外其他的namespace都要进行
权限控制。
其他package只需要继承main既可实现权限控制。
1 <package name="user" namespace="/user" extends="main">2 <action name="userCenter_*" class="syxh.grzx.action.UserCenterAction" method="{1}">3 <result name="updatePasswordUI">/jsp/grzx/updatePassword.jsp</result>4 <result name="updateError">/jsp/grzx/updatePassword.jsp</result>5 <result name="updateSuccess" type="redirect">/zp/myWorksHome.action</result>6 <result name="updatePhoteUI">/jsp/grzx/updatePhoto.jsp</result>7 <result name="updateInfoUI">/jsp/grzx/updateInfo.jsp</result>8 </action>9 </package>
注:由于这个拦截器主要是根据namespace来进行控制的,所以在配置package,要添加namespace。
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 使用Struts 拦截namespace进行权限控制
- 使用Struts 拦截namespace进行权限控制
- 使用Struts 拦截namespace进行权限控制
- 使用拦截器进行权限的控制
- 使用拦截器进行权限控制
- Struts 权限控制 拦截器
- java struts 2 使用拦截器完成权限控制
- Struts2中使用拦截器进行权限控制
- SSM项目,使用拦截器进行权限控制
- Struts 2读书笔记-----拦截器之示例:使用拦截器完成权限控制
- Struts实现的权限控制拦截器
- struts interceptor拦截器做权限控制
- struts自定义拦截器--登录权限控制
- 在struts中使用拦截器(Interceptor)控制登录和权限
- Struts之使用拦截器实现的登录权限的控制
- struts下通过过滤器进行权限控制
- struts下通过过滤器进行权限控制
- struts下通过过滤器进行权限控制
- Activity生命周期详解
- A Christmas Carol——4、The last of the spirits
- Android笔记之handler
- 如何测试文件copy的速度
- PAT (Basic Level) Practise (中文)—— 1010. 一元多项式求导
- 使用Struts 拦截namespace进行权限控制
- JavaEE 快速开发平台G4Studio作者熊春专访
- 海淀三甲免选
- 东城三甲免选
- HDU 1702 STL list的练习
- WWDC+2013 PPT
- 朝阳三甲免选
- 说说我的离职
- windows中的CMake如何使用