- All Implemented Interfaces:
Serializable
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
.
- Since:
- 1.4
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionSubject()
Create an instance of aSubject
with an emptySet
of Principals and empty Sets of public and private credentials.Subject
(boolean readOnly, Set<? extends Principal> principals, Set<?> pubCredentials, Set<?> privCredentials) Create an instance of aSubject
with Principals and credentials. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
Executes aCallable
withsubject
as the current subject.static Subject
current()
Returns the current subject.static <T> T
doAs
(Subject subject, PrivilegedAction<T> action) Deprecated, for removal: This API element is subject to removal in a future version.static <T> T
doAs
(Subject subject, PrivilegedExceptionAction<T> action) Deprecated, for removal: This API element is subject to removal in a future version.This method depends onAccessControlContext
which, in conjunction with the Security Manager, is deprecated and subject to removal in a future release.static <T> T
doAsPrivileged
(Subject subject, PrivilegedAction<T> action, AccessControlContext acc) Deprecated, for removal: This API element is subject to removal in a future version.This method is only useful in conjunction with the Security Manager, which is deprecated and subject to removal in a future release.static <T> T
doAsPrivileged
(Subject subject, PrivilegedExceptionAction<T> action, AccessControlContext acc) Deprecated, for removal: This API element is subject to removal in a future version.This method is only useful in conjunction with the Security Manager, which is deprecated and subject to removal in a future release.boolean
Compares the specified Object with thisSubject
for equality.Return theSet
of Principals associated with thisSubject
.getPrincipals
(Class<T> c) Return aSet
of Principals associated with thisSubject
that are instances or subclasses of the specifiedClass
.Return theSet
of private credentials held by thisSubject
.<T> Set<T>
getPrivateCredentials
(Class<T> c) Return aSet
of private credentials associated with thisSubject
that are instances or subclasses of the specifiedClass
.Return theSet
of public credentials held by thisSubject
.<T> Set<T>
getPublicCredentials
(Class<T> c) Return aSet
of public credentials associated with thisSubject
that are instances or subclasses of the specifiedClass
.static Subject
AccessControlContext
which, in conjunction with the Security Manager, is deprecated and subject to removal in a future release.