Java.lang - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.lang Security

Java.lang Security

shape Description

The java.lang.SecurityManager class permits applications to actualize a security arrangement. It permits an application to decide, before playing out a potentially perilous or delicate operation, what the operation is and whether it is being endeavored in a security setting that permits the operation to be performed. The application can permit or refuse the operation. Following is the syntax for java.lang.SecurityManager class. [java] public class SecurityManager extends Object [/java]

Methods and Constructor

shape Methods

Following are the methods of java.lang SecurityManager class.
Methods Description
checkAccept(String, int) It Tosses a SecurityException if the calling thread is not allowed to acknowledge an attachment association from the predetermined host and port number.
checkAccess(Thread) Tosses a SecurityException if the calling thread is not permitted to change the string parameter.
checkConnect(String, int) Tosses a SecurityException if the calling string is not permitted to open an attachment association with the predetermined host and port number.
checkExit(int) Tosses a SecurityException if the calling string is not permitted to bring about the Java Virtual Machine to end with the predetermined status code.
checkLink(String) Tosses a SecurityException if the calling string is not permitted to element connect the library code determined by the string contention record.

shape Constructor

Following are the constructors of java.lang SecurityManager.
Constructor Description
SecurityManager() To build the new SecurityManager

shape Example

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]

Summary

shape Key Points

  • The inCheck is the field of SecurityManager class.
  • The classDepth(String) method is used To give back the stack profundity of the predetermined class.