您的位置:首页 > 编程语言 > Java开发

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

2009-11-29 18:58 681 查看
使用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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: