Listing All Permissions Granted to Classes Loaded from a URL or Directory : SecurityManager « Security « Java Tutorial






import java.io.File;
import java.net.URL;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Policy;
import java.security.cert.Certificate;
import java.util.Enumeration;

public class Main {
  public static void main(String[] argv) throws Exception {

    SecurityManager sm = new SecurityManager();
    System.setSecurityManager(sm);
    URL codebase = new URL("http://java.sun.com/");

    //codebase = new File("c:\\java\\").toURI().toURL();
    //codebase = new File(System.getProperty("user.home")).toURI().toURL();

    CodeSource cs = new CodeSource(codebase, (Certificate[])null);

    PermissionCollection pcoll = Policy.getPolicy().getPermissions(cs);

    Enumeration e = pcoll.elements();
    for (; e.hasMoreElements();) {
      Permission p = (Permission) e.nextElement();
    }
  }
}








36.41.SecurityManager
36.41.1.extends SecurityManager
36.41.2.Use SecurityManager to check AWT permission and file permission
36.41.3.Listing All Permissions Granted to Classes Loaded from a URL or Directory
36.41.4.Define your own security manager
36.41.5.Enabling the Security Manager
36.41.6.To specify an additional policy file, set the java.security.policy system property at the command line:
36.41.7.To ignore the policies in the java.security file, and use the specified policy, use '==' instead of '='