By Scott Oaks
Price: $49.99 USD
£31.95 GBP
Cover | Table of Contents | Colophon
java.security.debug
, and it may be set to the following
values:
checkPermission(
)
method of the
access controller.
This allows you to see which permissions your code is requesting,
which calls are succeeding, and which ones are failing.
java.security.AllPermission
, which allows code to do anything)
require no name. Otherwise, the name is based on the type of the
permission; for example, the name of a file permission is a file or
directory name. The names of permissions are often specified as
wildcards, such as all files in a directory or all hosts on the local
network.
permission java.security.AllPermission; permission java.lang.RuntimePermission "stopThread"; permission java.io.FilePermission "/tmp/foo", "read";
net.jini.discovery.DiscoveryPermission
class. Classes that want to participate in a Jini community must be
granted this permission in order to execute the code that
participates in Jini's discovery protocol. In general, then,
you may need to become familiar with arbitrary permission types in
order to use certain APIs.
jarsigner (or
equivalent) tool. You can grant permissions to code that is signed by
a particular entity.
keytool utility (see Chapter 10). By default, the keystore is held in a file
called .keystore in the user's home
directory. When you install a public key certificate into the
keystore, you give that certificate an alias that is used to look up
the certificate in the future. For example, my public key certificate
lists my full name and other identifying information, but you may
enter it into your keystore with an alias of
sdo. This alias is the information that you
list in a policy file.
policytool
, or you can edit them by hand. Hand
editing is discouraged (in 1.3, policytool
writes a warning at the top of the file not to edit it by hand), but
real programmers still edit them by hand. Policy files are also used
with JAAS, in which case their syntax changes slightly and you must
edit them by hand (at least until 1.4, when JAAS becomes integrated
with the SDK). So first, we'll see how they look, and then
we'll look at how they are created with
policytool.
keystore "${user.home}${/}.keystore";
// Grant these permissions to code loaded from O'Reilly, regardless of
// whether the code is signed.
grant codeBase "http://www.oreilly.com/" {
permission java.io.FilePermission "/tmp", "read";
permission java.lang.RuntimePermission "queuePrintJob";
};
// Grant these permissions to code loaded from Sun but only if it is
// signed by sdo.
grant signedBy "sdo", codeBase "http://www.sun.com/" {
permission java.security.AllPermission;
};
// Grant these permissions to code signed by jra, no matter where it
// was loaded from
grant signedBy "jra" {
permission java.net.SocketPermission "*:1024-",
"accept, connect, listen, resolve";
};
// Grant these permissions to any code, no matter where it came
// from or whether it is signed
grant {
permission java.util.PropertyPermission
"java.version", "read";
};java.security.manager property like this:
piccolo% java -Djava.security.manager <other args>
java.security.policy property:
piccolo% java -Djava.security.policy=<URL>
http:
or file: protocol) or simply list a filename. If
you want the given policy file to be the only policy file used
(bypassing the ones in $JREHOME/lib/security and
the user's home directory), specify two equals signs:
piccolo% java -Djava.security.policy==<URL>
PayrollApp in the default sandbox with
additional permissions loaded from the file
java.policy in the local directory:
piccolo% java -Djava.security.manager \ -Djava.security.policy=java.policy PayrollApp
-J
argument:
piccolo% appletviewer -J-Djava.security.policy=<URL>policy.expandProperties=true
policy.allowSystemProperty=true
policy.url.1=file:${java.home}/lib/security/java.policy
policy.url.2=file:${user.home}/.java.policy
java.home expands to what we've been
calling JREHOME). If you would prefer a different set of policy files
to be used by default, you can edit the
java.security file and change the URLs that are
used. You may specify any number of URLs, but they must be numbered
consecutively beginning with 1.
allowSystemProperty
property to false. A site that has that value
set to false, removes the user's
.java.policy entry from this file, and makes the
java.security file uneditable by end users has
established a sandbox that cannot be modified by end users.
expandProperties property to
false.
java package.
javakey), it is
possible to sign Java applets; these applets can then be given
permission to perform any operation. However, the 1.1-based signing
infrastructure has been deprecated, and an applet signed with
javakey will not be given any special
permissions in Java 2.
private
protected
public
Vector v = new Vector( ); String s = (String) v;
X to type Y where
Y is a subclass of X cannot
be known at compile time, so the compiler must let such a construct
pass.
acctNo in our CreditCard
class but forgot to recompile our Test class.
classpath
,
the local class will not be verified and may violate the rules
we've discussed here. In Java 2, this is much harder since the
class must be added to the jar file containing the core API classes.
java with the
-verify
option, which will verify all classes.
Similarly, if you are using a browser written in Java -- including
the -Djava.security.manager option installs a
security manager. The security manager is installed programatically
by the
appletviewer and the Java Plug-in.
System
class that are
used to work with the security manager itself:
null if no security manager
is in place). Once obtained, this object can be used to test against
various security policies.
createSecurityManager in
order to instantiate the security manager object and
setSecurityManager in order to install it.
getSecurityManager( ) method to retrieve the
security manager and invoke an operation on it. Setting the security
manager is a predictably simple operation:
public static void main(String args[]) {
System.setSecurityManager(new SecurityManagerImpl( ));
... do the work of the application ...
}
}
SecurityManager
class provides a complete implementation
that uses the access controller to implement the permission-based
sandbox we discussed in Chapter 2. When you
specify the -Djava.security.manager option to a
Java application, the virtual machine executes the
setSecurityManager( )setSecurityManager(
)
method
can
only be called once, and once installed,
the security manager cannot be removed. Attempting to call this
method after a security manger has already been installed will result
in a