O'Reilly logo

Java RMI by William Grosso

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Security Managers

Within a running JVM, permissions are enforced by an instance of the SecurityManager class. When a program attempts to do something that requires permission, the instance of SecurityManager is queried to see whether the operation succeeds. Thus, for example, attempting to read data from a file involves asking the security manager if the program is allowed to read data from the file.

To see this in action, let’s look at the constructor from the FileInputStream class:

public FileInputStream(String name) throws FileNotFoundException {
		SecurityManager security = System.getSecurityManager(  );
		if (security != null) {
			security.checkRead(name);
		}
		fd = new FileDescriptor(  );
		open(name);
}

The first thing this constructor does is obtain the installed instance of SecurityManager and ask it whether the file can be read. If the application doesn’t have permission to read the file, an exception is thrown. Similarly, the constructor for Socket begins with the following code snippet, which calls the installed security manager and checks to see if a connection is allowed:

private Socket(InetAddress address, int port, InetAddress localAddr, int localPort, boolean stream) throws IOException { this( ); if (port < 0 || port > 0xFFFF) { throw new IllegalArgumentException("port out range:"+port); } if (localPort < 0 || localPort > 0xFFFF) { throw new IllegalArgumentException("port out range:"+localPort); } SecurityManager security = System.getSecurityManager( ); if (security != null) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required