Syntax:
句法:
public void checkConnect(String host_add, int port_no);
public void checkConnect(String host_add, int port_no, Object cntxt);
SecurityManager类的checkConnect()方法 (SecurityManager Class checkConnect() method)
checkConnect() method is available in java.lang package.
checkConnect()方法在java.lang包中可用。
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.
当且仅当port_no不等于-1时,才将 checkConnect(String host_add,int port_no)方法称为checkPermission和SocketPermission(host“:” + port_no,“ connect”) ,否则,将调用带有SocketPermission(host_add,“ resolve”的checkPermission) )仅当port_no等于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.
当且仅当port_no不等于-1并且cntxt参数是AccessControlContext的实例时, checkConnect(String host_add,int port_no,Object cntxt)方法才用SocketPermission(host“:” + port_no,“ connect”)调用checkPermission当且仅当port_no等于-1并且cntxt参数是AccessControlContext的实例时,才使用SocketPermission(host_add,“ resolve”)调用checkPermission。
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.
checkConnect(String host_add,int port_no) , checkConnect(String host_add,int port_no,Object cntxt)方法在检查连接时可能会引发异常。
- 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.checkConnect(String host_add,int port_no) -当调用线程无权打开与给定主机和port_no的套接字连接时,可能会引发此异常。
- 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.checkConnect(String host_add,int port_no,Object cntxt) -当给定的cntxt(上下文)无权打开与给定的主机和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"
在第一种情况下, “字符串host_add,int port_no”
String host_add - This parameter represents the host to connect to protocol.
字符串host_add-此参数表示要连接到协议的主机。
int port_no – This parameter represents the port number to connect to.
int port_no –此参数表示要连接的端口号。
In the second case, "String host_add, int port_no, Object cntxt"
在第二种情况下, “字符串host_add,int port_no,对象cntxt”
String host_add – Similar as defined in the first case.
字符串host_add –与第一种情况下定义的相似。
int port_no - Similar as defined in the second case.
int port_no-与第二种情况中定义的类似。
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 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)
翻译自: https://www.includehelp.com/java/securitymanager-checkconnect-method-with-example.aspx