Following is an example by using java.lang.SecurityManager.checkAccept(String host, int port) method.
DemoSecurityManager.java
[java]
package com.SPlessons;
public class DemoSecurityManager {
public static void main(String[] args) {
// set the policy file as the system security policy
System.setProperty("java.security.policy", "file:/C:/java.policy");
// create a security manager
SecurityManager sm = new SecurityManager();
// set the system security manager
System.setSecurityManager(sm);
// check if accepting socket connection is enabled
sm.checkAccept("www.splessons.com", 8080);
// print a message if we passed the check
System.out.println("Allowed!");
}
}
[/java]
Following is the syntax declaration of this method.
[java]public void checkAccept(String host, int port)[/java]
Output: When compile the code result will be as follows.
[java]
Exception in thread "main" java.security.AccessControlException: access denied (java.net.SocketPermission www.splessons.com:8080 accept,resolve)
[/java]
Where
port
is the port number of the socket connection.
By using java.lang.SecurityManager.checkAccess(Thread t) method
[java]
package com.SPlessons;
public class DemoSecurityManager extends SecurityManager {
// check access needs to overriden
@Override
public void checkAccess(Thread t) {
throw new SecurityException("Not allowed.");
}
public static void main(String[] args) {
// set the policy file as the system securuty policy
System.setProperty("java.security.policy", "file:/C:/java.policy");
// create a security manager
DemoSecurityManager sm = new DemoSecurityManager();
// set the system security manager
System.setSecurityManager(sm);
// check if accepting access for thread is enabled
sm.checkAccess(Thread.currentThread());
// print a message if we passed the check
System.out.println("Allowed!");
}
}
[/java]
Following is the syntax declaration of this method.
[java]public void checkAccess(Thread t)[/java]
Where
t
is the thread to be checked.
Output: When compile the code result will be as follows.
[java]
Exception in thread "main" java.lang.SecurityException: Not allowed.
at com.SPlessons.DemoSecurityManager.checkAccess(DemoSecurityManager.java:7)
at com.SPlessons.DemoSecurityManager.main(DemoSecurityManager.java:22)
[/java]
By using java.lang.SecurityManager.checkConnect(String host, int port) method
[java]
package com.SPlessons;
public class DemoSecurityManager extends SecurityManager {
public static void main(String[] args) {
// set the policy file as the system securuty policy
System.setProperty("java.security.policy", "file:/C:/java.policy");
// create a security manager
SecurityManager sm = new SecurityManager();
// set the system security manager
System.setSecurityManager(sm);
// perform the check
sm.checkConnect("www.splessons.com", 8080);
// print a message if we passed the check
System.out.println("Allowed!");
}
}
[/java]
The java.lang.SecurityManager.checkConnect(String host, int port) method does not return any value, the following is the syntax declaration.
[java]public void checkConnect(String host, int port)[/java]
Output: When compile the code result will be as follows.
[java]
Exception in thread "main" java.security.AccessControlException: access denied ("java.net.SocketPermission" "www.splessons.com:8080" "connect,resolve")
[/java]
By using java.lang.SecurityManager.checkExit(int status) method
[java]package com.SPlessons;
public class DemoSecurityManager extends SecurityManager {
// checkExit needs to be overridden.
@Override
public void checkExit(int status) {
throw new SecurityException("Not allowed.");
}
public static void main(String[] args) {
// set the policy file as the system securuty policy
System.setProperty("java.security.policy", "file:/C:/java.policy");
// create a security manager
DemoSecurityManager sm = new DemoSecurityManager();
// set the system security manager
System.setSecurityManager(sm);
// perform the check
sm.checkExit(5);
// print a message if we passed the check
System.out.println("Allowed!");
}
}
[/java]
Following is the syntax declaration of the java.lang.SecurityManager.checkExit(int status) method.
[java]public void checkExit(int status)[/java]
Output: When compile the code result will be as follows.
[java]Exception in thread "main" java.lang.SecurityException: Not allowed.[/java]
By using java.lang.SecurityManager.checkLink(String lib) method
[java]
package com.SPlessons;
public class DemoSecurityManager {
public static void main(String[] args) {
// set the policy file as the system securuty policy
System.setProperty("java.security.policy", "file:/C:/java.policy");
// create a security manager
SecurityManager sm = new SecurityManager();
// set the system security manager
System.setSecurityManager(sm);
// perform the check
sm.checkLink("JDBC - ODBC");
// print a message if we passed the check
System.out.println("Allowed!");
}
}
[/java]
Following is the syntax declaration of java.lang.SecurityManager.checkLink() method.
[java]public void checkLink(String lib)[/java]
Where
lib
is nothing but name of the library.
Output: When compile the code result will be as follows.
[java]
Exception in thread "main" java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "loadLibrary.JDBC - ODBC")
[/java]