Java RMI 客户端访问被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13101787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:29:34  来源:igfitidea点击:

Java RMI Client access denied

javarmi

提问by Graeme T

I have a small Java RMI Server and Client program I'm writing. I have spent some time trying to figure out the error messages without success.

我正在编写一个小型 Java RMI 服务器和客户端程序。我花了一些时间试图找出错误消息,但没有成功。

The Client generates the following error:

客户端生成以下错误:

Trying to connect to: 127.0.0.1:3232 ERROR!!!: StockClient: main: Could not connect to the server: java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is: java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is: java.io.EOFException java.io.EOFException at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:209) at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:359) at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) at StockClient.StockClient.main(StockClient.java:44) Caused by: java.io.EOFException at java.io.DataInputStream.readByte(DataInputStream.java:250) at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:195) ... 3 more

试图连接到:127.0.0.1:3232 错误!!!:StockClient:main:无法连接到服务器:java.rmi.UnmarshalException:解组返回标头时出错;嵌套异常是: java.rmi.UnmarshalException: Error unmarshaling return header; 嵌套异常是: java.io.EOFException java.io.EOFException at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:209) at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:359) at sun .rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) at StockClient.StockClient.main(StockClient.java:44) 由:java.io.EOFException at java.io.DataInputStream.readByte(DataInputStream.java:250) at sun .rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:195) ... 3 更多

With the server the following error only when the client attempts to connect.

仅当客户端尝试连接时,服务器才会出现以下错误。

this address=localhost/127.0.0.1,port=3232 Exception in thread "RMI TCP Connection(idle)" java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:62586 accept,resolve) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374) at java.security.AccessController.checkPermission(AccessController.java:549) at java.lang.SecurityManager.checkPermission(SecurityManager.java:532) at java.lang.SecurityManager.checkAccept(SecurityManager.java:1157) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.checkAcceptPermission(TCPTransport.java:636) at sun.rmi.transport.tcp.TCPTransport.checkAcceptPermission(TCPTransport.java:275) at sun.rmi.transport.Transport$1.run(Transport.java:158) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Transport.java:155) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:680)

此地址=localhost/127.0.0.1,port=3232 线程“RMI TCP 连接(空闲)”中的异常 java.security.AccessControlException:在 java.security 处拒绝访问(java.net.SocketPermission 127.0.0.1:62586 接受,解决) .AccessControlContext.checkPermission(AccessControlContext.java:374) at java.security.AccessController.checkPermission(AccessController.java:549) at java.lang.SecurityManager.checkPermission(SecurityManager.java:532) at java.lang.SecurityManager.checkAccept( SecurityManager.java:1157) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.checkAcceptPermission(TCPTransport.java:636) at sun.rmi.transport.tcp.TCPTransport.checkAcceptPermission(TCPTransport.java:275) at sun.rmi .transport.Transport$1.run(Transport.java:158) 在 java.security.AccessController。doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Transport.java:155) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535) at sun.rmi.transport.tcp。 TCPTransport$ConnectionHandler.run0(TCPTransport.java:790) 在 sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649) 在 java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:第 886 章run0(TCPTransport.java:790) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java .util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 在 java.lang.Thread.run(Thread.java:680)run0(TCPTransport.java:790) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java .util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 在 java.lang.Thread.run(Thread.java:680)

Because of the server error I'm fairly sure its a security or registry error, security policy for the server is:

由于服务器错误,我很确定这是一个安全或注册表错误,服务器的安全策略是:

grant { permission java.security.AllPermission; };

授予 { 权限 java.security.AllPermission; };

and being run with the following argument

并使用以下参数运行

-Djava.security.policy=client.policy

-Djava.security.policy=client.policy

I've tried everything I can find but seem to keep going around in circles.

我已经尝试了我能找到的一切,但似乎一直在兜圈子。

relevant methods:

相关方法:

Server:

服务器:

 public static void main(String[] args)
    {//main(...) starts
        // set up the data structures and data

        //add users
        //Users hard coded as this is an example proof on concept program
        //Names and passwords not hashed for simplicity
        User alpha = new User("Alpha", "AlphaPass");
        User omega = new User("Omega", "OmegaPass");
        users.add(alpha);
        users.add(omega);

        //startup the RMI server
        try
        {
            System.setSecurityManager(new RMISecurityManager());
            StockServer server = new StockServer();
            StockServerInterface inter = (StockServerInterface)
UnicastRemoteObject.exportObject (server,0);

            // create the registry and bind the name and object.
            registry = LocateRegistry.createRegistry(thisPort);
            registry.rebind("StockServer", inter);
        }
        catch (Exception e)
        {
            System.out.println("Unable to create StockServer: " + e);
            e.printStackTrace();
            System.exit(1);
        }
    }//main(...) ends


    /**
     * Constructor for StockServer
     *
     * @throws RemoteException
     */
    public StockServer() throws RemoteException
    {

        //try to get the host machine's IP address
        try
        {
            // get the address of this host.
            thisAddress = (InetAddress.getLocalHost()).toString();
        } catch (Exception e)
        {
            throw new RemoteException("can't get inet address. " + e);
        }

        //Set the port
        thisPort = 3232;

        //Print out the server address and port
        System.out.println("this address=" + thisAddress + ",port=" + thisPort);
    }

Client:

客户:

private static StockServerInterface stockServer;
static public void main(String args[])
{
    Registry registry;
    //get the server address from the args
    String serverAddress = args[0];

    //get the server port from the args
    String serverPort = args[1];

    //Let the user know we are about to try to connect
    System.out.println("Trying to connect to: " + serverAddress + ":" + serverPort);
    try
    {
        // get the registry
        registry = LocateRegistry.getRegistry(
                serverAddress,
                (new Integer(serverPort)).intValue());
        // look up the remote object
        stockServer = (StockServerInterface) (registry.lookup("StockServer"));


        //Authenticate the user
        authenticate();

        //setup the hashset
        HashSet<Stock> hashStockSet = null;

        //setup the hashset of desired stocks
        try
        {
            hashStockSet = getHashSet();
        } catch (IOException e)
        {
            e.printStackTrace();
            System.exit(1);
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
            System.exit(1);
        }
        //bit of a heavy handed infinte loop so we continue to get the loop
        while(true)
        {
            //Run the ticker
            ticker(hashStockSet);
        }

        // call the remote method


    }
    catch (RemoteException e)
    {
        System.out.println("ERROR!!!: StockClient: main: Could not connect to the server: "+e);
        e.printStackTrace();
    }
    catch (NotBoundException e)
    {
        System.out.println("ERROR!!!: StockClient: main: Could not connect to the server: "+e);
        e.printStackTrace();
    }

回答by user207421

You don't need a SecurityManagerin an RMI server unless the client is relying on the codebase feature to supply the server with classes. Either remove it, or debug the .policy file. Clearly the one you've written isn't being loaded.

SecurityManager除非客户端依赖代码库功能为服务器提供类,否则在 RMI 服务器中不需要 a 。删除它,或调试 .policy 文件。很明显,你写的那个没有被加载。

Run your server with -Djava.security.debug=access,failureand you will see where all the security domains are getting their configurations from, and the domain that is failing at the point where the exception is thrown.

运行您的服务器,-Djava.security.debug=access,failure您将看到所有安全域从何处获取其配置,以及在抛出异常时出现故障的域。