struts2攔截器 獲得請求方法名+獲得請求參數

struts2攔截器裏如何知道你請求的是那個方法
使用:invocation.getInvocationContext().getName(); //輸出Priv_queryPriv,這正是我訪問的Action中的方法。

 

1.struts.xml中這麼定義的

Xml代碼  收藏代碼
  1. <struts>  
  2.     <!-- character filter -->  
  3.     <constant name="struts.i18n.encoding" value="utf-8" />  
  4.     <constant name="struts.multipart.saveDir" value="/tmp" />  
  5.     <constant name="struts.multipart.maxSize" value="1000000000" />  
  6.     <!-- CONFIG Global Exception -->  
  7.       
  8.     <package name="basePriv" extends="struts-default">  
  9.         <interceptors>  
  10.             <interceptor name="myPrivInterceptor" class="PrivInterceptor"/>  
  11.             <interceptor-stack name="b2cplatPrivInterceptor">  
  12.                 <interceptor-ref name="myPrivInterceptor">  
  13.                     <param name="includeMethods"></param>  
  14.                     <param name="excludeMethods">  
  15.                         loginMain,loginTop,loginSwitch,loginRight,login,leftMenuShow,  
  16.                         queryCityList,queryInOrOutAreaList,queryDistricts  
  17.                     </param>  
  18.                 </interceptor-ref>  
  19.                 <interceptor-ref name="defaultStack"/>  
  20.             </interceptor-stack>  
  21.         </interceptors>  
  22.           
  23.         <default-interceptor-ref name="b2cplatPrivInterceptor"/>  
  24.           
  25.         <global-results>  
  26.             <result name="privError">/errorPrivPage.jsp</result>  
  27.             <result name="updateEmpPassword">/jsp/phone/xxxx.jsp</result>  
  28.             <result name="loginPage" type="redirect">/jsp/phone/login/trunToLogin.jsp</result>  
  29.         </global-results>  
  30.           
  31.         <global-exception-mappings>  
  32.             <exception-mapping result="error" exception="java.lang.Exception">/errorPage.jsp  
  33.             </exception-mapping>  
  34.         </global-exception-mappings>  
  35.     </package>  
  36.       
  37.       
  38. <package name="managerPlatform" extends="basePriv" namespace="/">  
  39.     <action name="*_*" class="{1}Action" method="{2}">  
  40.         <result name="success">${successPath}</result>  
  41.         <result name="error">${errorPath}</result>  
  42.         <result name="input">${inputPath}</result>  
  43.         <result name="redirectAction" type="redirectAction">${redirectActionPath}</result>  
  44.         <result name="doChain" type="chain">${chainPath}</result>  
  45.         <result name="redirect" type="redirect">${redirectPath}</result>  
  46.         <result name="print" type="stream">  
  47.             <param name="contentType">application/vnd.ms-excel</param>  
  48.             <param name="inputName">inputStream</param>  
  49.             <param name="contentDisposition">filename="${printFileName}"</param>  
  50.             <param name="bufferSize">1024</param>  
  51.         </result>  
  52.     </action>  
  53. </package>  
  54.           
  55. </struts>  

 2.Action這麼寫

Java代碼  收藏代碼
  1. /** 
  2.  * 權限信息控制 
  3.  * @author  ken 
  4.  * @date 2011-9-13 下午15:00:46 
  5.  */  
  6. @Scope("prototype")  
  7. @Controller("PrivAction")  
  8. public class PrivAction extends BaseAction{  
  9.   
  10.     private static final long serialVersionUID = 1L;  
  11.     static final Logger log = Logger.getLogger(PrivAction.class);  
  12.       
  13.     @Autowired  
  14.     private PrivService privService;  
  15.       
  16.     /* 權限模型 */  
  17.     private TEmployeePriv employeePriv;  
  18.       
  19.     /** 
  20.      * 權限查詢 
  21.      * @return 
  22.      */  
  23.     public String queryPriv(){  
  24.         if(employeePriv==null){  
  25.             employeePriv = new TEmployeePriv();  
  26.             successPath = "/jsp/phone/priv/priv/privList.jsp";  
  27.             return SUCCESS;  
  28.         }  
  29.         try {  
  30.             entitys = this.privService.queryAllPriv(employeePriv);  
  31.         } catch (Exception e) {  
  32.             log.error("",e);  
  33.         }  
  34.           
  35.         successPath = "/jsp/phone/priv/priv/privList.jsp?flag=true";  
  36.         return SUCCESS;  
  37.     }  
  38. }  

 3.struts2攔截器

