mercredi 19 septembre 2018

SecurityManager is not allowing read file although stated in the policy

This is my error:

java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\Temp\SettingsApp.policy" "read")

This is my policy (dynamically made for each app)

grant signedBy "SmartMirror" {
    permission java.lang.RuntimePermission "setSecurityManager";
    permission java.lang.RuntimePermission "getSecurityManager";
    permission java.lang.RuntimePermission "createSecurityManager";
    permission java.lang.RuntimePermission "usePolicy";
};

grant signedBy "currentUser" codeBase "file:/C:/Temp/SettingsApp.jar" {
    permission java.io.FilePermission "C:/Temp/SettingsApp.policy", "read";
};

This is how I am setting the policy and using the security manager:

System.setSecurityManager(null); // reset so can reload new policy
System.setProperty("java.security.policy", "file:/C:/Temp/" + app.getName() + ".policy");
System.setSecurityManager(new SecurityManager()); // instance with new policy
JPanel panel = (JPanel) app.getObject().newInstance(); // instance Class <?>

The jar file is being loaded like so and works fine:

try {
    appLoader = URLClassLoader.newInstance(new URL[] { download }); // download is a URL of the C:/Temp/SettingsApp.jar
    appBuilder = appLoader.loadClass("iezon.app." + name); // name is SettingsApp
} catch (ClassNotFoundException e) {
    // TODO: Add class not found error for Application (App Developer issue)
}

When the JPanel panel = (JPanel) app.getObject().newInstance(); is first run, it works fine and the JPanel is returned. However, if I try to run it for a second time, I get the error.





Aucun commentaire:

Enregistrer un commentaire