use of java.security.PermissionCollection in project Java-Tutorial by gpcodervn.
the class FilePermissionExample method main.
public static void main(String[] args) throws IOException {
String srg = "D:\\WorkSpace\\gpcoder\\JavaIOTutorial\\data\\java.txt";
// cấp quyền cho path1
FilePermission file1 = new FilePermission("D:\\WorkSpace\\gpcoder\\JavaIOTutorial\\data\\-", "read");
PermissionCollection permission = file1.newPermissionCollection();
permission.add(file1);
// cấp quyền cho path2
FilePermission file2 = new FilePermission(srg, "write");
permission.add(file2);
if (permission.implies(new FilePermission(srg, "read,write"))) {
System.out.println("Read, Write permission is granted for the path " + srg);
} else {
System.out.println("No Read, Write permission is granted for the path " + srg);
}
}
use of java.security.PermissionCollection in project tomcat70 by apache.
the class WebappClassLoaderBase method check.
@Override
public boolean check(Permission permission) {
if (!Globals.IS_SECURITY_ENABLED) {
return true;
}
Policy currentPolicy = Policy.getPolicy();
if (currentPolicy != null) {
ResourceEntry entry = findResourceInternal("/", "/", false);
if (entry != null) {
CodeSource cs = new CodeSource(entry.codeBase, (java.security.cert.Certificate[]) null);
PermissionCollection pc = currentPolicy.getPermissions(cs);
if (pc.implies(permission)) {
return true;
}
}
}
return false;
}
use of java.security.PermissionCollection in project Bytecoder by mirkosertic.
the class BuiltinClassLoader method getPermissions.
// -- permissions
/**
* Returns the permissions for the given CodeSource.
*/
@Override
protected PermissionCollection getPermissions(CodeSource cs) {
PermissionCollection perms = super.getPermissions(cs);
// add the permission to access the resource
URL url = cs.getLocation();
if (url == null)
return perms;
// avoid opening connection when URL is to resource in run-time image
if (url.getProtocol().equals("jrt")) {
perms.add(new RuntimePermission("accessSystemModules"));
return perms;
}
// open connection to determine the permission needed
try {
Permission p = url.openConnection().getPermission();
if (p != null) {
// for directories then need recursive access
if (p instanceof FilePermission) {
String path = p.getName();
if (path.endsWith(File.separator)) {
path += "-";
p = new FilePermission(path, "read");
}
}
perms.add(p);
}
} catch (IOException ioe) {
}
return perms;
}
use of java.security.PermissionCollection in project Bytecoder by mirkosertic.
the class Loader method getPermissions.
// -- permissions
/**
* Returns the permissions for the given CodeSource.
*/
@Override
protected PermissionCollection getPermissions(CodeSource cs) {
PermissionCollection perms = super.getPermissions(cs);
URL url = cs.getLocation();
if (url == null)
return perms;
// add the permission to access the resource
try {
Permission p = url.openConnection().getPermission();
if (p != null) {
// for directories then need recursive access
if (p instanceof FilePermission) {
String path = p.getName();
if (path.endsWith(File.separator)) {
path += "-";
p = new FilePermission(path, "read");
}
}
perms.add(p);
}
} catch (IOException ioe) {
}
return perms;
}
use of java.security.PermissionCollection in project herd by FINRAOS.
the class SecurityManagerHelper method doPrivileged.
/**
* Executes the given {@link PrivilegedAction} using the given collection of {@link Permission}.
*
* @param <T> the return type
* @param privilegedAction the {@link PrivilegedAction} to execute
* @param permissions collection of permissions to apply. May be empty but not null.
* @return return value of the privileged action
*/
public static <T> T doPrivileged(PrivilegedAction<T> privilegedAction, Collection<Permission> permissions) {
CodeSource codeSource = new CodeSource(null, (Certificate[]) null);
PermissionCollection permissionCollection = new Permissions();
for (Permission permission : permissions) {
permissionCollection.add(permission);
}
ProtectionDomain protectionDomain = new ProtectionDomain(codeSource, permissionCollection);
ProtectionDomain[] protectionDomains = { protectionDomain };
AccessControlContext accessControlContext = new AccessControlContext(protectionDomains);
return AccessController.doPrivileged(privilegedAction, accessControlContext);
}
Aggregations