rmi服务器抛出AccessControlException

时间:2016-03-09 12:31:39

标签: java intellij-idea rmi

我正在尝试使用here的教程构建客户端 - 服务器关系。我的服务器类在我的路径的引擎包中:/ home / rozerin / IdeaProjects / computetest / src / main / java / engine和client,接口包也在〜/ main / java包中。 就像之前关于这个问题的问题中所建议的那样,我在策略文件中以“/”结束了我的目录以获得权限,但是它没有用。现在,我的策略文件位于引擎文件夹中。 我正在使用Intellij Idea进行编译,但我没有使用IDE,因为我正在尝试从ubuntu中的命令行构建服务器。我不知道出了什么问题,我在这一点上非常困难。

我的命令行参数如下;

  

java -cp /home/rozerin/IdeaProjects/computetest/src/main/java:/home/rozerin/Public/public_html/classes/compute.jar -Djava.rmi.server.codebase = http://mycomputer/home/rozerin/IdeaProjects/computetest/src/main/java/compute.jar - Djava.rmi.server.hostname = mycomputer.example.com -Djava.security.policy = server.policy engine / ComputeEngine

我的客户端类如下;

package client;

/**
 * Created by rozerin on 08.03.2016.
 */
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.math.BigDecimal;
import compute.Compute;

public class ComputePi {
 public static void main(String args[]) {
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
    }
    try {
        String name = "Compute";
        Registry registry = LocateRegistry.getRegistry(args[0]);
        Compute comp = (Compute) registry.lookup(name);
        Pi task = new Pi(Integer.parseInt(args[1]));
        BigDecimal pi = comp.executeTask(task);
        System.out.println(pi);
    } catch (Exception e) {
        System.err.println("ComputePi exception:");
        e.printStackTrace();
    }
  }
}

我的服务器类如下;

package engine;

/**
 * Created by rozerin on 08.03.2016.
 */
 import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import compute.Compute;
import compute.Task;

public class ComputeEngine implements Compute {

    public ComputeEngine() {
    super();
    }

    public <T> T executeTask(Task<T> t) {
    return t.execute();
    }

    public static void main(String[] args) {
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
    }
      try {
        String name = "Compute";
        Compute engine = new ComputeEngine();
        Compute stub =
                (Compute) UnicastRemoteObject.exportObject(engine, 0);
        Registry registry = LocateRegistry.getRegistry();
        registry.rebind(name, stub);
        System.out.println("ComputeEngine bound");
    } catch (Exception e) {
        System.err.println("ComputeEngine exception:");
        e.printStackTrace();
    }
 }
}

我的政策文件是

  

grant codeBase“file:/ home / rozerin / IdeaProjects / computetest / src / main / java /”{       permission java.security.AllPermission;   };

堆栈跟踪是;

  

ComputeEngine异常:   java.security.AccessControlException:拒绝访问(“java.net.SocketPermission”“127.0.0.1:1099”“connect,resolve”)       at java.security.AccessControlContext.checkPermission(AccessControlContext.java:474)       在java.security.AccessController.checkPermission(AccessController.java:685)       在java.lang.SecurityManager.checkPermission(SecurityManager.java:549)       在java.lang.SecurityManager.checkConnect(SecurityManager.java:1051)       在java.net.Socket.connect(Socket.java:574)       在java.net.Socket.connect(Socket.java:528)       在java.net.Socket。(Socket.java:425)       在java.net.Socket。(Socket.java:208)       at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)       at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)       at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)       at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)       at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)       at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:341)       at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)       在engine.ComputeEngine.main(ComputeEngine.java:33)

“at engine.ComputeEngine.main(ComputeEngine.java:33)”是它使用服务器类的重新绑定方法的地方。

界面在我分享的网站上,我不想让这个地方太过浓密。任何帮助将非常感激。谢谢!

1 个答案:

答案 0 :(得分:0)

对于服务器端和客户端,两者都应该使用:

System.setProperty("java.security.policy", "path_to_your_Security_policy_file_located_in_file_system");
System.setSecurityManager(new RMISecurityManager());

安全策略文件应包含

 grant {
    permission java.security.AllPermission;
    };

我认为这可能会帮助你。