隨筆 - 1  文章 - 37  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          留言簿(16)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          test

          搜索

          •  

          最新評(píng)論

          示例中需要在eclipse中創(chuàng)建2個(gè)project:ServiceCentre和TestService

          java.policy

          grant codeBase "file:/D:/Workspaces/ExchangeConnect_V2_Trunk_Maven_workspace/ServiceCentre/bin/*" {
              permission java.io.FilePermission 
          "c:/TestService-1.0.jar""read";
              permission java.lang.RuntimePermission 
          "createClassLoader";
          }
          ;

          grant codeBase 
          "file:/c:/TestService-1.0.jar" {
              permission java.io.FilePermission 
          "C:/text.txt""read";
          }
          ;




          Project - ServiceCentre

          package test;

          import java.lang.reflect.Method;
          import java.net.URL;
          import java.net.URLClassLoader;

          /**
           * 
          @author Donf Yang
           
          */

          public class ServiceCentreMain {

              
          public void loadService() {
                  URL[] urls;
                  
          try {
                      urls 
          = new URL[] new URL("file:c:/TestService-1.0.jar") };
                      URLClassLoader ll 
          = new URLClassLoader(urls);
                      
          final Class a = ll.loadClass("test.TestService");
                      Object o 
          = a.newInstance();
                      Method m 
          = a.getMethod("doService"null);
                      m.invoke(o, 
          null);

                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }


              
          /**
               * 
          @param args
               
          */

              
          public static void main(String[] args) {
                  ServiceCentreMain s 
          = new ServiceCentreMain();
                  s.loadService();
              }


          }


           

          Project - TestService
          將TestService打包,放到C盤
          package test;

          import java.io.FilePermission;
          import java.security.AccessController;
          import java.security.Permission;

          /**
           * 
          @author Donf Yang
           * 
           
          */

          public class TestService {

              
          public void doService() {
                  
                  doFileOperation();

              }


              
          private void doFileOperation() {
                  Permission perm 
          = new FilePermission("C:/text.txt""read");
                  AccessController.checkPermission(perm);
                  System.out.println(
          "TestService has permission");
              }


          }

          運(yùn)行這個(gè)例子的時(shí)候,會(huì)出現(xiàn)權(quán)限錯(cuò)誤,把doService()修改一下,就可以順利通過
          public void doService() {

                  
          // doFileOperation();

                  AccessController.doPrivileged(
          new PrivilegedAction() {
                      
          public Object run() {
                          doFileOperation();
                          
          return null;
                      }

                  }
          );
              }

          在這個(gè)例子中AccessControlContext的stack順序?yàn)?br /> 2.  file:/D:/Workspaces/ExchangeConnect_V2_Trunk_Maven_workspace/ServiceCentre/bin/*
          1 . file:/c:/TestService-1.0.jar

          2沒有權(quán)限,1有權(quán)限,使用doPrivileged后,不檢查2



          看一下java.security.AccessController的JavaDoc:

          A caller can be marked as being "privileged" (see doPrivileged and below). When making access control decisions, the checkPermission method stops checking if it reaches a caller that was marked as "privileged" via a doPrivileged call without a context argument (see below for information about a context argument). If that caller's domain has the specified permission, no further checking is done and checkPermission returns quietly, indicating that the requested access is allowed. If that domain does not have the specified permission, an exception is thrown, as usual. 

          其中提到的no further checking is done的意思是指stack中的checking

          加入一個(gè)TestService2,文件操作在1,stack為(1,2,3為checking順序)
          3 . file:/D:/Workspaces/ExchangeConnect_V2_Trunk_Maven_workspace/ServiceCentre/bin/*
          2 . file:/c:/TestService-1.0.jar
          1.  file:/c:/TestService2-1.0.jar
           checking順序?yàn)?nbsp; 1->2->3
          如果doPrivileged是在2中調(diào)用,那么1,2需要具有權(quán)限,3不再進(jìn)行檢查
          如果doPrivileged是在1中調(diào)用,那么1需要具有權(quán)限,2,3不再進(jìn)行檢查


          總結(jié):
          1.  這里容易理解錯(cuò)誤的地方是checking順序,例如一個(gè)調(diào)用鏈 MethodA->MethodB->MethodC(這里的3個(gè)方法需要在3個(gè)不同的ProtectionDomain中),doPrivileged在MethodB中,很容易理解成檢查A,B而不檢查C,實(shí)際上stack中檢查順序?yàn)镃->B->A,也就是檢查C,B而不檢查A

          2. ServiceCentre不需要太多權(quán)限,而Service就需要使用doPrivileged來避免受到ServiceCentre的權(quán)限限制(如果service有足夠的權(quán)限),Equinox中有很多這樣的例子(Equinox扮演Service的角色)。
          posted on 2009-03-11 12:44 Phrancol Yang 閱讀(6753) 評(píng)論(1)  編輯  收藏

          FeedBack:
          # re: [原]一個(gè)例子理解AccessController.doPrivileged() 2012-12-24 16:19 Flexin
          謝謝分享,我第一遍沒看懂,后來去其他地方看了有關(guān)權(quán)限檢查的的文章以后才看懂,建議解釋一下調(diào)用棧,和權(quán)限檢查(取調(diào)用棧中權(quán)限的交集)。  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 上饶县| 武安市| 腾冲县| 城步| 抚松县| 大连市| 泸州市| 石河子市| 卢龙县| 汶上县| 广元市| 青海省| 磴口县| 定安县| 澜沧| 桃园县| 榆社县| 漯河市| 耒阳市| 田阳县| 慈利县| 武川县| 平凉市| 海南省| 如东县| 英超| 二连浩特市| 通江县| 奉节县| 烟台市| 博客| 罗山县| 石景山区| 汨罗市| 望都县| 上杭县| 兴仁县| 大庆市| 江都市| 湘乡市| 昂仁县|