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 class
import 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 manager
System.setProperty("java.security.policy", "file:/C:/java.policy");
// Instantiating a CheckPermission object
CheckPermission cp = new CheckPermission();
// By using setSecurityManager() method is to set the
// security manager
System.setSecurityManager(cp);
// By using checkPermission(Permission) method is to
// check that restricted permission
cp.checkPermission(perm);
// By using checkPermission(Permission,Object) method is to
// check that restricted permission when cntxt is an instance
// of AccessControlContext
cp.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