RMI访问始终拒绝错误

时间:2012-05-12 15:43:54

标签: java rmi

我在创建RMIserver类时遇到问题,因为我一直收到此错误:访问被拒绝(java.net.SocketPermission 127.0.0.1:1099 connect,resolve)

现在我在google上阅读了关于修改新政策文件的内容,但我真的不明白该怎么做,可以请别人帮帮我吗?

这是我的服务器代码:

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.*; 
import java.security.Permission;
import java.security.Security;

public class RmiServer extends UnicastRemoteObject 
    implements RmiServerIntf 
    {
    public static final String MESSAGE = "Hello world";

    public RmiServer() throws RemoteException
    {
    }

    public String getMessage()
    {
        return MESSAGE;
    }


    public static void main(String args[])
    {
        System.out.println("RMI server started");

        // Create and install a security manager
        if (System.getSecurityManager() == null)
        {
            System.setSecurityManager(new RMISecurityManager());
            System.out.println("Security manager installed.");
        }
        else
        {
            System.out.println("Security manager already exists.");
        }

        try
        { //special exception handler for registry creation
            LocateRegistry.createRegistry(1099); 
            System.out.println("java RMI registry created.");
        } 
        catch (RemoteException e)
        {
            //do nothing, error means registry already exists
            System.out.println("java RMI registry already exists.");
        }

        try 
        {
            //Instantiate RmiServer
            RmiServer obj = new RmiServer();


            // Bind this object instance to the name "RmiServer"
            Naming.rebind("//localhost/RmiServer", obj);

            System.out.println("PeerServer bound in registry");
        } 
        catch (Exception e) 
        {
            System.err.println("RMI server exception:" + e);
            e.printStackTrace();
        }
    }

}    

2 个答案:

答案 0 :(得分:2)

您正在使用SecurityManager(为什么?)但您的安全策略未授予您在异常中指定的权限。除非您打算使用RMI代码库功能,否则根本不需要SecurityManager:是吗?

答案 1 :(得分:0)

真的需要阅读RMI手册,以及如何创建策略文件:

http://docs.oracle.com/javase/tutorial/rmi/running.html