📜  Java Java类

📅  最后修改于: 2022-05-13 01:54:28.930000             🧑  作者: Mango

Java Java类

此类用于财产许可。它扩展了 BasicPermission 。名称是属性的名称(“Java.home”、“os.name”等)。命名约定遵循分层属性命名约定。此外,星号可能出现在名称的末尾,跟在“.”之后,或者单独出现,表示通配符匹配。例如:“Java.*”或“*”有效,“* Java”或“a*b”无效。

在授予代码访问某些系统属性的权限之前,应小心谨慎。例如,授予访问“Java.home”系统属性的权限会提供有关系统环境( Java安装目录)的潜在恶意代码敏感信息。此外,授予访问“user.name”和“user.home”系统属性的权限会提供有关用户环境(用户的帐户名和主目录)的潜在恶意代码敏感信息。

构造函数:
PropertyPermission(字符串名称,字符串操作):

  • 创建具有指定名称的新 PropertyPermission 对象。
  • 要授予的操作在包含一个或多个逗号分隔关键字列表的字符串中传递给构造函数。可能的关键字是“读”和“写”。它们的含义定义如下:
    读:读权限。允许调用 System.getProperty。
    写:写权限。允许调用 System.setProperty。

方法:

  1. boolean equals(Object obj):检查两个 PropertyPermission 对象是否相等。检查 obj 是否为 PropertyPermission,并且与此对象具有相同的名称和操作。它覆盖类BasicPermission中的equals
    Syntax: public boolean equals(Object obj).
    Returns: true if obj is a PropertyPermission, and 
    has the same name and actions as this PropertyPermission object.
    Exception: NA.
    
    // Java code illustrating equals() method
    import java.util.*;
    class PropertyPermissionDemo
    {
        public static void main(String arg[])
        {
           // creating property permission
           PropertyPermission gfg = (new PropertyPermission("os.name", "read"));
           if(gfg.equals(new PropertyPermission("os.name", "read")))
               System.out.println("both have same name and action");
           else
               System.out.println("both have different name or action");
        }
    }
    

    输出:

    both have same name and action
    
  2. String getActions():返回动作的“规范字符串表示”。也就是说,此方法始终按以下顺序返回当前操作:读、写。它覆盖了BasicPermission类中的getActions
    Syntax: public String getAction().
    Returns: the canonical string representation of the actions.
    Exception: NA.
    
    // Java code illustrating getAction() method
    import java.util.*;
    class PropertyPermissionDemo
    {
        public static void main(String arg[])
        {
           // creating property permission
           PropertyPermission gfg = (new PropertyPermission("os.name",
                       "read,write"));
           System.out.print(gfg.getName() + " has permission to " 
                    + gfg.getActions());
        }
    }
    

    输出:

    os.name has permission to read,write
    
  3. int hashCode():返回此对象的哈希码值。使用的哈希码就是这个权限名称的哈希码,即getName().hashCode(),其中getName来自Permission超类。它覆盖了BasicPermission中的hashCode
    Syntax: public int hashCode().
    Returns: a hash code value for this object.
    Exception: NA.
    
    // Java code illustrating hashCode() method
    import java.util.*;
    class PropertyPermissionDemo
    {
        public static void main(String arg[])
        {
           // creating property permission
           PropertyPermission gfg = (new PropertyPermission("os.name",
                  "read,write"));
           System.out.print("hash code of the object is: " +
                  gfg.getName().hashCode());
        }
    }
    

    输出:

    hash code of the object is: -1228098475
    
  4. 布尔暗示(权限 p):此方法检查此 PropertyPermission 是否暗示指定的权限。这是通过检查 p 是 PropertyPermission 对象来完成的,p 的动作是这个对象动作的子集,如果这个对象的名称暗示 p 的动作。它覆盖了类BasicPermission中的暗示
    Syntax: public boolean implies(Permission p).
    Returns: true if obj is a PropertyPermission, and 
    has the same name and actions as this PropertyPermission object.
    Exception: NA.
    
    // Java code illustrating implies() method
    import java.util.*;
    class PropertyPermissionDemo
    {
        public static void main(String arg[])
        {
           // creating property permission
           PropertyPermission gfg = (new PropertyPermission("os.name",
                   "read,write"));
           if(gfg.implies(new PropertyPermission("os.name",
                   "read,write")))
               System.out.println(gfg.getName() + " has permission " 
                       + gfg.getActions());
        }
    }
    

    输出:

    os.name has permission read,write
    
  5. PermissionCollection newPermissionCollection():返回一个新的 PermissionCollection 对象,用于存储 PropertyPermission 对象。
    Syntax: public PermissionCollection newPermissionCollection().
    Returns: a new PermissionCollection object suitable 
    for storing PropertyPermissions.
    Exception: NA.
    
    // Java code illustrating newPermissionCollection() method
    import java.security.PermissionCollection;
    import java.util.*;
    class PropertyPermissionDemo
    {
        public static void main(String arg[])
        {
           // creating property permission
           PropertyPermission gfg = (new PropertyPermission("os.name",
                   "read"));
           // create property permissions collection
           PermissionCollection permission;
           permission = gfg.newPermissionCollection();
           permission.add(gfg);
           permission.add(new PropertyPermission("java.home.*",
                   "read,write"));
             
           if(permission.implies(gfg))
               System.out.println("java.home.*" + " has permission to "
                       + "read" );
             
        }
    }
    

    输出:

    java.home.* has permission to read