Home » Java programming language

Java SecurityManager checkSecurityAccess() method with example

SecurityManager Class checkSecurityAccess() method: Here, we are going to learn about the checkSecurityAccess() method of SecurityManager Class with its syntax and example.
Submitted by Preeti Jain, on December 17, 2019

SecurityManager Class checkSecurityAccess() method

  • checkSecurityAccess() method is available in java.lang package.
  • checkSecurityAccess() method is used to check whether the permission with the given permission t_name(target name) should be allowed (i.e. granted) or not allowed (i.e. denied) and it calls checkPermission with the SecurityPermission object for the given t_name(target name).
  • checkSecurityAccess() 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.
  • checkSecurityAccess() method may throw an exception at the time of checking granted permission.
    • SecurityException – This exception may throw when the calling thread is denied for the requested access.
    • NullPointerException – This exception may throw when the given parameter t_name is null.
    • IllegalArgumentException – This exception may throw when the given parameter t_name is blank or empty.

Syntax:

    public void checkSecurityAccess(String t_name);

Parameter(s):

  • String t_name – represents the target name of the SecurityPermission.

Return value:

The return type of this method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void checkSecurityAccess(String t_name)
// method of SecurityManager 

public class CheckSecurityAccess extends SecurityManager {
    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 CheckSecurityAccess object
        CheckSecurityAccess csa = new CheckSecurityAccess();

        // By using setSecurityManager() method is to set the
        // security manager
        System.setSecurityManager(csa);

        // By using checkSecurityAccess(string t_name) method is to
        //check restriction on which access
        csa.checkSecurityAccess("write");

        // Display the message
        System.out.println("Not Restricted..");
    }
}

Output

Exception in thread "main" java.security.AccessControlException: access denied ("java.security.SecurityPermission" "write")
	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.checkSecurityAccess(SecurityManager.java:1435)
	at CheckSecurityAccess.main(CheckSecurityAccess.java:20)


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.