后台管理的时候,为了防止直接输入jsp或者对应的action绕过用户登录而进入系统,设置拦截器来进行权限控制。
1.添加类:PrivilegeInterceptor
<span style="font-family:SimSun;font-size:18px;">package cn.itcast.shop;
import org.apache.struts2.ServletActionContext;
import cn.itcast.shop.adminuser.vo.AdminUser;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
//对没有登录的用户,不能进行访问
public class PrivilegeInterceptor extends MethodFilterInterceptor
{
@Override
protected String doIntercept(ActionInvocation actionInvocation) throws Exception
{
//判断session中是否保存了后台用户的信息
AdminUser existUser = (AdminUser)ServletActionContext.getRequest().getSession().getAttribute("existUser");
if(existUser == null){
ActionSupport actionSupport = (ActionSupport)actionInvocation.getAction();
actionSupport.addActionError("亲,请先登录!");
return "loginFail";
}else{
//已经登录
return actionInvocation.invoke();
}
}
}</span>2配置struts.xml文件
<span style="font-family:SimSun;font-size:18px;"><struts>
<constant name="struts.devMode" value="false" />
<package name="shop" extends="struts-default" namespace="/">
<!--配置拦截器-->
<interceptors>
<interceptor name="PrivilegeInterceptor" class="cn.itcast.shop.PrivilegeInterceptor"/>
</interceptors>
<!-- 配置后台一级分类的Action -->
<action name="adminCategory_*" class="adminCategoryAction" method="{1}">
<result name="findAll">/admin/category/list.jsp</result>
<result name="saveSuccess" type="redirectAction">adminCategory_findAll.action </result>
<!--使用拦截器,对action中的所有方法进行拦截-->
<interceptor-ref name="PrivilegeInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action></span>3结果显示
当用户没有登录的时候,输入jsp或者对应的action时跳转到提示页面。

被折叠的 条评论
为什么被折叠?



