gpt4 book ai didi

java - rmi 服务器抛出 AccessControlException

转载 作者:行者123 更新时间:2023-12-01 10:15:55 27 4
gpt4 key购买 nike

我正在尝试使用 here 中的教程建立客户端-服务器关系。我的服务器类位于我的路径中的引擎包中:/home/rozerin/IdeaProjects/computetest/src/main/java/engine 和客户端,接口(interface)包也位于 ~/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 exception: java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve") at java.security.AccessControlContext.checkPermission(AccessControlContext.java:474) at java.security.AccessController.checkPermission(AccessController.java:685) at java.lang.SecurityManager.checkPermission(SecurityManager.java:549) at java.lang.SecurityManager.checkConnect(SecurityManager.java:1051) at java.net.Socket.connect(Socket.java:574) at java.net.Socket.connect(Socket.java:528) at java.net.Socket.(Socket.java:425) at 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) at engine.ComputeEngine.main(ComputeEngine.java:33)

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

界面在我分享的网站上,我不想让这个地方太拥挤。任何帮助将不胜感激。谢谢!

最佳答案

对于服务器端和客户端都应该使用这个:

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

安全策略文件应包含

 grant {
permission java.security.AllPermission;
};

我想这可能对你有帮助。

关于java - rmi 服务器抛出 AccessControlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35891684/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com