简体   繁体   中英

Java.security.Access.ControlException: access denied(“java.io.FilePermission” “[object file]” “read”)

I have the following problem:

I have a Java program that receives via Applet a binary file.

I received that file with getParameter(file) and read that file with java.io.FileInputStream(file) .

I put this file on web server and call the java program via javascript

First, when I was running the program, was occuring the message error:

Java.security.Access.ControlException: access denied("java.io.FilePermission" "[object file]" "read")

I created a key via keytool and signed the jar file with jarsigner .

But, even executing the command jarsigner , when I run the Java program again, the error message continues occuring:

Java.security.Access.ControlException: access denied("java.io.FilePermission" "[object file]" "read"). 

Therefore, the error persists even after signing.

I really do not know what to do.

Can anyone help me?

Below the java code:

public class InJava extends Applet{
    String parametro;    
    public void sayHello() {

        parametro = getParameter("parametro");

        java.io.FileInputStream fis = null;

        try  {
            fis = new java.io.FileInputStream(parametro);
        }
        catch (Exception e)  {
            String retorno_exc = e.toString();
            return ;
        }

    }

You need to wrap your code in AccessController.doPrivileged , like:

public class InJava extends Applet{

  public void sayHello() {

    final String parametro = getParameter("parametro");

    FileInputStream fis =  AccessController.doPrivileged(new PrivilegedAction<FileInputStream>() {
      public FileInputStream run() {
        try  {
          retrun new FileInputStream(parametro);
        } catch (IOException e)  {
          // handle exception
        }
      }
    });
  }

Make sure that your applet jar(s) are signed, and that you understand all other consequences of running an applet .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM