简体   繁体   中英

Java denying access to property permissions

new to parallel/distributed computing and having issues with a client-server program I'm trying to write. What's supposed to happen is the server receives an integer from the client and sends back the sum all the numbers leading up to it (ex, user enters 5, server calculates 1+2+3+4+5, server sends back 15). I'm still trying to figure it out, so I've hard coded the input on the client side.

This is what I have on the server side:

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.*;
import java.util.*;
public class Server {

public static void main(String[]args) {

    try{
        int port = 16790;
        String host = "localhost";
        CalculateSumServerImpl export = new CalculateSumServerImpl();
        LocateRegistry.createRegistry(port);
        String registryURL = "rmi://" + host + ":" + port + "/sum";
        Naming.rebind(registryURL, export);
        System.out.println("Server ready");
    } catch (Exception e) {
        e.printStackTrace();
    }
}   }


//to calculate the sum
import java.rmi.*;
import java.rmi.server.*;

public class CalculateSumServerImpl extends UnicastRemoteObject implements     CalServerInterface {

public int n; //value entered
public int sum; //sum

protected CalculateSumServerImpl() throws RemoteException {
    super();
}

@Override
public int calculateSum(int n) throws RemoteException {

    n = (n*(n+1))/2; //sum of 1 + 2 + 3 + .. + n

    sum = n;

    return sum;
} }

//interface
import java.rmi.Remote;

public interface CalServerInterface extends Remote {

 public int calculateSum(int n ) throws java.rmi.RemoteException;
}

And on the client side:

import java.rmi.*;
import java.util.PropertyPermission;

public class Client {
public static void main(String[]args) {

    System.setSecurityManager(new java.rmi.RMISecurityManager());
    System.setProperty("java.net.preferIPv4Stack" , "true");

    try {
        int port = 16790;
        String host = "localhost";
        String registryURL = "rmi://" + host + ":" + port + "/sum";

        Project4ServerInterface obj = (Project4ServerInterface)Naming.lookup(registryURL);
        System.out.println("Lookup completed.");

        int output = obj.calculateSum(3);
        System.out.println("Sum is: " + output);

    } catch (Exception e) {
        e.printStackTrace();
    }

    System.setProperty("java.net.preferIPv4Stack","true");

} }

And I've implemented the Interface on the client side as well.

The error that I've been getting on the client side is:

Exception in thread "main" java.security.AccessControlException: access denied ("java.util.PropertyPermission" "java.net.preferIPv4Stack" "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.System.setProperty(System.java:792) at project04client.Client.main(Client.java:10)

with the error pointing to the line with this code:

System.setProperty("java.net.preferIPv4Stack" , "true");

Anyone have any experience trouble shooting this error?

Thanks!

The problem is that you have set a security manager for the entire (client) application that won't let you modify system properties.

The simple fix is to set the system properties you need to set before you set the RMI security manager.

Alternatively, you may be able to get rid of the System.setSecurityManager(...) call entirely. You (probably) only need it if you want the client to be able to download classes from your RMI service.


I tried setting the system property before the security manager and got an AccessControlException , denying socket permissions.

That doesn't make much sense. You would only get an AccessControlException if there was a security manager in place at that point. There shouldn't be ... unless this is applet code or similar launched in a web browser. Also, I don't know why a call to set a property would be denied saying that you don't have socket permissions.

When I took the security manager out completely, I got an UnmarshalException pointing to the interface.

You also need to add the classes / interfaces for the objects that tou will be unmarshalling to the client-side classpath.


Actually, I just noticed that the javadoc for RMISecurityManager says:

" RMISecurityManager implements a policy identical to the policy implemented by SecurityManager . RMI applications should use the SecurityManager class or another appropriate SecurityManager implementation instead of this class. "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM