A Subject
represents a grouping of related information
for a single entity, such as a person.
Such information includes the Subject's identities as well as
its security-related attributes
(passwords and cryptographic keys, for example).
Subjects may potentially have multiple identities.
Each identity is represented as a Principal
within the Subject
. Principals simply bind names to a
Subject
. For example, a Subject
that happens
to be a person, Alice, might have two Principals:
one which binds "Alice Bar", the name on her driver license,
to the Subject
, and another which binds,
"999-99-9999", the number on her student identification card,
to the Subject
. Both Principals refer to the same
Subject
even though each has a different name.
A Subject
may also own security-related attributes,
which are referred to as credentials.
Sensitive credentials that require special protection, such as
private cryptographic keys, are stored within a private credential
Set
. Credentials intended to be shared, such as
public key certificates or Kerberos server tickets are stored
within a public credential Set
. Different permissions
are required to access and modify the different credential Sets.
To retrieve all the Principals associated with a Subject
,
invoke the getPrincipals
method. To retrieve
all the public or private credentials belonging to a Subject
,
invoke the getPublicCredentials
method or
getPrivateCredentials
method, respectively.
To modify the returned Set
of Principals and credentials,
use the methods defined in the Set
class.
For example:
Subject subject; Principal principal; Object credential; // add a Principal and credential to the Subject subject.getPrincipals().add(principal); subject.getPublicCredentials().add(credential);
This Subject
class implements Serializable
.
While the Principals associated with the Subject
are serialized,
the credentials associated with the Subject
are not.
Note that the java.security.Principal
class
does not implement Serializable
. Therefore, all concrete
Principal
implementations associated with Subjects
must implement Serializable
.
The following methods in this class for user-based authorization that are dependent on Security Manager APIs are deprecated for removal:
getSubject(AccessControlContext)
doAs(Subject, PrivilegedAction)
doAs(Subject, PrivilegedExceptionAction)
doAsPrivileged(Subject, PrivilegedAction, AccessControlContext)
doAsPrivileged(Subject, PrivilegedExceptionAction, AccessControlContext)
current()
and callAs(Subject, Callable)
are replacements for these methods, where current
is mostly equivalent to getSubject(AccessController.getContext())
and callAs
is similar to doAs
except that the
input type and exceptions thrown are slightly different.
These methods behave differently depending on whether a security manager is allowed or disallowed:
Subject
object is associated
with an AccessControlContext
through a doAs
or
callAs
call, and the subject can then be retrieved using the
getSubject(AccessControlContext)
or current
method.
doAs
or callAs
call
binds a Subject
object to the period of execution of an action,
and the subject can be retrieved using the current
method inside
the action. This subject can be inherited by child threads if they are
started and terminate within the execution of its parent thread using
structured concurrency.
java.security.Principal
, java.security.DomainCombiner
Modifier and Type | Class and Description |
---|---|
pack-priv static class | |
private class | Subject.
This class implements a |
private static class |
Modifier and Type | Field and Description |
---|---|
private static final ProtectionDomain[] | |
private static final int | |
pack-priv Set | principals
A |
private static final int | |
pack-priv transient Set | |
private static final int | |
pack-priv transient Set | pubCredentials
Sets that provide a view of all of this Subject's Credentials |
private volatile boolean | readOnly
Whether this Subject is read-only |
private static final ScopedValue | |
private static final long |
Access | Constructor and Description |
---|---|
public | Subject()
Create an instance of a |
public | Subject(boolean
true if the readOnly, Set<? extends Principal> Subject is to be read-only,
and false otherwise.the principals, Set<?> Set of Principals
to be associated with this Subject .the pubCredentials, Set<?> Set of public credentials
to be associated with this Subject .the privCredentials)Set of private credentials
to be associated with this Subject .Create an instance of a |
Modifier and Type | Method and Description |
---|---|
public static < the type of value returned by the T> Tcall method
of action | Returns: the value returned by thecall method of action the subject, final Callable<T> Subject that the specified action
will run as. This parameter may be null .the code to be run with action)subject as its current
subject. Must not be null .Executes a |
private static <E> LinkedList | collectionNullClean(Collection<? extends E>
A coll)Collection to be tested for null referencesTests for null-clean collections (both non-null reference and no null elements) |
private static AccessControlContext | createContext(final Subject subject, final AccessControlContext acc)
References Deprecated
AccessControlContext is deprecated or references (maybe indirectly) at least one deprecated element.
|
public static Subject | Returns: the current subject, ornull if a current subject is
not installed or the current subject is set to null .Returns the current subject. |
public static < the type of the value returned by the PrivilegedAction's
T> Trun method. | Returns: the value returned by the PrivilegedAction'srun method.the subject, final PrivilegedAction<T> Subject that the specified
action will run as. This parameter
may be null .the code to be run as the specified
action)Subject .
Deprecated
for removal since 18. This method depends on
AccessControlContext
which, in conjunction with
the Security Manager, is deprecated
and subject to removal in a future release.
Perform work as a particular |
public static < the type of the value returned by the
PrivilegedExceptionAction's T> Trun method. | Returns: the value returned by the PrivilegedExceptionAction'srun method.the subject, final PrivilegedExceptionAction<T> Subject that the specified
action will run as. This parameter
may be null .the code to be run as the specified
action)Subject .
Deprecated
for removal since 18. This method depends on
AccessControlContext
which, in conjunction with
the Security Manager, is deprecated
and subject to removal in a future release.
Perform work as a particular |
public static < the type of the value returned by the PrivilegedAction's
T> Trun method. | Returns: the value returned by the PrivilegedAction'srun method.the subject, final PrivilegedAction<T> Subject that the specified
action will run as. This parameter
may be null .the code to be run as the specified
action, final AccessControlContext Subject .the acc)AccessControlContext to be tied to the
specified subject and action.
Deprecated
for removal since 17. This method is only useful in conjunction with
the Security Manager, which is
deprecated and subject to removal in a future release.
Perform privileged work as a particular |
public static < the type of the value returned by the
PrivilegedExceptionAction's T> Trun method. | Returns: the value returned by the PrivilegedExceptionAction'srun method.the subject, final PrivilegedExceptionAction<T> Subject that the specified
action will run as. This parameter
may be null .the code to be run as the specified
action, final AccessControlContext Subject .the acc)AccessControlContext to be tied to the
specified subject and action.
Deprecated
for removal since 17. This method is only useful in conjunction with
the Security Manager, which is
deprecated and subject to removal in a future release.
Perform privileged work as a particular |
public boolean | Returns: true if the specified Object is equal to thisSubject .Object to be compared for equality with this
o)Subject .Overrides java. Compares the specified Object with this |
private int | |
public Set | Returns: theSet of Principals associated with this
Subject .Return the |
public < the type of the class modeled by T extends Principal> Setc | Returns: aSet of Principals that are instances of the
specified Class .the returned c)Set of Principals will all be
instances of this class.Return a |
public Set | Returns: aSet of private credentials held by this
Subject .Return the |
public < the type of the class modeled by T> Setc | Returns: aSet of private credentials that are instances
of the specified Class .the returned c)Set of private credentials will all be
instances of this class.Return a |
public Set | Returns: aSet of public credentials held by this
Subject .Return the |
public < the type of the class modeled by T> Setc | Returns: aSet of public credentials that are instances
of the specified Class .the returned c)Set of public credentials will all be
instances of this class.Return a |
public static Subject | Returns: theSubject associated with the provided
AccessControlContext , or null
if no Subject is associated
with the provided AccessControlContext .the acc)AccessControlContext from which to retrieve
the Subject .
Deprecated
for removal since 17. This method depends on
AccessControlContext
which, in conjunction with
the Security Manager, is deprecated
and subject to removal in a future release.
Get the |
public int | Returns: a hashcode for thisSubject Overrides java. Returns a hashcode for this |
public boolean | Returns: true if thisSubject is read-only, false otherwise.Query whether this |
private void | readObject(ObjectInputStream
the s)ObjectInputStream from which data is readReads this object from a stream (i.e., deserializes it) |
public void | |
public String | Returns: the String representation of thisSubject .Overrides java. Return the String representation of this |
pack-priv String | toString(boolean includePrivateCredentials)
package private convenience method to print out the Subject without firing off a security check when trying to access the Private Credentials |
private void | writeObject(ObjectOutputStream
the oos)ObjectOutputStream to which data is writtenWrites this object out to a stream (i.e., serializes it). |