Class Subject
- All Implemented Interfaces:
Serializable
public final class Subject extends Object implements 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.
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.
Deprecated Methods and Replacements
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 equivalent to getSubject(AccessController.getContext()) (as originally specified) and callAs is similar to doAs except that the input type and exceptions thrown are slightly different. A 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.
- Since:
- 1.4
- See Also:
Constructor Summary
| Constructor | Description |
|---|---|
Subject() |
Create an instance of a Subject with an empty Set of Principals and empty Sets of public and private credentials. |
Subject |
Create an instance of a Subject with Principals and credentials. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
static <T> T |
callAs |
Executes a Callable with subject as the current subject. |
static Subject |
current() |
Returns the current subject. |
static <T> T |
doAs |
Deprecated, for removal: This API element is subject to removal in a future version. |
static <T> T |
doAs |
Deprecated, for removal: This API element is subject to removal in a future version. This method originally performed the specified PrivilegedExceptionAction with privileges enabled. |
static <T> T |
doAsPrivileged |
Deprecated, for removal: This API element is subject to removal in a future version. This method originally performed the specified PrivilegedAction with privileges enabled and restricted by the specified AccessControlContext. |
static <T> T |
doAsPrivileged |
Deprecated, for removal: This API element is subject to removal in a future version. This method originally performed the specified PrivilegedExceptionAction with privileges enabled and restricted by the specified AccessControlContext. |
boolean |
equals |
Compares the specified Object with this Subject for equality. |
Set |
getPrincipals() |
Return the Set of Principals associated with this Subject. |
<T extends Principal> |
getPrincipals |
Return a Set of Principals associated with this Subject that are instances or subclasses of the specified Class. |
Set |
getPrivateCredentials() |
Return the Set of private credentials held by this Subject. |
<T> Set |
getPrivateCredentials |
Return a Set of private credentials associated with this Subject that are instances or subclasses of the specified Class. |
Set |
getPublicCredentials() |
Return the Set of public credentials held by this Subject. |
<T> Set |
getPublicCredentials |
Return a Set of public credentials associated with this Subject that are instances or subclasses of the specified Class. |
static Subject |
getSubject |
Deprecated, for removal: This API element is subject to removal in a future version. This method used to get the subject associated with the provided AccessControlContext, which was only useful in conjunction with the Security Manager, which is no longer supported. |
int |
hashCode() |
Returns a hashcode for this Subject. |
boolean |
isReadOnly() |
Query whether this Subject is read-only. |
void |
setReadOnly() |
Set this Subject to be read-only. |
String |
toString() |
Return the String representation of this Subject. |
Methods declared in class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait | Modifier and Type | Method | Description |
|---|---|---|
protected Object |
clone() |
Creates and returns a copy of this object. |
protected void |
finalize() |
Deprecated, for removal: This API element is subject to removal in a future version. Finalization is deprecated and subject to removal in a future release. |
final Class |
getClass() |
Returns the runtime class of this Object. |
final void |
notify() |
Wakes up a single thread that is waiting on this object's monitor. |
final void |
notifyAll() |
Wakes up all threads that are waiting on this object's monitor. |
final void |
wait() |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
Constructor Details
Subject
public Subject()
Subject with an empty Set of Principals and empty Sets of public and private credentials. The newly constructed Sets check whether this Subject has been set read-only before permitting subsequent modifications. These Sets also prohibit null elements, and attempts to add, query, or remove a null element will result in a NullPointerException.
Subject
public Subject(boolean readOnly, Set<? extends Principal> principals, Set<?> pubCredentials, Set<?> privCredentials)
Subject with Principals and credentials. The Principals and credentials from the specified Sets are copied into newly constructed Sets. These newly created Sets check whether this Subject has been set read-only before permitting subsequent modifications. These Sets also prohibit null elements, and attempts to add, query, or remove a null element will result in a NullPointerException.
- Parameters:
-
readOnly- true if theSubjectis to be read-only, and false otherwise. -
principals- theSetof Principals to be associated with thisSubject. -
pubCredentials- theSetof public credentials to be associated with thisSubject. -
privCredentials- theSetof private credentials to be associated with thisSubject. - Throws:
-
NullPointerException- if the specifiedprincipals,pubCredentials, orprivCredentialsarenull, or a null value exists within any of these three Sets.
Method Details
setReadOnly
public void setReadOnly()
Subject to be read-only. Modifications (additions and removals) to this Subject's Principal Set and credential Sets will be disallowed. The destroy operation on this Subject's credentials will still be permitted.
Subsequent attempts to modify the Subject's Principal and credential Sets will result in an IllegalStateException being thrown. Also, once a Subject is read-only, it can not be reset to being writable again.
isReadOnly
public boolean isReadOnly()
Subject is read-only.- Returns:
- true if this
Subjectis read-only, false otherwise.
getSubject
@Deprecated(since="17", forRemoval=true) public static Subject getSubject(AccessControlContext acc)
AccessControlContext, which was only useful in conjunction with the Security Manager, which is no longer supported. This method has been changed to always throw UnsupportedOperationException. A replacement API named current() has been added which can be used to obtain the current subject. There is no replacement for the Security Manager.UnsupportedOperationException. A replacement API named current() has been added which can be used to obtain the current subject.- Parameters:
-
acc- ignored - Returns:
- n/a
- Throws:
-
UnsupportedOperationException- always - See Also:
current
public static Subject current()
The current subject is installed by the callAs(Subject, Callable) method. When callAs(subject, action) is called, action is executed with subject as its current subject which can be retrieved by this method. After action is finished, the current subject is reset to its previous value. The current subject is null before the first call of callAs().
This method returns the Subject bound to the period of the execution of the current thread.
- Returns:
- the current subject, or
nullif a current subject is not installed or the current subject is set tonull. - Since:
- 18
- See Also:
callAs
public static <T> T callAs(Subject subject, Callable<T> action) throws CompletionException
Callable with subject as the current subject. This method launches action and binds subject to the period of its execution.
- Type Parameters:
T- the type of value returned by thecallmethod ofaction- Parameters:
-
subject- theSubjectthat the specifiedactionwill run as. This parameter may benull. -
action- the code to be run withsubjectas its current subject. Must not benull. - Returns:
- the value returned by the
callmethod ofaction - Throws:
-
NullPointerException- ifactionisnull -
CompletionException- ifaction.call()throws an exception. The cause of theCompletionExceptionis set to the exception thrown byaction.call(). - Since:
- 18
- See Also:
doAs
@Deprecated(since="18", forRemoval=true) public static <T> T doAs(Subject subject, PrivilegedAction<T> action)
PrivilegedAction with privileges enabled. Running the action with privileges enabled was only useful in conjunction with the Security Manager, which is no longer supported. This method has been changed to launch the action as is and bind the subject to the period of its execution. A replacement API named callAs(Subject, Callable) has been added which can be used to perform the same work. There is no replacement for the Security Manager.Subject. This method launches action and binds subject to the period of its execution.
- Type Parameters:
T- the type of the value returned by the PrivilegedAction'srunmethod.- Parameters:
-
subject- theSubjectthat the specifiedactionwill run as. This parameter may benull. -
action- the code to be run as the specifiedSubject. - Returns:
- the value returned by the PrivilegedAction's
runmethod. - Throws:
-
NullPointerException- if thePrivilegedActionisnull. - See Also:
doAs
@Deprecated(since="18", forRemoval=true) public static <T> T doAs(Subject subject, PrivilegedExceptionAction<T> action) throws PrivilegedActionException
PrivilegedExceptionAction with privileges enabled. Running the action with privileges enabled was only useful in conjunction with the Security Manager, which is no longer supported. This method has been changed to launch the action as is and bind the subject to the period of its execution. A replacement API named callAs(Subject, Callable) has been added which can be used to perform the same work. There is no replacement for the Security Manager.Subject. This method launches action and binds subject to the period of its execution.
- Type Parameters:
T- the type of the value returned by the PrivilegedExceptionAction'srunmethod.- Parameters:
-
subject- theSubjectthat the specifiedactionwill run as. This parameter may benull. -
action- the code to be run as the specifiedSubject. - Returns:
- the value returned by the PrivilegedExceptionAction's
runmethod. - Throws:
-
PrivilegedActionException- if thePrivilegedExceptionAction.runmethod throws a checked exception. -
NullPointerException- if the specifiedPrivilegedExceptionActionisnull. - See Also:
doAsPrivileged
@Deprecated(since="17", forRemoval=true) public static <T> T doAsPrivileged(Subject subject, PrivilegedAction<T> action, AccessControlContext acc)
PrivilegedAction with privileges enabled and restricted by the specified AccessControlContext. Running the action with privileges enabled was only useful in conjunction with the Security Manager, which is no longer supported. This method has been changed to ignore the AccessControlContext and launch the action as is and bind the subject to the period of its execution. A replacement API named callAs(Subject, Callable) has been added which can be used to perform the same work. There is no replacement for the Security Manager.Subject. This method launches action and binds subject to the period of its execution.
- Type Parameters:
T- the type of the value returned by the PrivilegedAction'srunmethod.- Parameters:
-
subject- theSubjectthat the specifiedactionwill run as. This parameter may benull. -
action- the code to be run as the specifiedSubject. -
acc- ignored - Returns:
- the value returned by the PrivilegedAction's
runmethod. - Throws:
-
NullPointerException- if thePrivilegedActionisnull. - See Also:
doAsPrivileged
@Deprecated(since="17", forRemoval=true) public static <T> T doAsPrivileged(Subject subject, PrivilegedExceptionAction<T> action, AccessControlContext acc) throws PrivilegedActionException
PrivilegedExceptionAction with privileges enabled and restricted by the specified AccessControlContext. Running the action with privileges enabled was only useful in conjunction with the Security Manager, which is no longer supported. This method has been changed to ignore the AccessControlContext and launch the action as is and bind the subject to the period of its execution. A replacement API named callAs(Subject, Callable) has been added which can be used to perform the same work. There is no replacement for the Security Manager.Subject. This method launches action and binds subject to the period of its execution.
- Type Parameters:
T- the type of the value returned by the PrivilegedExceptionAction'srunmethod.- Parameters:
-
subject- theSubjectthat the specifiedactionwill run as. This parameter may benull. -
action- the code to be run as the specifiedSubject. -
acc- ignored - Returns:
- the value returned by the PrivilegedExceptionAction's
runmethod. - Throws:
-
PrivilegedActionException- if thePrivilegedExceptionAction.runmethod throws a checked exception. -
NullPointerException- if the specifiedPrivilegedExceptionActionisnull. - See Also:
getPrincipals
public Set<Principal> getPrincipals()
Set of Principals associated with this Subject. Each Principal represents an identity for this Subject. The returned Set is backed by this Subject's internal Principal Set. Any modification to the returned Set affects the internal Principal Set as well.
- Returns:
- the
Setof Principals associated with thisSubject.
getPrincipals
public <T extends Principal> Set<T> getPrincipals(Class<T> c)
Set of Principals associated with this Subject that are instances or subclasses of the specified Class. The returned Set is not backed by this Subject's internal Principal Set. A new Set is created and returned for each method invocation. Modifications to the returned Set will not affect the internal Principal Set.
- Type Parameters:
T- the type of the class modeled byc- Parameters:
-
c- the returnedSetof Principals will all be instances of this class. - Returns:
- a
Setof Principals that are instances of the specifiedClass. - Throws:
-
NullPointerException- if the specifiedClassisnull.
getPublicCredentials
public Set<Object> getPublicCredentials()
Set of public credentials held by this Subject. The returned Set is backed by this Subject's internal public Credential Set. Any modification to the returned Set affects the internal public Credential Set as well.
- Returns:
- a
Setof public credentials held by thisSubject.
getPrivateCredentials
public Set<Object> getPrivateCredentials()
Set of private credentials held by this Subject. The returned Set is backed by this Subject's internal private Credential Set. Any modification to the returned Set affects the internal private Credential Set as well.
- Returns:
- a
Setof private credentials held by thisSubject.
getPublicCredentials
public <T> Set<T> getPublicCredentials(Class<T> c)
Set of public credentials associated with this Subject that are instances or subclasses of the specified Class. The returned Set is not backed by this Subject's internal public Credential Set. A new Set is created and returned for each method invocation. Modifications to the returned Set will not affect the internal public Credential Set.
- Type Parameters:
T- the type of the class modeled byc- Parameters:
-
c- the returnedSetof public credentials will all be instances of this class. - Returns:
- a
Setof public credentials that are instances of the specifiedClass. - Throws:
-
NullPointerException- if the specifiedClassisnull.
getPrivateCredentials
public <T> Set<T> getPrivateCredentials(Class<T> c)
Set of private credentials associated with this Subject that are instances or subclasses of the specified Class. The returned Set is not backed by this Subject's internal private Credential Set. A new Set is created and returned for each method invocation. Modifications to the returned Set will not affect the internal private Credential Set.
- Type Parameters:
T- the type of the class modeled byc- Parameters:
-
c- the returnedSetof private credentials will all be instances of this class. - Returns:
- a
Setof private credentials that are instances of the specifiedClass. - Throws:
-
NullPointerException- if the specifiedClassisnull.
equals
public boolean equals(Object o)
Subject for equality. Returns true if the given object is also a Subject and the two Subject instances are equivalent. More formally, two Subject instances are equal if their Principal and Credential Sets are equal.toString
hashCode
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
PrivilegedActionwith privileges enabled.