java 中用户访问权限(spring 权限管理)

 

// AuthorityInterceptor.java 文件

package com.aptech.jb.epet.web.authority;

import javax.servlet.http.HttpServletRequest;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.struts.action.ActionMapping;

public class AuthorityInterceptor implements MethodInterceptor {

 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  System.out.println("=============AuthorityInterceptor==="+methodInvocation.getMethod().getName());
  HttpServletRequest request=null;
  ActionMapping mapping=null;
  Object [] args=methodInvocation.getArguments();
  for(int i=0;i<args.length;i++){
   //左边是否是右边的一实例
   if(args[i] instanceof HttpServletRequest)
    request=(HttpServletRequest) args[i];
   if(args[i] instanceof ActionMapping)
    mapping=(ActionMapping) args[i];
   
  }
  if(request.getSession().getAttribute("CURRENT_PET")!=null){
   return methodInvocation.proceed();
  }else{
   return mapping.findForward("index");
  }
  
 }

}

------------------------------------------------------------------------

------------------spring 配置文件如下------------------

-----authorityInterceptor 为 AuthorityInterceptor 的实例。

<!-- 自动创建权限代理 -->
 <bean
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">0
   <list>
    <value>/pet</value>
    <value>/diary</value> //不能包含当前登录Action path 路径
   </list>
  </property>
  
  <property name="interceptorNames">
     <list>
        <value>authorityInterceptor</value>
       
     </list>
  </property>
 </bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值