访问被拒绝("java.net.SocketPermission" 在 NetBeans 中运行 CLient 项目时出错



java.security.AccessControlException: access deny ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")

我的服务器端运行良好,服务器上没有错误......而当我运行我的客户端代码时,我收到此访问被拒绝("java.net.SocketPermission"127.0.0.1:1099"连接,解析")错误。

请任何专家帮助我:(

这是我的客户端代码

/**
 *
 * @author saqibhussain
 */
public class ChatClient extends UnicastRemoteObject implements ChatClientIF, Runnable {
public ChatClient() throws RemoteException {
}
private ChatServerIF chat;
private String name = null;
protected ChatClient(String name, ChatServerIF chat) throws RemoteException {        this.name = name;
    this.chat = chat;
    chat.RegisterChatClient(this);
}
public void retrieveMessage(String msg) throws RemoteException {
    System.out.println(msg);
}
public void run() {
    Scanner scaner = new Scanner(System.in);
    String message;
    while (true) {
        try {
            message = scaner.nextLine();
            chat.broadcastMessage(name + " : " + message);
        } catch (RemoteException ex) {
            Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
public static void main(String[] args) throws NotBoundException, MalformedURLException, RemoteException {
        System.setSecurityManager(new RMISecurityManager());
        try {
        String url = "rmi://localhost/RMIChatServer";
        ChatServerIF remoteObject = (ChatServerIF) Naming.lookup(url);
        System.out.println("Got remote object");
        new Thread(new ChatClient(args[0], remoteObject)).start();
        } catch (Exception e) {
        System.out.println(e);
        }
}
}

向客户端应用程序添加安全策略。您可以从此处下载示例策略:http://www.comp.lancs.ac.uk/~weerasin/csc253/tutorials/week8code/client.policy

之后,使用以下 vm 参数启动客户端

java -Djava.security.policy==client.policy

在生产环境中要小心,因为给定的策略授予客户端执行的任何操作。

您已经定义了一个SecurityManager,但尚未授予自己足够的权限来执行代码。您需要为自己编写一个策略文件,并在通过 -Djava.security.policy=... 启动时将其指定给 JVM。

或者,只需删除安全管理器。除非您使用的是 RMI 代码库功能,否则您不需要它。

最新更新