java.net.NetPermission Class in Java
Last Updated :
13 Jun, 2022
NetPermission class is used to allow network permissions. NetPermission class extends BasicPermission class. It is a “named” permission i.e it contains a name but no action.
Permission name | What permission allows | Risks associated with this permission |
---|
allowHttpTrace | This permission allows using the HTTP TRACE method in HttpURLConnection | Attackers may use HTTP TRACE to access security in the HTTP headers |
getCookieHandler | This permission allows getting the cookie handler that processes highly secure cookie information for this HTTP session | Attackers may get a cookie handler and get access to highly secure cookie information |
getNetworkInformation | This permission allows getting information about local network interfaces | Attackers may get information about local hardware |
getProxySelector | This permission allows the proxy selector to select which proxies to use when making network connections | Attackers may get a ProxySelector and get information about the proxy hosts and ports of internal networks |
getResponseCache | The permission allows accessing the local response cache | Attackers may get access the local cache which may contain security information |
requestPasswordAuthentication | This permission grants the ability to ask the authenticator for a password | Attackers may steal this password |
setCookieHandler | This permission allows setting the cookie handler that processes highly secure cookie information for this HTTP session | Attackers may get a cookie handler and get access to highly secure cookie information |
setDefaultAuthenticator | This allows to set an authenticator | Attackers may set an authenticator and get security information |
setProxySelector | This permission allows the proxy selector to set which proxies to use when making network connections | Attackers may set a ProxySelector and get information about the proxy hosts and ports of internal networks |
setResponseCache | The permission allows setting the local response cache | Attackers may get access the local cache which may contain security information |
specifyStreamHandler | The permission allows specifying a StreamHandler to create URLs | Attackers may create a URL and get access to resources to which they normally not have access |
Syntax: Class declaration
public final class NetPermission
extends BasicPermission
Constructors of this class
Constructor | Description |
---|
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 |
Methods inherited from class java.security.BasicPermission
Method | Description |
---|
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 |
Methods inherited from class java.security.Permission
Method | Description |
---|
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() |
Example 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();
}
}
}
Output:
allowHttpTrace
class java.net.NetPermission
303901780
Example 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 occurred
// using printStackTrace() method
e.printStackTrace();
}
}
}
Output:
getCookieHandler
class java.net.NetPermission
1381623952
Example 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();
}
}
}
Output:
Both permission are not equal
Similar Reads
java.net.SocketPermission Class in Java The java.net.SocketPermisson class represents whether you have permission to access a network via sockets. A SocketPermission consists of a host and a set of actions. Class Declaration: public final class SocketPermission extends Permission implements SerializableConstructor: ConstructorDescriptionM
2 min read
java.lang.reflect.ReflectPermission Class in Java ReflectPermission class extends BasicPermission class. It is a ânamedâ permission i.e it contains a name but no action. It may implement actions on top of BasicPermission, if desired. It is used to get information about the behaviour of Constructors. ConstructorsDescriptionReflectPermission(String n
2 min read
java.net.SocketOption Class in Java The java.net.SocketOption is a socket option that is connected with a socket, as the set of channels packages is java.nio.channels.NetworkChannel that this interface has defined the setOption as well as the getOption methods to set and query's the channels within its Socket Options. --> java.net
5 min read
java.net.Proxy Class in Java A proxy is an immutable object and type of tool or application or program or system, which helps to protect the information of its users and computers. It acts as a barrier between computer and internet users. A Proxy Object defines the Proxy settings to be used with a connection. Proxy servers are
3 min read
java.net.URLConnection Class in Java URLConnection Class in Java is an abstract class that represents a connection of a resource as specified by the corresponding URL. It is imported by the java.net package. The URLConnection class is utilized for serving two different yet related purposes, Firstly it provides control on interaction wi
5 min read
Java.util.PropertyPermission class in Java This class is for property permission. It extends BasicPermission. The name is the name of the property ("java.home", "os.name", etc). The naming convention follows the hierarchical property naming convention. Also, an asterisk may appear at the end of the name, following a ".", or by itself, to sig
4 min read