Subject (Java SE 15 & JDK 15) (original) (raw)

All Implemented Interfaces:

[Serializable](../../../java/io/Serializable.html "interface in java.io")


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 aSubject. 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 sameSubject 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 credentialSet. 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 orgetPrivateCredentials 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 concretePrincipal 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 with an empty Set of Principals and empty Sets of public and private credentials.
Subject​(boolean readOnly,Set principals,[Set](../../../java/util/Set.html "interface in java.util") pubCredentials,Set<?> privCredentials) Create an instance of a Subject with Principals and credentials.
Modifier and Type Method Description
static T doAs​(Subject subject,PrivilegedAction action) Perform work as a particular Subject.
static T doAs​(Subject subject,PrivilegedExceptionAction action) Perform work as a particular Subject.
static T doAsPrivileged​(Subject subject,PrivilegedAction action,AccessControlContext acc) Perform privileged work as a particular Subject.
static T doAsPrivileged​(Subject subject,PrivilegedExceptionAction action,AccessControlContext acc) Perform privileged work as a particular Subject.
boolean equals​(Object o) Compares the specified Object with this Subject for equality.
Set<Principal> getPrincipals() Return the Set of Principals associated with thisSubject.
<T extends Principal>Set getPrincipals​(Class c) Return a Set of Principals associated with thisSubject that are instances or subclasses of the specifiedClass.
Set<Object> getPrivateCredentials() Return the Set of private credentials held by thisSubject.
Set getPrivateCredentials​(Class c) Return a Set of private credentials associated with thisSubject that are instances or subclasses of the specifiedClass.
Set<Object> getPublicCredentials() Return the Set of public credentials held by thisSubject.
Set getPublicCredentials​(Class c) Return a Set of public credentials associated with thisSubject that are instances or subclasses of the specifiedClass.
static Subject getSubject​(AccessControlContext acc) Get the Subject associated with the providedAccessControlContext.
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.