package cn.itcast.shop.Interceptor;
import org.apache.struts2.ServletActionContext;
import cn.itcast.shop.vo.AdminUser;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
/**
* 后台用户登录权限拦截器(Action文件拦截,在action中配置拦截)
*
* @author 吕小刚
* @version V1.0
*
*/
// 继承AbstractInterceptor子类MethodFilterInterceptor拦截器
@SuppressWarnings("serial")
public class PrivilegeInterceptor extends MethodFilterInterceptor {
@Override
// 执行拦截的方法
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
// 判断Session中是否保存了后台用户信息
AdminUser adminUser = (AdminUser) ServletActionContext.getRequest().getSession().getAttribute("adminUser");
if (adminUser == null) {
// 没有登录进行访问
// 对继承过ActionSupport类里面的方法进行拦截
ActionSupport actionSupport = (ActionSupport) actionInvocation.getAction();
actionSupport.addFieldError("loginFail", "亲!你还没有登录!没有访问权限!");
return "loginFail";
} else {
// 已经登录过
return actionInvocation.invoke();
}
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 指定默认编码集 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 设置视图主题为默认 -->
<constant name="struts.ui.theme" value="simple" />
<!-- 指后后缀名为.action -->
<constant name="struts.action.extension" value="action" />
<!-- 设置struts2为开发模式 -->
<constant name="struts.devMode" value="true" />
<package name="shop" namespace="/" extends="struts-default">
<!-- 定义拦截器 -->
<interceptors>
<interceptor name="privilegeInterceptor"
class="cn.itcast.shop.Interceptor.PrivilegeInterceptor" />
</interceptors>
<!-- 全局跳转提示页面 -->
<global-results>
<result name="msg">/WEB-INF/jsp/msg.jsp</result>
<result name="login">/WEB-INF/jsp/login.jsp</result>
<!-- 后台用户拦截,提示跳转 -->
<result name="loginFail">/admin/index.jsp</result>
</global-results>
<!-- 首页访问配置 -->
<action name="index" class="indexAction">
<result name="index">/WEB-INF/jsp/index.jsp</result>
</action>
<!-- 用户注册配置 -->
<action name="user_*" class="userAction" method="{1}">
<result name="registPage">/WEB-INF/jsp/regist.jsp</result>
<result name="input">/WEB-INF/jsp/{1}.jsp</result>
<result name="loginPage">/WEB-INF/jsp/login.jsp</result>
<result name="loginSuccess" type="redirectAction">index</result>
<result name="loginout" type="redirectAction">index</result>
</action>
<!-- 验证码Action -->
<action name="checkImg" class="checkImgAction">
</action>
<!-- 商品模块Action -->
<action name="product_*" class="productAction" method="{1}">
<result name="findById">/WEB-INF/jsp/product.jsp</result>
<!-- 查询一级分类下的二级分类 -->
<result name="findBycId">/WEB-INF/jsp/productList.jsp</result>
<!-- 查询二级级分类下的商品信息 -->
<result name="findBycsId">/WEB-INF/jsp/productList.jsp</result>
</action>
<!-- 购物车模块Action -->
<action name="cart_*" class="cartAction" method="{1}">
<result name="addCart">/WEB-INF/jsp/cart.jsp</result>
<result name="clearCart" type="redirectAction">cart_addCart</result>
<result name="removerCart" type="redirectAction">cart_addCart</result>
<result name="myCart" type="redirectAction">cart_addCart</result>
</action>
<!-- 订单模块Action -->
<action name="order_*" class="orderAction" method="{1}">
<result name="saveSuccess">/WEB-INF/jsp/order.jsp</result>
<result name="findByUserIdSuccess">/WEB-INF/jsp/orderList.jsp</result>
<result name="findByOrderId">/WEB-INF/jsp/order.jsp</result>
</action>
<!-- 配置后台用户管理模块Action -->
<action name="adminUser_*" class="adminUserAction" method="{1}">
<result name="loginFail">/admin/index.jsp</result>
<result name="loginSuccess" type="redirect">/admin/home.jsp</result>
</action>
<!-- 配置后台一级分类后台管理Action -->
<action name="adminCategory_*" class="adminCategoryAction"
method="{1}">
<result name="findAll">/admin/category/list.jsp</result>
<result name="addUI">/admin/category/add.jsp</result>
<result name="addSuccess" type="redirectAction">adminCategory_findAll.action
</result>
<result name="deleteSuccess" type="redirectAction">adminCategory_findAll.action
</result>
<result name="editUI">/admin/category/edit.jsp</result>
<result name="editSuccess" type="redirectAction">adminCategory_findAll.action
</result>
<!-- 引用拦截器(在要拦截的action中引用拦截器) -->
<interceptor-ref name="privilegeInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<!-- 配置后台二级分类后台管理Action -->
<action name="adminCategorySecond_*" class="adminCategorySecondAction"
method="{1}">
<result name="findAll">/admin/categorySecond/list.jsp</result>
<result name="addUI">/admin/categorySecond/add.jsp</result>
<result name="addSuccess" type="redirectAction">adminCategorySecond_findAll.action?page=1
</result>
<result name="deleteSuccess" type="redirectAction">adminCategorySecond_findAll.action?page=1
</result>
<result name="editUI">/admin/categorySecond/edit.jsp</result>
<result name="editSuccess" type="redirectAction">adminCategorySecond_findAll.action?page=1
</result>
<!-- 引用拦截器(在要拦截的action中引用拦截器) -->
<interceptor-ref name="privilegeInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<!-- 配置后台商品管理 -->
<action name="adminProduct_*" class="adminProductAction"
method="{1}">
<result name="findAll">/admin/product/list.jsp</result>
<result name="addUI">/admin/product/add.jsp</result>
<result name="addSuccess" type="redirectAction">adminProduct_findAll.action?page=1
</result>
<result name="deleteSuccess" type="redirectAction">adminProduct_findAll.action?page=1
</result>
<result name="editUI">/admin/product/edit.jsp</result>
<result name="editSuccess" type="redirectAction">adminProduct_findAll.action?page=1
</result>
<!-- 引用拦截器(在要拦截的action中引用拦截器) -->
<interceptor-ref name="privilegeInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<!-- 配置后台订单管理 -->
<action name="adminOrder_*" class="adminOrderAction" method="{1}">
<result name="findAll">/admin/order/list.jsp</result>
<result name="findOrderItem">/admin/order/orderItem.jsp</result>
<result name="editStateSuccess" type="redirectAction">adminOrder_findAll.action?page=1
</result>
<!-- 引用拦截器(在要拦截的action中引用拦截器) -->
<interceptor-ref name="privilegeInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>