📜  Java的.net.NetPermission类在Java中

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

Java的.net.NetPermission类在Java中

NetPermission类用于允许网络权限。 NetPermission 类扩展 BasicPermission 类。它是一个“命名”权限,即它包含一个名称但不包含任何操作。

Permission nameWhat permission allowsRisks associated with this permission
allowHttpTraceThis permission allows using the HTTP TRACE method in HttpURLConnectionAttackers may use HTTP TRACE to access security in the HTTP headers
getCookieHandlerThis permission allows getting the cookie handler that processes highly secure cookie information for this HTTP sessionAttackers may get a cookie handler and get access to highly secure cookie information
getNetworkInformationThis permission allows getting information about local network interfacesAttackers may get information about local hardware 
getProxySelectorThis permission allows the proxy selector to select which proxies to use when making network connectionsAttackers may get a ProxySelector and get information about the proxy hosts and ports of  internal networks
getResponseCache The permission allows accessing the local response cacheAttackers may get access the local cache which may contain security information
requestPasswordAuthenticationThis permission grants the ability to ask the authenticator for a passwordAttackers may steal this password
setCookieHandlerThis permission allows setting the cookie handler that processes highly secure cookie information for this HTTP sessionAttackers may get a cookie handler and get access to highly secure cookie information
setDefaultAuthenticatorThis allows to set an authenticatorAttackers may set an authenticator and get security information
setProxySelector This permission allows the proxy selector to set which proxies to use when making network connectionsAttackers may set a ProxySelector and get information about the proxy hosts and ports of  internal networks
setResponseCacheThe permission allows setting the local response cacheAttackers may get access the local cache which may contain security information
specifyStreamHandler The permission allows specifying a StreamHandler to create URLsAttackers may create a URL and get access to resources to which they normally not have access

语法:类声明

public final class NetPermission
extends BasicPermission

这个类的构造函数

ConstructorDescription
NetPermission(String name)Used for creating a new NetPermission object with the given name
NetPermission(String name, String action)Used for creating a new NetPermission object with the given name and action

从类Java.security.BasicPermission 继承的方法

MethodDescription
equals(Object obj)Checks whether the two BasicPermission objects are equal or not
getActions()Returns the actions in String format
hashCode()Returns the hash value for this object
implies(Permission permission)Checks whether the given permission is implied by this object or not
newPermissionCollection()Returns a new PermissionCollection object

从类Java.security.Permission 继承的方法



MethodDescription
checkGuard()Used to implement guard interface
getName()Returns name of this permission object
toString()Returns string representation of this permission object
Methods inherited from class java.lang.Object
clone(), finalize(), getClass(), notify(), notifyAll(), wait(), wait(), wait()

示例 1:

Java
// Java program to Create a New allow HttpTrace Permission
 
// Importing required network permission classes
import java.net.NetPermission;
import java.security.Permission;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Try block to check for exceptions
        try {
 
            // Creating a new allowHttpTrace permission
            Permission permission
                = new NetPermission("allowHttpTrace");
 
            // Printing the name of the permission using
            // getName() method
            System.out.println(permission.getName());
 
            // Printing the class of the permission using
            // getClass method
            System.out.println(permission.getClass());
 
            // Printing the hash value of this permission
            // object using hashCode() method
            System.out.println(permission.hashCode());
        }
 
        // Catch block to handle the exceptions
        catch (Exception e) {
 
            // Print the line number where the exception occurred
            e.printStackTrace();
        }
    }
}


Java
// Java Program to Create a New getCookieHandler Permission
 
// Importing required network permission classes
import java.net.NetPermission;
import java.security.Permission;
 
// Main Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Try block to check for exceptions
        try {
 
            // Creating a new getCookieHandler permission
            Permission permission
                = new NetPermission("getCookieHandler");
 
            // Printing the name of the permission using
            // getName() method
            System.out.println(permission.getName());
 
            // Printing the class of the permission using
            // getClass method
            System.out.println(permission.getClass());
 
            // Printing the hash value of this permission
            // object using hashCode() method
            System.out.println(permission.hashCode());
        }
 
        // Catch block to handle the exceptions
        catch (Exception e) {
 
            // Print the line number where exception occured
            // using printStackTrace() method
            e.printStackTrace();
        }
    }
}


Java
// Java Program to Illustrate the Working of equals() Method
 
// Importing permission classes for networking
import java.net.NetPermission;
import java.security.Permission;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Try block to check for exceptions
        try {
 
            // Creating a new getNetworkInformation
            // permission
            Permission Permission1 = new NetPermission(
                "getNetworkInformation");
 
            // Creating a new getProxySelector permission
            Permission Permission2
                = new NetPermission("getProxySelector");
 
            // Checking if both the given permissions are
            // equal or not using equals() method
            if (Permission1.equals(Permission2)) {
 
                // Print statement
                System.out.println(
                    "Both permission are equal");
            }
 
            // Statements differ
            else {
 
                // Print statement
                System.out.println(
                    "Both permission are not equal");
            }
        }
 
        // Catch block to handle the exceptions
        catch (Exception e) {
 
            // Print the line number where the exception occurred
            e.printStackTrace();
        }
    }
}


输出:

allowHttpTrace
class java.net.NetPermission
303901780

示例 2:

Java

// Java Program to Create a New getCookieHandler Permission
 
// Importing required network permission classes
import java.net.NetPermission;
import java.security.Permission;
 
// Main Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Try block to check for exceptions
        try {
 
            // Creating a new getCookieHandler permission
            Permission permission
                = new NetPermission("getCookieHandler");
 
            // Printing the name of the permission using
            // getName() method
            System.out.println(permission.getName());
 
            // Printing the class of the permission using
            // getClass method
            System.out.println(permission.getClass());
 
            // Printing the hash value of this permission
            // object using hashCode() method
            System.out.println(permission.hashCode());
        }
 
        // Catch block to handle the exceptions
        catch (Exception e) {
 
            // Print the line number where exception occured
            // using printStackTrace() method
            e.printStackTrace();
        }
    }
}

输出:

getCookieHandler
class java.net.NetPermission
1381623952

示例 3:

Java

// Java Program to Illustrate the Working of equals() Method
 
// Importing permission classes for networking
import java.net.NetPermission;
import java.security.Permission;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Try block to check for exceptions
        try {
 
            // Creating a new getNetworkInformation
            // permission
            Permission Permission1 = new NetPermission(
                "getNetworkInformation");
 
            // Creating a new getProxySelector permission
            Permission Permission2
                = new NetPermission("getProxySelector");
 
            // Checking if both the given permissions are
            // equal or not using equals() method
            if (Permission1.equals(Permission2)) {
 
                // Print statement
                System.out.println(
                    "Both permission are equal");
            }
 
            // Statements differ
            else {
 
                // Print statement
                System.out.println(
                    "Both permission are not equal");
            }
        }
 
        // Catch block to handle the exceptions
        catch (Exception e) {
 
            // Print the line number where the exception occurred
            e.printStackTrace();
        }
    }
}

输出:

Both permission are not equal