Java Reference
In-Depth Information
Example 6−3: SecureService.java (continued)
out.println("Opened file for writing: " + f);
}
catch (Exception e) { out.println("Failed: " + e); }
// Check whether we've been given permision to read files from
// the tmpdir directory
out.println();
out.println("Attempting to read from " + tmpdir + "...");
try {
FileReader in = new FileReader(f);
out.println("Opened file for reading: " + f);
}
catch (Exception e) { out.println("Failed: " + e); }
}
catch (Exception e) { out.println("Failed: " + e); }
// Close the Service sockets
out.close();
i.close();
}
}
To test SafeServer with the SecureService class, you have to decide which direc-
tories you'll use for storing the service classes and for the scratch directory. In the
material that follows, I've used /tmp/services and /tmp/scratch as the directory
names.
First, compile SecureService using the -d option to tell javac where to put the
resulting class file:
% javac -d /tmp/services SecureService.java
It is important that you make sure there isn't a copy of the Secur eService.class file
in the current directory or anywhere else that Java might find it in the local class
path. If the URLClassLoader can find the class locally, it won't bother loading it
through the URL you specify.
Now, to run the SafeServer class, specify the name of the SecureService class,
the URL to load it from, the port to listen for connections on, and four different
system properties with -D options:
% java -Djava.security.manager -Djava.security.policy=SafeServer.policy \
-Dservice.dir=/tmp/services -Dservice.tmp=/tmp/scratch \
com.davidflanagan.examples.security.SafeServer file:/tmp/services/ \
SecureService 4000
This is a complicated command line, but it produces the desired results. When you
connect to port 4000, you get the following output from the service:
% java com.davidflanagan.examples.net.GenericClient localhost 4000
Connected to localhost/127.0.0.1:4000
Trying to create and install a security manager...
Failed: java.security.AccessControlException: access denied
(java.lang.RuntimePermission createSecurityManager)
Trying to exit...
Failed: java.security.AccessControlException: access denied
Search WWH ::




Custom Search