public final class AccessController extends Object
The AccessController class is used for access control operations and decisions.
More specifically, the AccessController class is used for three purposes:
The checkPermission
method
determines whether the access request indicated by a specified
permission should be granted or denied. A sample call appears
below. In this example, checkPermission
will determine
whether or not to grant "read" access to the file named "testFile" in
the "/temp" directory.
FilePermission perm = new FilePermission("/temp/testFile", "read"); AccessController.checkPermission(perm);
If a requested access is allowed,
checkPermission
returns quietly. If denied, an
AccessControlException is
thrown. AccessControlException can also be thrown if the requested
permission is of an incorrect type or contains an invalid value.
Such information is given whenever possible.
Suppose the current thread traversed m callers, in the order of caller 1
to caller 2 to caller m. Then caller m invoked the
checkPermission
method.
The checkPermission
method determines whether access
is granted or denied based on the following algorithm:
i = m; while (i > 0) { if (caller i's domain does not have the permission) throw AccessControlException else if (caller i is marked as privileged) { if (a context was specified in the call to doPrivileged) context.checkPermission(permission) return; } i = i - 1; }; // Next, check the context inherited when // the thread was created. Whenever a new thread is created, the // AccessControlContext at that time is // stored and associated with the new thread, as the "inherited" // context. inheritedContext.checkPermission(permission);
A caller can be marked as being "privileged"
(see doPrivileged
and below).
When making access control decisions, the checkPermission
method stops checking if it reaches a caller that
was marked as "privileged" via a doPrivileged
call without a context argument (see below for information about a
context argument). If that caller's domain has the
specified permission, no further checking is done and
checkPermission
returns quietly, indicating that the requested access is allowed.
If that domain does not have the specified permission, an exception
is thrown, as usual.
The normal use of the "privileged" feature is as follows. If you don't need to return a value from within the "privileged" block, do the following:
somemethod() { ...normal code here... AccessController.doPrivileged(new PrivilegedAction() { public Object run() { // privileged code goes here, for example: System.loadLibrary("awt"); return null; // nothing to return } }); ...normal code here... }
PrivilegedAction is an interface with a single method, named
run
, that returns an Object.
The above example shows creation of an implementation
of that interface; a concrete implementation of the
run
method is supplied.
When the call to doPrivileged
is made, an
instance of the PrivilegedAction implementation is passed
to it. The doPrivileged
method calls the
run
method from the PrivilegedAction
implementation after enabling privileges, and returns the
run
method's return value as the
doPrivileged
return value (which is
ignored in this example).
If you need to return a value, you can do something like the following:
somemethod() { ...normal code here... String user = (String) AccessController.doPrivileged( new PrivilegedAction() { public Object run() { return System.getProperty("user.name"); } } ); ...normal code here... }
If the action performed in your run
method could
throw a "checked" exception (those listed in the throws
clause
of a method), then you need to use the
PrivilegedExceptionAction
interface instead of the
PrivilegedAction
interface:
somemethod() throws FileNotFoundException {
...normal code here...
try {
FileInputStream fis = (FileInputStream) AccessController.doPrivileged(
new PrivilegedExceptionAction() {
public Object run() throws FileNotFoundException {
return new FileInputStream("someFile");
}
}
);
} catch (PrivilegedActionException e) {
// e.getException() should be an instance of FileNotFoundException,
// as only "checked" exceptions will be "wrapped" in a
// PrivilegedActionException
.
throw (FileNotFoundException) e.getException();
}
...normal code here...
}
Be *very* careful in your use of the "privileged" construct, and always remember to make the privileged code section as small as possible.
Note that checkPermission
always performs security checks
within the context of the currently executing thread.
Sometimes a security check that should be made within a given context
will actually need to be done from within a
different context (for example, from within a worker thread).
The getContext
method and
AccessControlContext class are provided
for this situation. The getContext
method takes a "snapshot"
of the current calling context, and places
it in an AccessControlContext object, which it returns. A sample call is
the following:
AccessControlContext acc = AccessController.getContext()
AccessControlContext itself has a checkPermission
method
that makes access decisions based on the context it encapsulates,
rather than that of the current execution thread.
Code within a different context can thus call that method on the
previously-saved AccessControlContext object. A sample call is the
following:
acc.checkPermission(permission)
There are also times where you don't know a priori which permissions to check the context against. In these cases you can use the doPrivileged method that takes a context:
somemethod() { AccessController.doPrivileged(new PrivilegedAction() { public Object run() { // Code goes here. Any permission checks within this // run method will require that the intersection of the // callers protection domain and the snapshot's // context have the desired permission. } }, acc); ...normal code here... }
AccessControlContext
Modifier and Type | Method and Description |
---|---|
static void |
checkPermission(Permission perm)
Determines whether the access request indicated by the
specified permission should be allowed or denied, based on
the security policy currently in effect.
|
static void |
checkPermission(String name)
Determines whether the access request indicated by the
specified permission should be allowed or denied, based on
the security policy currently in effect.
|
static void |
checkPermission(String name,
String resource)
Check for permission and throw an exception if not allowed.
|
static void |
checkPermission(String name,
String resource,
String extraValue)
Checks for permission and throw an exception if not allowed.
|
static AccessControlContext |
getAccessControlContext()
Returns the access control context for the running application.
|
static void |
setAccessControlContext(AccessControlContext accessControlContext)
Called to set the access control context for the application running
in this isolate.
|
public static void checkPermission(Permission perm) throws AccessControlException
perm
- the requested permission.AccessControlException
- if the specified permission
is not permitted, based on the current security policy.public static void setAccessControlContext(AccessControlContext accessControlContext)
accessControlContext
- access control context for the application
running in the isolateSecurityException
- if the context is already setpublic static AccessControlContext getAccessControlContext()
SecurityException
- if context has not been set yetpublic static void checkPermission(String name) throws SecurityException
If the permission check failed because an InterruptedException was thrown, this method will throw a InterruptedSecurityException.
name
- name of the requested permissionSecurityException
- if the specified permission
is not permitted, based on the current security policypublic static void checkPermission(String name, String resource) throws SecurityException
If the permission check failed because an InterruptedException was thrown, this method will throw a InterruptedSecurityException.
name
- name of the requested permissionresource
- string to insert into the question, can be null if
no %2 in the questionname
- name of the requested permissionSecurityException
- if the specified permission
is not permitted, based on the current security policypublic static void checkPermission(String name, String resource, String extraValue) throws SecurityException
If the permission check failed because an InterruptedException was thrown, this method will throw a InterruptedSecurityException.
name
- ID of the permission to check for,
the ID must be from
com.sun.midp.security.Permissions
resource
- string to insert into the question, can be null if
no %2 in the questionextraValue
- string to insert into the question,
can be null if no %3 in the questionname
- name of the requested permissionSecurityException
- if the specified permission
is not permitted, based on the current security policy