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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章