我正在尝试建立一个 rmi 连接。我遇到过许多安全问题,但一直无法找到解决所有这些问题的方法。我执行我的 jar 文件:
java -Djava.security.policy=java.security.AllPermission -jar "myjarfile"
我用来创建这个的代码是:
public class server
{
public static void main(String args[])throws Exception
{
if (System.getSecurityManager() == null)
System.setSecurityManager ( new RMISecurityManager() {
public void checkConnect (String host, int port) {}
public void checkConnect (String host, int port, Object context) {}
});
try
{
sampleserverimpl server = new sampleserverimpl();
System.out.println("SERVER IS WAITING");
LocateRegistry.createRegistry(2020);
//Runtime.getRuntime().exec("rmiregistry 2020");
Naming.rebind("//localhost:2020/SERVER", server);
}
catch(Exception e)
{
System.out.println(e);
}
}
};
我收到的错误跟踪是:
Exception in thread "RMI TCP Connection(idle)" java.security.AccessControlExcept
ion: access denied (java.net.SocketPermission 127.0.0.1:31199 accept,resolve)jav
a.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:
java.io.EOFException
我尝试了不同的方法来解决这个问题,有人能看到这里的问题吗?
谢谢
最佳答案
-Djava.security.policy
接受指向策略文件的 URL,该文件又包含权限。所以你应该有: -Djava.security.policy=/some/path/my.policy
作为 JVM 参数,其中 my.policy
文件包含:
grant {
permission java.security.AllPermission;
};
此外,为了避免代码中出现 NULL 检查和手动创建 SecurityManager
,您可以通过传递 JVM 开关请求为您的应用程序自动安装 SecurityManager:-Djava.security.manager
.
您最终的 JVM 调用应该如下所示:
java -Djava.security.manager -Djava.security.policy=/some/path/my.policy
关于java - RMI 连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4214238/