On this page
Class Subject
- java.lang.Object
-
- javax.security.auth.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. 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:
Principal,DomainCombiner, Serialized Form
Constructors
| Constructor | Description |
|---|---|
Subject() |
Create an instance of a |
Subject(boolean readOnly, Set<? extends Principal> principals, Set<?> pubCredentials, Set<?> privCredentials) |
Create an instance of a |
Methods
| Modifier and Type | Method | Description |
|---|---|---|
static <T> T |
doAs(Subject subject, PrivilegedAction<T> action) |
Perform work as a particular |
static <T> T |
doAs(Subject subject, PrivilegedExceptionAction<T> action) |
Perform work as a particular |
static <T> T |
doAsPrivileged(Subject subject, PrivilegedAction<T> action, AccessControlContext acc) |
Perform privileged work as a particular |
static <T> T |
doAsPrivileged(Subject subject, PrivilegedExceptionAction<T> action, AccessControlContext acc) |
Perform privileged work as a particular |
boolean |
equals(Object o) |
Compares the specified Object with this |
Set<Principal> |
getPrincipals() |
Return the |
<T extends Principal> |
getPrincipals(Class<T> c) |
Return a |
Set<Object> |
getPrivateCredentials() |
Return the |
<T> Set<T> |
getPrivateCredentials(Class<T> c) |
Return a |
Set<Object> |
getPublicCredentials() |
Return the |
<T> Set<T> |
getPublicCredentials(Class<T> c) |
Return a |
static Subject |
getSubject(AccessControlContext acc) |
Get the |
int |
hashCode() |
Returns a hashcode for this |
boolean |
isReadOnly() |
Query whether this |
void |
setReadOnly() |
Set this |
String |
toString() |
Return the String representation of this |
Methods declared in class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Constructors
Subject
public Subject()
Create an instance of a 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. The newly created Sets also prevent illegal modifications by ensuring that callers have sufficient permissions. These Sets also prohibit null elements, and attempts to add or query a null element will result in a NullPointerException.
To modify the Principals Set, the caller must have AuthPermission("modifyPrincipals"). To modify the public credential Set, the caller must have AuthPermission("modifyPublicCredentials"). To modify the private credential Set, the caller must have AuthPermission("modifyPrivateCredentials").
Subject
public Subject(boolean readOnly,
Set<? extends Principal> principals,
Set<?> pubCredentials,
Set<?> privCredentials)
Create an instance of a 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. The newly created Sets also prevent illegal modifications by ensuring that callers have sufficient permissions. These Sets also prohibit null elements, and attempts to add or query a null element will result in a NullPointerException.
To modify the Principals Set, the caller must have AuthPermission("modifyPrincipals"). To modify the public credential Set, the caller must have AuthPermission("modifyPublicCredentials"). To modify the private credential Set, the caller must have AuthPermission("modifyPrivateCredentials").
- 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.
Methods
setReadOnly
public void setReadOnly()
Set this 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.
- Throws:
SecurityException- if a security manager is installed and the caller does not have anAuthPermission("setReadOnly")permission to set thisSubjectto be read-only.
isReadOnly
public boolean isReadOnly()
Query whether this Subject is read-only.
- Returns:
-
true if this
Subjectis read-only, false otherwise.
getSubject
public static Subject getSubject(AccessControlContext acc)
Get the Subject associated with the provided AccessControlContext.
The AccessControlContext may contain many Subjects (from nested doAs calls). In this situation, the most recent Subject associated with the AccessControlContext is returned.
- Parameters:
acc- theAccessControlContextfrom which to retrieve theSubject.- Returns:
-
the
Subjectassociated with the providedAccessControlContext, ornullif noSubjectis associated with the providedAccessControlContext. - Throws:
SecurityException- if a security manager is installed and the caller does not have anAuthPermission("getSubject")permission to get theSubject.NullPointerException- if the providedAccessControlContextisnull.
doAs
public static <T> T doAs(Subject subject,
PrivilegedAction<T> action)
Perform work as a particular Subject.
This method first retrieves the current Thread's AccessControlContext via AccessController.getContext, and then instantiates a new AccessControlContext using the retrieved context along with a new SubjectDomainCombiner (constructed using the provided Subject). Finally, this method invokes AccessController.doPrivileged, passing it the provided PrivilegedAction, as well as the newly constructed AccessControlContext.
- 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.SecurityException- if a security manager is installed and the caller does not have anAuthPermission("doAs")permission to invoke this method.
doAs
public static <T> T doAs(Subject subject,
PrivilegedExceptionAction<T> action)
throws PrivilegedActionException
Perform work as a particular Subject.
This method first retrieves the current Thread's AccessControlContext via AccessController.getContext, and then instantiates a new AccessControlContext using the retrieved context along with a new SubjectDomainCombiner (constructed using the provided Subject). Finally, this method invokes AccessController.doPrivileged, passing it the provided PrivilegedExceptionAction, as well as the newly constructed AccessControlContext.
- 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.SecurityException- if a security manager is installed and the caller does not have anAuthPermission("doAs")permission to invoke this method.
doAsPrivileged
public static <T> T doAsPrivileged(Subject subject,
PrivilegedAction<T> action,
AccessControlContext acc)
Perform privileged work as a particular Subject.
This method behaves exactly as Subject.doAs, except that instead of retrieving the current Thread's AccessControlContext, it uses the provided AccessControlContext. If the provided AccessControlContext is null, this method instantiates a new AccessControlContext with an empty collection of ProtectionDomains.
- 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- theAccessControlContextto be tied to the specified subject and action.- Returns:
-
the value returned by the PrivilegedAction's
runmethod. - Throws:
NullPointerException- if thePrivilegedActionisnull.SecurityException- if a security manager is installed and the caller does not have aAuthPermission("doAsPrivileged")permission to invoke this method.
doAsPrivileged
public static <T> T doAsPrivileged(Subject subject,
PrivilegedExceptionAction<T> action,
AccessControlContext acc)
throws PrivilegedActionException
Perform privileged work as a particular Subject.
This method behaves exactly as Subject.doAs, except that instead of retrieving the current Thread's AccessControlContext, it uses the provided AccessControlContext. If the provided AccessControlContext is null, this method instantiates a new AccessControlContext with an empty collection of ProtectionDomains.
- 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- theAccessControlContextto be tied to the specified subject and action.- Returns:
-
the value returned by the PrivilegedExceptionAction's
runmethod. - Throws:
PrivilegedActionException- if thePrivilegedExceptionAction.runmethod throws a checked exception.NullPointerException- if the specifiedPrivilegedExceptionActionisnull.SecurityException- if a security manager is installed and the caller does not have aAuthPermission("doAsPrivileged")permission to invoke this method.
getPrincipals
public Set<Principal> getPrincipals()
Return the 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.
If a security manager is installed, the caller must have a AuthPermission("modifyPrincipals") permission to modify the returned set, or a SecurityException will be thrown.
- Returns:
-
the
Setof Principals associated with thisSubject.
getPrincipals
public <T extends Principal> Set<T> getPrincipals(Class<T> c)
Return a 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()
Return the 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.
If a security manager is installed, the caller must have a AuthPermission("modifyPublicCredentials") permission to modify the returned set, or a SecurityException will be thrown.
- Returns:
-
a
Setof public credentials held by thisSubject.
getPrivateCredentials
public Set<Object> getPrivateCredentials()
Return the 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.
If a security manager is installed, the caller must have a AuthPermission("modifyPrivateCredentials") permission to modify the returned set, or a SecurityException will be thrown.
While iterating through the Set, a SecurityException is thrown if a security manager is installed and the caller does not have a PrivateCredentialPermission to access a particular Credential. The Iterator is nevertheless advanced to the next element in the Set.
- Returns:
-
a
Setof private credentials held by thisSubject.
getPublicCredentials
public <T> Set<T> getPublicCredentials(Class<T> c)
Return a 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)
Return a Set of private credentials associated with this Subject that are instances or subclasses of the specified Class.
If a security manager is installed, the caller must have a PrivateCredentialPermission to access all of the requested Credentials, or a SecurityException will be thrown.
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)
Compares the specified Object with this 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.
- Overrides:
equalsin classObject- Parameters:
o- Object to be compared for equality with thisSubject.- Returns:
-
true if the specified Object is equal to this
Subject. - Throws:
SecurityException- if a security manager is installed and the caller does not have aPrivateCredentialPermissionpermission to access the private credentials for thisSubjector the providedSubject.- See Also:
Object.hashCode(),HashMap
toString
public String toString()
Return the String representation of this Subject.
hashCode
public int hashCode()
Returns a hashcode for this Subject.
- Overrides:
hashCodein classObject- Returns:
-
a hashcode for this
Subject. - Throws:
SecurityException- if a security manager is installed and the caller does not have aPrivateCredentialPermissionpermission to access this Subject's private credentials.- See Also:
Object.equals(java.lang.Object),System.identityHashCode(java.lang.Object)
© 1993, 2020, 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.
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/javax/security/auth/Subject.html