访问被拒绝(当我在 NetBeans 中运行我的客户端项目时发生“java.net.SocketPermission”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20982947/
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-08-13 05:43:24  来源:igfitidea点击:

Access denied ("java.net.SocketPermission" Error Occurred when i run my CLient Project in NetBeans

javarmisecuritymanager

提问by user3125051

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

java.security.AccessControlException: 访问被拒绝 ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")

My server side is working perfectly, no errors on the server.. while when I run my client code, I get this access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve") error.

我的服务器端工作正常,服务器上没有错误......而当我运行我的客户端代码时,我收到此访问被拒绝(“java.net.SocketPermission”“127.0.0.1:1099”“connect,resolve”)错误。

Please, any expert help me :(

请任何专家帮助我:(

This is my client code

这是我的客户端代码

/**
 *
 * @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);
        }
}
}

回答by user207421

You've defined a SecurityManagerbut you haven't granted yourself enough permissions to execute your code. You need to write yourself a policy fileand specify it to the JVM when starting via -Djava.security.policy=....

您已经定义了 aSecurityManager但您还没有授予自己足够的权限来执行您的代码。您需要自己编写一个策略文件,并在通过-Djava.security.policy=....

Or, just remove the security manager. You don't need it unless you're using the RMI Codebase feature.

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

回答by Diversity

Add a security policy to your client application. You can download a sample policy from here: http://www.comp.lancs.ac.uk/~weerasin/csc253/tutorials/week8code/client.policy

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

After that start your client with the following vm argument

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

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

Be carefull in production environments since the given policy grants permission to any operation performed by your client.

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