gpt4 book ai didi

java - RMI 服务器 java.security.AccessControlException : access denied

转载 作者:太空宇宙 更新时间:2023-11-03 13:22:14 25 4
gpt4 key购买 nike

我正在构建一个 RMI 应用程序并且我有以下服务器类。但是,当我在 eclipse 中运行它时,出现以下异常。出了什么问题,我该如何解决?我花了最后 2 天时间在网上阅读类似的问题,但找不到解决方案。我怀疑它与策略文件有关,但我不知道如何使用它。另外,如果问题出在我运行它的方式上,请给我在 eclipse 中执行此操作的指导。

import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;


public class MyServer extends UnicastRemoteObject implements Interface {

private static final int PORT = 2019;

public MyServer() throws Exception {
super(PORT, new SslRMIClientSocketFactory(), new SslRMIServerSocketFactory());
}

public static void main(String args[]) {


if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}

try {

Registry registry = LocateRegistry.createRegistry(PORT, new SslRMIClientSocketFactory(), new SslRMIServerSocketFactory());

MyServer obj = new MyServer();


registry.bind("HelloServer", obj);

} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}

@Override
public void sayHello() throws RemoteException {
System.out.println("Hello");
}
}

--

access denied ("java.net.SocketPermission" "localhost:2019" "listen,resolve")
java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:2019" "listen,resolve")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkListen(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at javax.rmi.ssl.SslRMIServerSocketFactory$1.<init>(Unknown Source)
at javax.rmi.ssl.SslRMIServerSocketFactory.createServerSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.listen(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.exportObject(Unknown Source)
at sun.rmi.transport.tcp.TCPEndpoint.exportObject(Unknown Source)
at sun.rmi.transport.LiveRef.exportObject(Unknown Source)
at sun.rmi.server.UnicastServerRef.exportObject(Unknown Source)
at sun.rmi.registry.RegistryImpl.setup(Unknown Source)
at sun.rmi.registry.RegistryImpl.<init>(Unknown Source)
at sun.rmi.registry.RegistryImpl.<init>(Unknown Source)
at java.rmi.registry.LocateRegistry.createRegistry(Unknown Source)
at MyServer.main(MyServer.java:27)

最佳答案

非常简单。

access denied ("java.net.SocketPermission" "localhost:2019" "listen,resolve")

意味着策略文件必须授予权限

java.net.SocketPermission "localhost:2019", "listen,resolve"

或任何正确的语法。使用 policytool 使其正确。您可能希望通配端口号。并且您必须通过 java.security.policy 系统属性指定策略文件的位置。当你得到其他访问控制异常时,就像你在测试中一样,你必须同样添加相应的权限,直到关闭。

但我更愿意问您为什么要使用安全管理器。除非您要使用代码库功能,否则没有必要,这很少见,并且在服务器中也没有必要,除非您要使用代码库功能从客户端上传类,这是很多更稀有。

关于java - RMI 服务器 java.security.AccessControlException : access denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47357754/

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