Home »
Java programming language
Java SecurityManager getSecurityContext() method with example
SecurityManager Class getSecurityContext() method: Here, we are going to learn about the getSecurityContext() method of SecurityManager Class with its syntax and example.
Submitted by Preeti Jain, on December 18, 2019
SecurityManager Class getSecurityContext() method
- getSecurityContext() method is available in java.lang package.
- getSecurityContext() method is used to return an object that encapsulates or binds the current execution environment and we don't override this method so it returns an object of "AccessControlContext".
- getSecurityContext() method is required to determine whether the untrusted method is eligible to execute on its own or not.
- getSecurityContext() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- getSecurityContext() method does not throw an exception at the time returning an object of executing the environment.
Syntax:
public Object getSecurityContext();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is Object, it returns an object that encapsulates enough information of an environment executing currently to operate some security validations.
Example:
// Java program to demonstrate the example
// of Object getSecurityContext() method of
// SecurityManager
import java.security.*;
public class GetSecurityContext {
public static void main(String[] args) {
// 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 getSecurityContext() method is to return the
// object of security context
Object ob = smgr.getSecurityContext();
// Display ob
System.out.println("Ob = " + ob);
}
}
Output
Ob = java.security.AccessControlContext@4d591d15