16
Дек
2017

java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:2222" "listen,resolve")

System.setSecurityManager(new SecurityManager());
        final int port = 2222;
        try {
            Registry registry = LocateRegistry.getRegistry(PORT);
            StubRealize stub = new StubRealize();
            Stub serverStub = (Stub) UnicastRemoteObject.exportObject(stub, PORT);
            registry.rebind("Stub", serverStub);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        System.out.println("Server started...");

Пытаюсь запустить rmi сервер с политикой:

grant {
    permission java.net.SocketPermission "localhost:2222", "listen, resolve";
}

Путь указан верно. Выдает ошибку:

java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:2222" "listen,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.checkListen(SecurityManager.java:1131) at java.net.ServerSocket.bind(ServerSocket.java:374) at java.net.ServerSocket.(ServerSocket.java:237) at java.net.ServerSocket.(ServerSocket.java:128) at sun.rmi.transport.proxy.RMIDirectSocketFactory.createServerSocket(RMIDirectSocketFactory.java:45) at sun.rmi.transport.proxy.RMIMasterSocketFactory.createServerSocket(RMIMasterSocketFactory.java:345) at sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(TCPEndpoint.java:666) at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:330) at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:249) at sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:411) at sun.rmi.transport.LiveRef.exportObject(LiveRef.java:147) at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:236) at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:383) at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:320)

VM параметры:

-Djava.security.policy=src\main\resources\server.policy;

Спасибо.

Решение:

System.setProperty("java.security.policy", "src\\main\\resources\\server.policy");

Источник: https://ru.stackoverflow.com/questions/759272/java-security-accesscontrolexception-access-denied-java-net-socketpermission

Тебе может это понравится...

Добавить комментарий