使用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.
使用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.
相关文章推荐
- struts2 使用注解、反射、拦截器实现基于方法的权限控制
- 使用Struts2的拦截器实现权限控制
- 在struts2中使用拦截器(Interceptor)控制登录和权限
- 【Spring MVC】教程——使用拦截器实现权限控制
- Struts2使用拦截器完成权限控制示例
- 在struts2中使用拦截器(Interceptor)控制登录和权限
- 4.5.12: Struts2的拦截器机制---使用拦截器完成权限控制
- Struts2中使用拦截器进行权限控制
- 在struts2.3.4.1中使用注解、反射、拦截器实现基于方法的权限控制
- Struts之使用拦截器实现的登录权限的控制
- 使用拦截器实现权限控制
- 使用Struts2拦截器进行权限控制
- Struts2拦截器实现登录权限控制(十二)
- 6、Struts2拦截器实现权限控制
- 在struts2中使用拦截器(Interceptor)控制登录和权限
- Struts2使用Interceptor实现权限控制的应用实例详解
- Struts2拦截器 实现未登录拦截和权限控制
- 【Spring MVC】教程——使用拦截器实现权限控制
- Struts2利用拦截器实现权限控制
- 【Spring MVC】教程——使用拦截器实现权限控制