为什么rmi代码抛出accesscontrolexception?

zf2sa74q  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(348)

我有一个java类,其中rmi代码编写为:

public class Test{
  public static void main(String[] args){
    System.setProperty("java.security.policy","file:/C:/myFloder/server.policy");

    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
        log.info("Security manager installed.");
    } else {
        log.info("Security manager already exists.");
    }

    try {
        LocateRegistry.createRegistry(1099);
        log.info("java RMI registry created.");
    } catch (RemoteException e) {
        log.info("java RMI registry already exists.");
    }
    Naming.unbind("//" + serverIp + ":1099/ServletPath");
    log.info("RMI stareted successfully...!!");
  }
}

以下是服务器策略:

grant {
    permission java.security.AllPermission;
};

但当我试图执行它时,它总是给我同样的异常。我也尝试过stackoverflow的各种解决方案,但都是徒劳的。

java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
    at java.security.AccessController.checkPermission(AccessController.java:884)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.SecurityManager.checkConnect(SecurityManager.java:1051)
    at java.net.Socket.connect(Socket.java:584)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:342)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
    at java.rmi.Naming.rebind(Naming.java:177)
    at com.ist.ivision.rmi.server.SendOnQueueImpl.initMethod(SendOnQueueImpl.java:99)
    at com.ist.ivision.rmi.server.SendOnQueueImpl.main(SendOnQueueImpl.java:143)
    at com.ist.ivision.servlet.IVisionStartup$2.run(IVisionStartup.java:124)
    at java.lang.Thread.run(Thread.java:745)

java.security.AccessControlException: access denied ("java.io.FilePermission" "filepath/A.java" "write")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
    at java.security.AccessController.checkPermission(AccessController.java:884)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.SecurityManager.checkWrite(SecurityManager.java:979)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:200)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
    at java.io.FileWriter.<init>(FileWriter.java:63)

有人能告诉我我做错了什么吗??
谢谢

wj8zmpe1

wj8zmpe11#

查看现有的java.policy文件,格式看起来也不同。
//默认情况下,标准扩展获得所有权限

grant codeBase "file:${{java.ext.dirs}}/*" {
        permission java.security.AllPermission;
};

我还要再次检查自定义.policy文件的路径是否正确。
一个有用的stackoverflow是:javarmi accesscontrolexception:access denied
=======已编辑
否则,请尝试从代码中删除system.setproperty(),并在运行时将自定义策略文件作为ide的运行时参数指向:
删除system.setproperty()
修改eclipse/intellij并添加运行时参数
-djava.security.manager-djava.security.policy=c:/myfloder/server.policy
=============编辑2
尝试这样做,而不是使用文件:

Policy.setPolicy(new Policy() {
        @Override
        public boolean implies(ProtectionDomain domain, Permission permission) {
            return true; // all permissions
        }
    });

    System.setSecurityManager(new SecurityManager());

相关问题