Package javax.security.auth
Class Subject
java.lang.Object
javax.security.auth.Subject
- All Implemented Interfaces:
Serializable
public final class Subject extends Object implements Serializable
The central class of the
javax.security.auth package representing an
authenticated user or entity (both referred to as "subject"). IT defines also
the static methods that allow code to be run, and do modifications according
to the subject's permissions.
A subject has the following features:
- A set of
Principalobjects specifying the identities bound to aSubjectthat distinguish it. - Credentials (public and private) such as certificates, keys, or authentication proofs such as tickets
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description Subject()The default constructor initializing the sets of public and private credentials and principals with the empty set.Subject(boolean readOnly, Set<? extends Principal> subjPrincipals, Set<?> pubCredentials, Set<?> privCredentials)The constructor for the subject, setting its public and private credentials and principals according to the arguments. -
Method Summary
Modifier and Type Method Description static <T> TdoAs(Subject subject, PrivilegedAction<T> action)Runs the code defined byactionusing the permissions granted to theSubjectitself and to the code as well.static <T> TdoAs(Subject subject, PrivilegedExceptionAction<T> action)Runs the code defined byactionusing the permissions granted to the subject and to the code itself.static <T> TdoAsPrivileged(Subject subject, PrivilegedAction<T> action, AccessControlContext context)Run the code defined byactionusing the permissions granted to theSubjectand to the code itself, additionally providing a more specific context.static <T> TdoAsPrivileged(Subject subject, PrivilegedExceptionAction<T> action, AccessControlContext context)Runs the code defined byactionusing the permissions granted to the subject and to the code itself, additionally providing a more specific context.booleanequals(Object obj)Checks two Subjects for equality.Set<Principal>getPrincipals()Returns thisSubject'sPrincipal.<T extends Principal>
Set<T>getPrincipals(Class<T> c)Set<Object>getPrivateCredentials()Returns the private credentials associated with thisSubject.<T> Set<T>getPrivateCredentials(Class<T> c)Returns thisSubject's private credentials which are a subclass of theClassprovided.Set<Object>getPublicCredentials()Returns the public credentials associated with thisSubject.<T> Set<T>getPublicCredentials(Class<T> c)Returns thisSubject's public credentials which are a subclass of theClassprovided.static SubjectgetSubject(AccessControlContext context)Returns theSubjectthat was last associated with thecontextprovided as argument.inthashCode()Returns a hash code of thisSubject.booleanisReadOnly()Returns whether thisSubjectis read-only or not.voidsetReadOnly()Prevents from modifications being done to the credentials andPrincipalsets.StringtoString()Returns aStringrepresentation of thisSubject.
-
Constructor Details
-
Subject
public Subject()The default constructor initializing the sets of public and private credentials and principals with the empty set. -
Subject
public Subject(boolean readOnly, Set<? extends Principal> subjPrincipals, Set<?> pubCredentials, Set<?> privCredentials)The constructor for the subject, setting its public and private credentials and principals according to the arguments.- Parameters:
readOnly-trueif thisSubjectis read-only, thus preventing any modifications to be done.subjPrincipals- the set of Principals that are attributed to thisSubject.pubCredentials- the set of public credentials that distinguish thisSubject.privCredentials- the set of private credentials that distinguish thisSubject.
-
-
Method Details
-
doAs
Runs the code defined byactionusing the permissions granted to theSubjectitself and to the code as well.- Parameters:
subject- the distinguishedSubject.action- the code to be run.- Returns:
- the
Objectreturned when running theaction.
-
doAsPrivileged
public static <T> T doAsPrivileged(Subject subject, PrivilegedAction<T> action, AccessControlContext context)Run the code defined byactionusing the permissions granted to theSubjectand to the code itself, additionally providing a more specific context.- Parameters:
subject- the distinguishedSubject.action- the code to be run.context- the specific context in which theactionis invoked. ifnulla newAccessControlContextis instantiated.- Returns:
- the
Objectreturned when running theaction.
-
doAs
public static <T> T doAs(Subject subject, PrivilegedExceptionAction<T> action) throws PrivilegedActionExceptionRuns the code defined byactionusing the permissions granted to the subject and to the code itself.- Parameters:
subject- the distinguishedSubject.action- the code to be run.- Returns:
- the
Objectreturned when running theaction. - Throws:
PrivilegedActionException- if running theactionthrows an exception.
-
doAsPrivileged
public static <T> T doAsPrivileged(Subject subject, PrivilegedExceptionAction<T> action, AccessControlContext context) throws PrivilegedActionExceptionRuns the code defined byactionusing the permissions granted to the subject and to the code itself, additionally providing a more specific context.- Parameters:
subject- the distinguishedSubject.action- the code to be run.context- the specific context in which theactionis invoked. ifnulla newAccessControlContextis instantiated.- Returns:
- the
Objectreturned when running theaction. - Throws:
PrivilegedActionException- if running theactionthrows an exception.
-
equals
Checks two Subjects for equality. More specifically if the principals, public and private credentials are equal, equality for twoSubjectsis implied.- Overrides:
equalsin classObject- Parameters:
obj- theObjectchecked for equality with thisSubject.- Returns:
trueif the specifiedSubjectis equal to this one.- See Also:
Object.hashCode()
-
getPrincipals
Returns thisSubject'sPrincipal.- Returns:
- this
Subject'sPrincipal.
-
getPrincipals
- Parameters:
c- theClassas a criteria which thePrincipalreturned must satisfy.- Returns:
- this
Subject'sPrincipal. Modifications to the returned set ofPrincipals do not affect thisSubject's set.
-
getPrivateCredentials
Returns the private credentials associated with thisSubject.- Returns:
- the private credentials associated with this
Subject.
-
getPrivateCredentials
Returns thisSubject's private credentials which are a subclass of theClassprovided.- Parameters:
c- theClassas a criteria which the private credentials returned must satisfy.- Returns:
- this
Subject's private credentials. Modifications to the returned set of credentials do not affect thisSubject's credentials.
-
getPublicCredentials
Returns the public credentials associated with thisSubject.- Returns:
- the public credentials associated with this
Subject.
-
getPublicCredentials
Returns thisSubject's public credentials which are a subclass of theClassprovided.- Parameters:
c- theClassas a criteria which the public credentials returned must satisfy.- Returns:
- this
Subject's public credentials. Modifications to the returned set of credentials do not affect thisSubject's credentials.
-
hashCode
public int hashCode()Returns a hash code of thisSubject.- Overrides:
hashCodein classObject- Returns:
- a hash code of this
Subject. - See Also:
Object.equals(java.lang.Object)
-
setReadOnly
public void setReadOnly()Prevents from modifications being done to the credentials andPrincipalsets. After setting it to read-only thisSubjectcan not be made writable again. The destroy method on the credentials still works though. -
isReadOnly
public boolean isReadOnly()Returns whether thisSubjectis read-only or not.- Returns:
- whether this
Subjectis read-only or not.
-
toString
Returns aStringrepresentation of thisSubject. -
getSubject
Returns theSubjectthat was last associated with thecontextprovided as argument.- Parameters:
context- thecontextthat was associated with theSubject.- Returns:
- the
Subjectthat was last associated with thecontextprovided as argument.
-