Java.util.PropertyPermission.newPermission Collection() の使い方

投稿者: | 2017年6月17日

Javaコード
package Permission;

import java.security.PermissionCollection;
import java.util.PropertyPermission;

public class NewPermissionDemo {
private static PermissionCollection permissions;

public static void main(String[] args) {
// Build property permissions collection
PropertyPermission permission =
new PropertyPermission(“java.*”, “read”);
permissions = permission.newPermissionCollection();
permissions.add(permission);
permissions.add(new PropertyPermission(“java.changfa.*”, “read,write”));

// Check permissions
checkFileReadPermission(“java.changfa”);
checkFileWritePermission(“java.changfa”);
checkFileReadPermission(“java.changfa.usr”);
checkFileWritePermission(“java.changfa.usr”);
}

private static void checkFileReadPermission(String path) {
// Check permission on read action
if(permissions.implies(new PropertyPermission(path, “read”))) {
System.out.println(“Has permissions on “+path+” 読込み権限”);
}
}

private static void checkFileWritePermission(String path) {
// Check permission on read action
if(permissions.implies(new PropertyPermission(path, “write”))) {
System.out.println(“Has permissions on “+path+” 書込み権限”);
}
}
}

実行結果
Has permissions on java.changfa 読込み権限
Has permissions on java.changfa.usr 読込み権限
Has permissions on java.changfa.usr 書込み権限

カテゴリー: Java