日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java SecurityManager checkPermission()方法与示例

發布時間:2023/12/1 java 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java SecurityManager checkPermission()方法与示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Syntax:

句法:

public void checkPermission(Permission perm);public void checkPermission(Permission perm, Object cntxt);

SecurityManager類的checkPermission()方法 (SecurityManager Class checkPermission() method)

  • checkPermission() method is available in java.lang package.

    checkPermission()方法在java.lang包中可用。

  • checkPermission(Permission perm) method invokes checkPermission of AccesController for the requested access, indicated by the specified permissions.

    checkPermission(Permission perm)方法為請求的訪問調用AccesController的checkPermission,由指定的權限指示。

  • checkPermission(Permission perm, Object cntxt) method invokes checkPermission of AccesControlContext for the given security context is the access granted to the resource, indicated by the specified permissions when cntxt is an instance of AccessControlContext.

    當給定的安全上下文是授予資源的訪問權限時,checkPermission(Permission perm,Object cntxt)方法調用AccesControlContext的checkPermission,當cntxt是AccessControlContext的實例時,由指定的權限表示。

  • checkPermission(Permission perm), checkPermission(Permission perm, Object cntxt) methods may throw an exception at the time of granting permission.

    checkPermission(Permission perm)和checkPermission(Permission perm,Object cntxt)方法在授予權限時可能會引發異常。

  • checkPermission(Permission perm):

    checkPermission(權限燙發):

    • SecurityException – This exception may throw when the access is denied on the security policy held currently.
    • NullPointerException – This exception may throw when the given parameter is null.
  • checkPermission(Permission perm, Object cntxt):

    checkPermission(權限權限,對象cntxt):

    • SecurityException – This exception may throw when the calling thread is not allowed to access the resource by the given permission or when the security cntxt(context) is not an object of AccessControlContext.
    • NullPointerException – This exception may throw when the given first parameter is null.
  • These are non-static methods, it is accessible with the class object only and, if we try to access these methods with the class name then we will get an error.

    這些是非靜態方法,只能通過類對象訪問,如果嘗試使用類名稱訪問這些方法,則會收到錯誤消息。

Parameter(s):

參數:

  • In the first case, Permission perm - This parameter represents the requested permission.

    在第一種情況下, 權限權限 -此參數表示請求的權限。

  • In the second case, Permission perm, Object cntxt

    在第二種情況下, 權限權限,對象cntxt

    • Permission perm – Similar as defined in the first case.
    • 許可權限 –與第一種情況中定義的相似。
    • Object cntxt – This parameter represents the system-specific security context.
    • 對象cntxt –此參數表示系統特定的安全上下文。

Return value:

返回值:

The return type of this method is void, it returns nothing.

此方法的返回類型為void ,不返回任何內容。

Example:

例:

// Java program to demonstrate the example // of checkPermission() method of SecurityManager classimport java.security.*; import java.io.*;public class CheckPermission extends SecurityManager {public static void main(String[] args) {Permission perm = new FilePermission("getProperties().doc", "read,write");AccessControlContext acc = AccessController.getContext();// By using setProperty() method is to set the policy property // with security managerSystem.setProperty("java.security.policy", "file:/C:/java.policy");// Instantiating a CheckPermission objectCheckPermission cp = new CheckPermission();// By using setSecurityManager() method is to set the// security managerSystem.setSecurityManager(cp);// By using checkPermission(Permission) method is to// check that restricted permissioncp.checkPermission(perm);// By using checkPermission(Permission,Object) method is to// check that restricted permission when cntxt is an instance// of AccessControlContextcp.checkPermission(perm, acc);// Display the message System.out.println("Accepted..");} }

Output

輸出量

Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "getProperties().doc" "read,write")at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)at java.base/java.security.AccessController.checkPermission(AccessController.java:897)at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:322)at CheckPermission.main(CheckPermission.java:25)

翻譯自: https://www.includehelp.com/java/securitymanager-checkpermission-method-with-example.aspx

總結

以上是生活随笔為你收集整理的Java SecurityManager checkPermission()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。