Home » Java programming language

Java SecurityManager checkPermission() method with example

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

Syntax:

    public void checkPermission(Permission  perm);
    public void checkPermission(Permission  perm, Object cntxt);

SecurityManager Class checkPermission() method

  • checkPermission() method is available in java.lang package.
  • checkPermission(Permission perm) method invokes checkPermission of AccesController for the requested access, indicated by the specified permissions.
  • checkPermission(Permission perm, Object cntxt) method invokes checkPermission of AccesControlContext for the given security context is the access granted to the resource, indicated by the specified permissions when cntxt is an instance of AccessControlContext.
  • checkPermission(Permission perm), checkPermission(Permission perm, Object cntxt) methods may throw an exception at the time of granting permission.
  • checkPermission(Permission perm):
    • SecurityException – This exception may throw when the access is denied on the security policy held currently.
    • NullPointerException – This exception may throw when the given parameter is null.
  • checkPermission(Permission perm, Object cntxt):
    • SecurityException – This exception may throw when the calling thread is not allowed to access the resource by the given permission or when the security cntxt(context) is not an object of AccessControlContext.
    • NullPointerException – This exception may throw when the given first parameter is null.
  • These 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, Permission perm - This parameter represents the requested permission.
  • In the second case, Permission perm, Object cntxt
    • Permission perm – Similar as defined in the first case.
    • Object cntxt – This parameter represents the system-specific security context.

Return value:

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

Example:

// Java program to demonstrate the example 
// of checkPermission() method of SecurityManager class

import java.security.*;
import java.io.*;

public class CheckPermission extends SecurityManager {
    public static void main(String[] args) {
        Permission perm = new FilePermission("getProperties().doc", "read,write");
        AccessControlContext acc = 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 CheckPermission object
        CheckPermission cp = new CheckPermission();

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

        // By using checkPermission(Permission) method is to
        // check that restricted permission
        cp.checkPermission(perm);

        // By using checkPermission(Permission,Object) method is to
        // check that restricted permission when cntxt is an instance
        // of AccessControlContext
        cp.checkPermission(perm, acc);

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

Output

Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "getProperties().doc" "read,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 CheckPermission.main(CheckPermission.java:25)


Comments and Discussions!

Load comments ↻





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