Java SecurityManager checkRead()方法与示例

Syntax:

句法:

    public void checkRead(FileDescriptor  file_des);
    public void checkRead(String fi);
    public void checkRead(String fi , Object cntxt);

SecurityManager类的checkRead()方法 (SecurityManager Class checkRead() method)

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

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

  • checkRead(FileDescriptor file_des) method invokes checkPermission with the RuntimePermission("readFileDescriptor") to read the file from the given file descriptor.

    checkRead(FileDescriptor file_des)方法使用RuntimePermission(“ readFileDescriptor”)调用checkPermission从给定的文件描述符中读取文件。

  • checkRead(String fi) method invokes checkPermission with the RuntimePermission(fi,"read") to read the file from the given fi parameter.

    checkRead(String fi)方法使用RuntimePermission(fi,“ read”)调用checkPermission以从给定的fi参数读取文件。

  • checkRead(String fi , Object cntxt) method invokes checkPermission with the FilePermission(fi,"read") to read file from the given fi parameter when cntxt is an instance of AccessControlContext otherwise it may throw an exception when cntxt is not instance of AccessControlContext.checkRead(FileDescriptor file_des), checkRead(String fi), checkRead(String fi , Object cntxt) methods may throw an exception at the time of reading file from different media.

    当cntxt是AccessControlContext的实例时,checkRead(String fi,Object cntxt)方法将使用FilePermission(fi,“ read”)调用checkPermission以从给定的fi参数读取文件,否则,当cntxt不是AccessControlContext的实例时,它可能会引发异常。从不同媒体读取文件时,checkRead(FileDescriptor file_des),checkRead(String fi),checkRead(String fi,Object cntxt)方法可能会引发异常。

  • checkRead(FileDescriptor file_des):

    checkRead(FileDescriptor file_des):

    • SecurityException – This exception may throw when the calling thread is not allowed to read the file from the given file descriptor.
    • NullPointerException – This exception may throw when the given parameter is null.
  • checkRead(String fi):

    checkRead(String fi):

    • SecurityException – This exception may throw when the calling thread is not allowed to read the file from the given fi(file) parameter.
    • NullPointerException – This exception may throw when the given parameter is null.
  • checkRead(String fi , Object cntxt):

    checkRead(String fi,Object cntxt):

    • SecurityException – This exception may throw when the calling thread is not allowed to read files from the given fi (file) parameter or when cntxt(context) parameter is not an instance of AccessControlContext.
    • NullPointerException – This exception may throw when the given 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, FileDescriptor file_des - This parameter represents the system-specific file descriptor.

    在第一种情况下, FileDescriptor file_des-此参数表示系统特定的文件描述符。

  • In the second case, String fi - This parameter represents the system-specific file name.

    在第二种情况下, 字符串fi-此参数表示系统特定的文件名。

  • In the third case, "String fi, Object cntxt"

    在第三种情况下, “字符串fi,对象cntxt”

    • String fi – Similar as defined in the second case.
    • 字符串fi –与第二种情况中定义的相似。
    • 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 checkRead () method of SecurityManager class

import java.security.*;
import java.io.*;

public class CheckRead extends SecurityManager {
     
    public static void main(String[] args) {
     
        FileDescriptor file_desc = new FileDescriptor();
        String fi = "getProperties().doc";
        AccessControlContext cntxt = 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 CheckRead object
        CheckRead cr = new CheckRead();

        // By using setSecurityManager() method is to set the
        // security manager
        System.setSecurityManager(cr);

        // By using checkRead(FileDescriptor) method is to
        //check that read file by using file descriptor
        cr.checkRead(file_desc);

        // By using checkRead(String) method is to
        // check that read file by using String 
        cr.checkRead(fi);

        // By using checkRead(String,cntxt) method is to
        // check that read file by using String when cntxt is an 
        // instance of AccessControlContext
        cr.checkRead(fi, cntxt);

        // Display the message 
        System.out.println("Accepted..");
    }
}

Output

输出量

Exception in thread "main" java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "readFileDescriptor")
	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 java.base/java.lang.SecurityManager.checkRead(SecurityManager.java:637)
	at CheckRead.main(CheckRead.java:26)


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

你可能感兴趣的:(字符串,java,python,linux,jvm)