Java代碼  收藏代碼
  1.  /** 
  2.   * 權限攔截器Interceptor 
  3.  * @author mengxianjun 
  4.  * @date 2011-4-8 下午03:07:24 
  5.  * 
  6.  */  
  7.   
  8. @SuppressWarnings("serial")  
  9. @Component"PrivInterceptor" )  
  10. @Scope("prototype")  
  11. public class PrivInterceptor extends MethodFilterInterceptor{  
  12.       
  13.     @Resource(name = "EmployeeService")  
  14.     private EmployeeService empSafeService;//工號安全Service  
  15.       
  16.     @Resource(name="EmployeeRoleService")  
  17.     private EmployeeRoleService empRoleService;  
  18.       
  19.     /* (non-Javadoc) 
  20.      * @see com.opensymphony.xwork2.interceptor.MethodFilterInterceptor#doIntercept(com.opensymphony.xwork2.ActionInvocation) 
  21.      * @author mengxianjun 
  22.      * @date 2011-4-8 下午03:07:24 
  23.      */  
  24.       
  25.     @SuppressWarnings("unchecked")  
  26.     @Override  
  27.     protected String doIntercept(ActionInvocation invocation) throws Exception {  
  28.   
  29.         System.out.println("============"+invocation.getInvocationContext().getName());  
  30.         System.out.println("============"+invocation.getInvocationContext().getLocale());  
  31.         System.out.println("============"+invocation.getInvocationContext().getParameters());  
  32.           
  33.           
  34.         System.out.println("執行到攔截器裏。。。。");  
  35.           
  36.         ActionContext act = invocation.getInvocationContext();  
  37.           
  38.         //獲得session  
  39.         Map session = invocation.getInvocationContext().getSession();  
  40.           
  41.         TEmployeeInfo sessionInfo = (TEmployeeInfo) session.get("user");  
  42.           
  43.         String employee_id="";  
  44.           
  45.         /** 
  46.          * 一、是否登錄 
  47.          */  
  48.         try  
  49.         {  
  50.             employee_id = sessionInfo.getEmployeeId();  
  51.         }  
  52.         catch( NullPointerException e )  
  53.         {  
  54.             act.put("message""Session過期,請重新登錄!");  
  55.             return "loginPage";  
  56.         }  
  57.           
  58. /*=========================================================單點登錄判斷============================================*/  
  59.         HashMap<String, String> map = (HashMap<String, String>)  ServletActionContext.getServletContext().getAttribute("userList");  
  60.         String sessionID_User = map.get( employee_id ); //登錄用戶session的ID  
  61.         String sessionID_Now = ServletActionContext.getRequest().getSession().getId(); //當前session的ID  
  62.           
  63.         if( ! sessionID_User.trim().equals(sessionID_Now) )  
  64.         {  
  65.             act.put("message""此賬號已登錄!");  
  66.             return "privError";  
  67.         }  
  68. /*=========================================================單點登錄判斷============================================*/  
  69.           
  70.         /** 
  71.          * 二、登錄成功後,根據URL進行權限判斷 
  72.          */  
  73.         if( !"".equals(employee_id.trim()) && null!=employee_id )  
  74.         {  
  75.             /** 
  76.              * 2.1判斷工號登錄後,業務密碼是否爲123456,是跳轉到商戶安全設置,修改業務密碼 
  77.              */  
  78.             /*TEmployeeSafe empSafe = empSafeService.queryEmployeSafe(employee_id); 
  79.             if( null!=empSafe ) 
  80.             { 
  81.                 String MD5password = KeyedDigestMD5.getKeyedDigest("123456","").toUpperCase();//獲得123456的MD5值 
  82.                 String employeePass = empSafe.getEmployeePass();//獲得登錄密碼 
  83.                 String employeePass2 = empSafe.getEmployeePass2();//獲得工號業務密碼 
  84.                 if( MD5password.equals(employeePass) || MD5password.equals(employeePass2) ) 
  85.                 { 
  86.                     act.put("message", "歡迎使用本系統,您的登錄密碼、業務密碼過於簡單,請修改!"); 
  87.                     return "updateEmpPassword"; 
  88.                 } 
  89.             }*/  
  90.               
  91.               
  92.             /** 
  93.              * 2.2截取請求URL 
  94.              */  
  95.             HttpServletRequest request = ServletActionContext.getRequest();  
  96.               
  97.             String currentURL = request.getRequestURI();  
  98.             String targetURL = "";  
  99.               
  100.             if( -1 != currentURL.indexOf("?") )//普通<form>標籤是?分隔傳來的參數  
  101.             {  
  102.                 String paramURL = currentURL.substring(currentURL.indexOf("?",0), currentURL.length());//參數URL  
  103.                   
  104.                 int targetLength = currentURL.length() - paramURL.length();//去掉請求參數Length  
  105.                   
  106.                 targetURL = currentURL.substring(currentURL.indexOf("/",1), targetLength);  
  107.                 System.out.println("去掉請求參數路徑URL:"+targetURL);  
  108.             }  
  109.             else if( -1 != currentURL.indexOf(";") )//struts2標籤<s:form>標籤是;分隔傳來的參數  
  110.             {  
  111.                 String paramURL = currentURL.substring(currentURL.indexOf(";",0), currentURL.length());//參數URL  
  112.                   
  113.                 int targetLength = currentURL.length() - paramURL.length();//去掉請求參數Length  
  114.                   
  115.                 targetURL = currentURL.substring(currentURL.indexOf("/",1), targetLength);  
  116.                 System.out.println("去掉請求參數路徑URL:"+targetURL);  
  117.             }  
  118.             else  
  119.             {  
  120.                 targetURL = currentURL.substring(currentURL.indexOf("/",1), currentURL.length());  
  121.                 System.out.println("請求路徑URL:"+targetURL);  
  122.             }  
  123.               
  124.               
  125.             /** 
  126.              * 2.3必須保證當前用戶:1.工號必須開啓2.角色已分配  3.角色已啓用   4.角色有權限集合 
  127.              */  
  128.             if("12".equals(sessionInfo.getState()))  
  129.             {  
  130.                 act.put("message""工號已鎖定!");  
  131.                 return "privError";  
  132.             }  
  133.             else if("15".equals(sessionInfo.getState()))  
  134.             {  
  135.                 act.put("message""工號已註銷!");  
  136.                 return "privError";  
  137.             }  
  138.             else if( sessionInfo.getRoleState()==null || "".equals(sessionInfo.getRoleState()) )  
  139.             {  
  140.                 act.put("message""未分配角色!");  
  141.                 return "privError";  
  142.             }  
  143.             else if( !"10".equals(sessionInfo.getRoleState()) )  
  144.             {  
  145.                 act.put("message""該角色未啓用!");  
  146.                 return "privError";  
  147.             }  
  148.             else  
  149.             {  
  150.                 try  
  151.                 {  
  152.                     /*1.得到中間表TRolePriv集合*/  
  153.                     TRolePriv rp = new TRolePriv();  
  154.                     rp.setRoleNum(sessionInfo.getRoleNum());  
  155.                     List<TRolePriv> rolePrivList = empRoleService.queryRolePriv(rp);  
  156.                       
  157.                     /*2.根據中間表TRolePriv,生成TEmployeePriv集合*/  
  158.                     List<TEmployeePriv> privList = new ArrayList<TEmployeePriv>();  
  159.                     for( TRolePriv trp : rolePrivList )  
  160.                     {  
  161.                         TEmployeePriv myPriv = empRoleService.queryPrivById(trp.getPrivNum());  
  162.                         if(myPriv!=null&&myPriv.getPrivUrl()!=null&&!"".equals(myPriv.getPrivUrl())){  
  163.                             privList.add(myPriv);//去掉一級菜單添加進privList,privUrl爲空是一級菜單  
  164.                         }  
  165.                     }  
  166.                       
  167.                     /*3.權限privUrl與targetURL比較*/  
  168.                     if( privList.size()>0 )  
  169.                     {  
  170.                         int privState = 0;  
  171.                           
  172.                         for( TEmployeePriv p : privList )  
  173.                         {  
  174.                             /** 
  175.                              * 對比去掉請求參數後的URL是否一致,即/Login_login 
  176.                              */  
  177.                             String privUrl = p.getPrivUrl();//TEmployeePriv中privUrl,可能帶參數,可能不帶參數  
  178.                               
  179.                             if(-1!=privUrl.indexOf("?",0)){  
  180.                                 String paramPrivURL = privUrl.substring(privUrl.indexOf("?",0), privUrl.length());//參數URL  
  181.                                 int targetPrivLength = privUrl.length() - paramPrivURL.length();//去掉請求參數Length  
  182.                                 privUrl = privUrl.substring(privUrl.indexOf("/",0), targetPrivLength);//TEmployeePriv中privUrl去掉參數  
  183.                             }  
  184.                               
  185.                             if( privUrl.equals(targetURL) )  
  186.                             {  
  187.                                 privState = 1;  
  188.                             }  
  189.                         }  
  190.                         if1 == privState )  
  191.                         {  
  192.                             return invocation.invoke();  
  193.                         }  
  194.                         else  
  195.                         {  
  196.                             System.out.println("-------得到Priv權限集合,但是無訪問權限---------");  
  197.                             act.put("message""您沒有權限  , 拒絕訪問!");  
  198.                             return "privError";  
  199.                         }  
  200.                     }  
  201.                     else  
  202.                     {  
  203.                         act.put("message""您沒有相應權限 , 拒絕訪問!");  
  204.                         return "privError";  
  205.                     }  
  206.                 }  
  207.                 catch( NullPointerException e )  
  208.                 {  
  209.                     act.put("message""您沒有權限 , 拒絕訪問!");  
  210.                     return "privError";  
  211.                 }  
  212.             }  
  213.         }  
  214.         else  
  215.         {  
  216.             act.put("message""Session過期,請重新登錄!");  
  217.             return "loginPage";  
  218.         }  
  219.     }  
  220. }  
發佈了35 篇原創文章 · 獲贊 9 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章