struts2 拦截器的研究 这里以登录为例

由于最近在负责公司项目的权限这一块,整个项目用到s2sh,所以自然想到了用struts2的拦截器作为登录和进行权限验证的接口

首先来谈谈struts2的实现原理:

Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.

Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的    拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器。

struts2给我们提供的拦截有很多,但是所有struts2的拦截器都直接或者接着的实现了com.opensymphony.xwork2.interceptor.Interceptor

这里我们会用到com.opensymphony.xwork2.interceptor.AbstractInterceptor

这里我只需要覆盖他的intercept(ActionInvocation invocation)这个方法就可以了

下面是A自定义AuthorInterceptor 的代码:

[java]  view plain  copy
 print ?
  1. import java.util.Map;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4.   
  5. import org.apache.struts2.ServletActionContext;  
  6.   
  7. import com.opensymphony.xwork2.Action;  
  8. import com.opensymphony.xwork2.ActionContext;  
  9. import com.opensymphony.xwork2.ActionInvocation;  
  10. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  11.   
  12. public class AuthorInterceptor extends AbstractInterceptor {  
  13.   
  14.  IAuthService authService;  
  15.  public void setAuthService(IAuthService authService) {  
  16.   this.authService = authService;  
  17.  }  
  18.  @SuppressWarnings("unchecked")  
  19.  @Override  
  20.  public String intercept(ActionInvocation invocation) throws Exception {  
  21.         // 获得拦截到的action名称  
  22.         String actionName =invocation.getInvocationContext().getName();  
  23.         ActionContext ctx = invocation.getInvocationContext();  
  24.         Map session = ctx.getSession();  
  25.         HttpServletRequest request = (HttpServletRequest)ctx.getContext().get(ServletActionContext.HTTP_REQUEST);  
  26.          
  27.          
  28.         //如果拦截到的action是请求登录的action,则放行.  
  29.         if("login".equals(actionName) || "logout".equals(actionName)){  
  30.          return invocation.invoke();  
  31.         }else{  
  32.          Op op = (Op)session.get("currentUser");  
  33.          if(op==null){  
  34.           return  Action.LOGIN;  
  35.          }  
  36.          String url = request.getRequestURI();  
  37.            //判断该用户有无访问该路径的权限  
  38.          boolean status = authService.interceptPermission(op.getOpId(), url);  
  39.          if(status){  
  40.           return invocation.invoke();  
  41.          }else{  
  42.           session.put("errorsInfo""您没有该资源的权限!");  
  43.                 return Action.ERROR;  
  44.          }  
  45.         }  
  46.  }  
  47.   
  48. }  

上面的拦截写完了,别忘记在spring中进行注册管理。

拦截器在struts.xml中的配置需要注意:下面有说明:

[html]  view plain  copy
 print ?
  1. <package name="BasePackage" extends="struts-default">  
  2.   <interceptors>  
  3.   
  4.        <!-- 声明自己的拦截器,起名叫check,对应的class属性为自己编写的拦截器路径 -->  
  5.            <interceptor name="check" class="authorInterceptor" />  
  6.            <!-- 定义拦截器栈,这里需要注意:在定制自己拦截器的同时,必须要把struts的默认栈加如里面,如果不加入,相当于把默认的覆盖了,会出现异常! -->  
  7.            <interceptor-stack name="myCheck">  
  8.               <interceptor-ref name="check" />  
  9.               <interceptor-ref name="defaultStack" />  
  10.            </interceptor-stack>  
  11.        </interceptors>  
  12.   <!-- 定义默认拦截器 -->  
  13.        <default-interceptor-ref name="myCheck" />  
  14.         <!-- 定义全局结果,用于在拦截器中返回登录页面或者错误提示页面 -->  
  15.        <global-results>  
  16.            <result name="login" type="redirect">/login.jsp</result>  
  17.            <result name="error">/error.jsp</result>  
  18.        </global-results>  
  19.  </package>  
  20.   
  21.  <!-- 登录的action -->  
  22. <package name="default" namespace="/" extends="BasePackage">  
  23.   <action name="login" class="loginAction">  
  24.    <result>/index.jsp</result>  
  25.    <result name="input" type="redirect">/input.jsp</result>  
  26.   </action>  
  27.   <action name="logout" class="loginAction" method="logout">  
  28.    <result type="redirect">/login.jsp</result>  
  29.   </action>  
  30.  </package>  

下面在简单介绍LoginAction供大家参考

[java]  view plain  copy
 print ?
  1. public class LoginAction extends ActionSupport{  
  2.   
  3. //service层   
  4.  private IAuthService authService;  
  5.    
  6.  public String execute(){  
  7.     HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);  
  8.     HttpSession session = request.getSession();  
  9.     Op op = this.authService.Login(request.getParameter("opName"),request.getParameter("pwd"));  
  10.     if(op==null){  
  11.      return Action.INPUT;  
  12.   }  
  13.     
  14.   session.setAttribute("currentUser", op);  
  15.   //获得用户的菜单列表形成tree  
  16.   List<Resource> listRes = this.authService.findResources(op.getOpId());  
  17.   session.setAttribute("listRes ", listRes );  
  18.   return SUCCESS;  
  19.  }  
  20.    
  21.  public String logout(){  
  22.   HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);  
  23.   HttpSession session = request.getSession();  
  24.   session.removeAttribute("currentUser");  
  25.   return SUCCESS;  
  26.  }  
  27.   
  28.    
  29.  public void setAuthService(IAuthService authService) {  
  30.   this.authService = authService;  
  31.  }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值