struts中的拦截器验证登录

权限验证拦截器PermissionInterceptor

//继承了AbstractInterceptor只需要重写intercept方法就可以了,省去了init()和destory()方法

public class PermissionInterceptor extends AbstractInterceptor
{
    //验证用户的权限
    @Override
    public String intercept(ActionInvocation arg0) throws Exception
    {
    if(ActionContext.getContext().getSession().get("admin")==null)
    {
       //用户没有登入,保存提示信息到request中
        ServletActionContext.getRequest().setAttribute("message","未登入,请先登入!");
           //ActionContext.getContext().put("message","未登入,请先登入!");
           return "nologin";
       }
       //继续执行后续的action
       returnarg0.invoke();
    }
}

Struts.xml配置

//因为验证是否登入是共用的,所以放在基包base中,在使用的时候必须要对添加的拦截器进行声明,并且还要将默认的拦截器加入不然就会失去默认拦截器的效果,

<package name="base" extends="struts-default">
   <!-- 拦截器的配置 -->
   <interceptors>
   <interceptor name="permissionInterceptor" class="interceptor.PermissionInterceptor"/> //声明
     <interceptor-stack name="allInterceptor">
     <!-- 必须包含原有的默认的defaultStack才会生效 -->
       <interceptor-ref name="defaultStack"/> //默认拦截器栈
       <interceptor-ref name="permissionInterceptor"/>  //引用
     </interceptor-stack>
   </interceptors>
   <default-interceptor-ref name="allInterceptor"/> //默认拦截器设置
<global-results>
    <result name="ajax">/ajax.jsp</result>
<result name="err">/err.jsp</result>  <!-- 出现错误跳转到err.jsp -->
<result name="nologin">/login.jsp</result>  <!-- 没有登陆就跳转到login.jsp -->
   </global-results>
 </package>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值