security

Check if a permission implies another permission example

In this example we shall show you how to check if a permission to a file implies another permission. To check the permissions to a specific file one should perform the following steps:

  • Create a new FilePermission with a given String as path and another given String as actions.
  • Create a second FilePermission with a another String as path and another String as actions. The second path is a path to a file that is a subdirectory of the file in the previous path.
  • Invoke the implies(Permission permission) API method of the Permission, for the first permission, using as parameter the second permission. The method returns true if the first permission implies the second one and false otherwise,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;
 
import java.io.FilePermission;
import java.security.Permission;
 
public class PermissionImplications {
 
  public static void main(String[] args) {

    String path1 = "/home/*";
    String actions1 = "read,write";
    Permission permission1 = new FilePermission(path1, actions1);

    String path2 = "/home/documents";
    String actions2 = "read";
    Permission permission2 = new FilePermission(path2, actions2);

    if (permission1.implies(permission2)) {

 System.out.println(actions1 + " on " + path1 + " implies " + actions2 + " on " + path2);
    }

  }
}

Output:

read,write on /home/* implies read on /home/documents

 
This was an example of how to check if a permission to a file implies another permission in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button