Search in sources :

Example 76 with PermissionCollection

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);
    }
}
Also used : PermissionCollection(java.security.PermissionCollection) FilePermission(java.io.FilePermission)

Example 77 with PermissionCollection

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;
}
Also used : Policy(java.security.Policy) PermissionCollection(java.security.PermissionCollection) CodeSource(java.security.CodeSource)

Example 78 with PermissionCollection

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;
}
Also used : PermissionCollection(java.security.PermissionCollection) FilePermission(java.io.FilePermission) Permission(java.security.Permission) IOException(java.io.IOException) FilePermission(java.io.FilePermission) URL(java.net.URL)

Example 79 with PermissionCollection

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;
}
Also used : PermissionCollection(java.security.PermissionCollection) FilePermission(java.io.FilePermission) Permission(java.security.Permission) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) FilePermission(java.io.FilePermission) URL(java.net.URL)

Example 80 with PermissionCollection

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);
}
Also used : PermissionCollection(java.security.PermissionCollection) ProtectionDomain(java.security.ProtectionDomain) AccessControlContext(java.security.AccessControlContext) Permissions(java.security.Permissions) Permission(java.security.Permission) CodeSource(java.security.CodeSource) Certificate(java.security.cert.Certificate)

Aggregations

PermissionCollection (java.security.PermissionCollection)107 Permission (java.security.Permission)39 Permissions (java.security.Permissions)29 CodeSource (java.security.CodeSource)25 FilePermission (java.io.FilePermission)20 ProtectionDomain (java.security.ProtectionDomain)19 AllPermission (java.security.AllPermission)16 Policy (java.security.Policy)15 URL (java.net.URL)14 File (java.io.File)10 IOException (java.io.IOException)10 Certificate (java.security.cert.Certificate)8 AccessControlContext (java.security.AccessControlContext)7 PropertyPermission (java.util.PropertyPermission)7 Test (org.junit.Test)7 SocketPermission (java.net.SocketPermission)6 Method (java.lang.reflect.Method)5 Principal (java.security.Principal)5 PrivilegedActionException (java.security.PrivilegedActionException)5 URLClassLoader (java.net.URLClassLoader)4