JavaFX - Unable to access file system with signed

2019-07-25 09:01发布

问题:

I am developing a JavaFX application that needs to access files on the users system. I know that my application must be signed before it could have such access, so i signed my app. But the app still throws java.security.AccessControlException

-The Application

public class TestApp extends Application
{
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) 
    {
         launch(args);
    }

    @Override
    public void start(Stage primaryStage) 
    {
         primaryStage.setTitle("Hello World!"); 
         StackPane root = new StackPane();
         primaryStage.setScene(new Scene(root, 300, 250));
         primaryStage.show();
    }

    public void callJSFunc(JSObject func) 
    {
         File fleExample = new File("F:/myfile.xml");
         func.call("call", fleExample.exists());
    }
}

Then i went on to sign the app

keytool -genkey -keystore myKeyStore -alias me
keytool -selfcert -keystore myKeyStore -alias me  
jarsigner -keystore myKeyStore TestApp.jar me

So i called the apps callJSFunc from javascript

function deployIt() 
{
      dtjava.embed(
      {
          id: "my2",
          url: "TestApp.jnlp",
          width: 300,
          height: 200,
          placeholder: "here"
       },
       { 
          javafx: "2.1+", 
           jvm: "1.6.0+" 
       },
       {
           onJavascriptReady: callApp
       });
}

function callApp(id) 
{
   var app = document.getElementById(id);
   app.callJSFunc(function(e){ alert(e); });
}

dtjava.addOnloadCallback(deployIt);

But my app outputed this on the browser

Uncaught Error: java.security.AccessControlException: access denied ("java.io.FilePermission" "F:/myfile.xml" "read") 

Also, i tested the app on my localhost. I don't understand why its throwing this exception after siging the app. Please what am i doing wrong? Thanks

回答1:

Don't use the keytool and jarsigner to sign the app.

Instead package, sign and ready your app for deployment using the javafx deployment tools such as javafxpackager or the JavaFX ant tasks.

Make sure that your jnlp file requests elevated permissions. If you are using the JavaFX ant tasks, you can request JavaFX to generate an appropriate jnlp with elevated permissions using a fx:permissions clause.



回答2:

Try this (with a doPrivileged block) :

public class TestApp extends Application
{
    // ...

    public void callJSFunc(JSObject func) 
    {
         File fleExample = null;
         AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                fleExample = new File("F:/myfile.xml");
                func.call("call", fleExample.exists());
            }
         });

    }
}