Home »
Java programming language
Java SecurityManager checkConnect() method with example
SecurityManager Class checkConnect() method: Here, we are going to learn about the checkConnect() method of SecurityManager Class with its syntax and example.
Submitted by Preeti Jain, on December 14, 2019
Syntax:
public void checkConnect(String host_add, int port_no);
public void checkConnect(String host_add, int port_no, Object cntxt);
SecurityManager Class checkConnect() method
- checkConnect() method is available in java.lang package.
- checkConnect(String host_add, int port_no) method is called checkPermission with SocketPermission(host ":" + port_no,"connect") if and only if when port_no is not equal to -1 otherwise it calls checkPermission with SocketPermission(host_add,"resolve") if and only if port_no is equal to 1.
- checkConnect(String host_add, int port_no, Object cntxt) method is calls checkPermission with SocketPermission(host ":" + port_no,"connect") if and only when port_no is not equal to -1 and cntxt parameter is an instance of AccessControlContext otherwise it calls checkPermission with SocketPermission(host_add,"resolve") if and only if when port_no is equal to -1 and cntxt parameter is an instance of AccessControlContext.
-
checkConnect(String host_add, int port_no), checkConnect(String host_add, int port_no, Object cntxt) methods may throw an exception at the time of checking connection.
- SecurityException: checkConnect(String host_add, int port_no) - This exception may throw when the calling thread does not have the authority to open a socket connection to the given host and port_no.
- SecurityException: checkConnect(String host_add, int port_no, Object cntxt) - This exception may throw when the given cntxt (context) does not have authority to open a socket connection to the given host and port_no.
- These methods 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, "String host_add, int port_no"
String host_add - This parameter represents the host to connect to protocol.
int port_no – This parameter represents the port number to connect to.
-
In the second case, "String host_add, int port_no, Object cntxt"
String host_add – Similar as defined in the first case.
int port_no - Similar as defined in the second case.
Object cntxt – This parameter represents the system-specific security context.
Return value:
The return type of this method is void, it returns nothing.
Example:
// Java program to demonstrate the example
// of checkConnect () method of SecurityManager class
import java.security.*;
public class CheckConnect {
public static void main(String[] args) {
String host_add = "www.includehelp.com";
int port_no = 8080;
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 SecurityManager object
SecurityManager smgr = new SecurityManager();
// By using setSecurityManager() method is to set the
// security manager
System.setSecurityManager(smgr);
// By using checkConnect(host_add,port_no) method is to //check that
// connection is enabled or not
smgr.checkConnect(host_add, port_no);
// By using checkConnect(host_add,port_no,cntxt) method is //to check that
// connection is enabled or not or when cntxt is an instance of
// AccessControlContext
smgr.checkConnect(host_add, port_no, cntxt);
// Display the message when connection is enabled
System.out.println("Accepted..");
}
}
Output
Exception in thread "main" java.security.AccessControlException: access denied ("java.net.SocketPermission" "www.includehelp.com:8080" "connect,resolve")
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.checkConnect(SecurityManager.java:824)
at CheckConnect.main(CheckConnect.java:26)