使用Struts2的拦截器实现权限控制

来源:互联网 发布:压缩照片的软件 编辑:程序博客网 时间:2024/05/16 12:59

 使用spring 的AOP实现权限控制,直接使用了struts2 提供的ActionContext,使spring和struts耦合在一起,从这个角度考虑,直接使用struts的拦截器更合适.

 

使用struts的拦截器,要实现Interceptor,今天没有遇到什么问题,调试也很顺利,代码如下:

package bbs.web.interceptor;

import bbs.domain.bean.User;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor{

 /**
  *
  */
 private static final long serialVersionUID = 5729500231434275095L;

 @Override
 public String intercept(ActionInvocation arg0) throws Exception {
  User user = (User) ActionContext.getContext().getSession().get("user");
  if (user == null)
   return "login";
  return arg0.invoke();
 }

}
在struts.xml配置:

<interceptors>
   <interceptor name="loginInterceptor" class="bbs.web.interceptor.LoginInterceptor"></interceptor>
  </interceptors>
...
  <action name="board_*" class="boardAction" method="{1}">
   <result name="success">/boardlist.jsp</result>
   <result name="error">/{1}board.jsp</result>
   <result name="login">/login.jsp</result>
   <interceptor-ref name="defaultStack"></interceptor-ref>
   <interceptor-ref name="loginInterceptor"></interceptor-ref>
  </action>

注意一点,拦截器使用要明确指定使用defaultStack,否则,struts默认的拦截器就不启用了.

 

做权限控制,还可以使用filter.

原创粉丝点击