public final class Subject extends Object implements Serializable
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:
Principal objects specifying the identities bound to a
Subject that distinguish it.| Constructor and 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.
|
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
doAs(Subject subject,
PrivilegedAction<T> action)
Runs the code defined by
action using the permissions granted to
the Subject itself and to the code as well. |
static <T> T |
doAs(Subject subject,
PrivilegedExceptionAction<T> action)
Runs the code defined by
action using the permissions granted to
the subject and to the code itself. |
static <T> T |
doAsPrivileged(Subject subject,
PrivilegedAction<T> action,
AccessControlContext context)
Run the code defined by
action using the permissions granted to
the Subject and to the code itself, additionally providing a more
specific context. |
static <T> T |
doAsPrivileged(Subject subject,
PrivilegedExceptionAction<T> action,
AccessControlContext context)
Runs the code defined by
action using the permissions granted to
the subject and to the code itself, additionally providing a more
specific context. |
boolean |
equals(Object obj)
Checks two Subjects for equality.
|
Set<Principal> |
getPrincipals()
Returns this
Subject's Principal. |
<T extends Principal> |
getPrincipals(Class<T> c)
|
Set<Object> |
getPrivateCredentials()
Returns the private credentials associated with this
Subject. |
<T> Set<T> |
getPrivateCredentials(Class<T> c)
Returns this
Subject's private credentials which are a subclass
of the Class provided. |
Set<Object> |
getPublicCredentials()
Returns the public credentials associated with this
Subject. |
<T> Set<T> |
getPublicCredentials(Class<T> c)
Returns this
Subject's public credentials which are a subclass of
the Class provided. |
static Subject |
getSubject(AccessControlContext context)
Returns the
Subject that was last associated with the context provided as argument. |
int |
hashCode()
Returns a hash code of this
Subject. |
boolean |
isReadOnly()
Returns whether this
Subject is read-only or not. |
void |
setReadOnly()
Prevents from modifications being done to the credentials and
Principal sets. |
String |
toString()
Returns a
String representation of this Subject. |
public Subject()
public Subject(boolean readOnly,
Set<? extends Principal> subjPrincipals,
Set<?> pubCredentials,
Set<?> privCredentials)
readOnly - true if this Subject is read-only, thus
preventing any modifications to be done.subjPrincipals - the set of Principals that are attributed to this Subject.pubCredentials - the set of public credentials that distinguish this Subject.privCredentials - the set of private credentials that distinguish this Subject.public static <T> T doAs(Subject subject, PrivilegedAction<T> action)
action using the permissions granted to
the Subject itself and to the code as well.subject - the distinguished Subject.action - the code to be run.Object returned when running the action.public static <T> T doAsPrivileged(Subject subject, PrivilegedAction<T> action, AccessControlContext context)
action using the permissions granted to
the Subject and to the code itself, additionally providing a more
specific context.subject - the distinguished Subject.action - the code to be run.context - the specific context in which the action is invoked.
if null a new AccessControlContext is
instantiated.Object returned when running the action.public static <T> T doAs(Subject subject, PrivilegedExceptionAction<T> action) throws PrivilegedActionException
action using the permissions granted to
the subject and to the code itself.subject - the distinguished Subject.action - the code to be run.Object returned when running the action.PrivilegedActionException - if running the action throws an exception.public static <T> T doAsPrivileged(Subject subject, PrivilegedExceptionAction<T> action, AccessControlContext context) throws PrivilegedActionException
action using the permissions granted to
the subject and to the code itself, additionally providing a more
specific context.subject - the distinguished Subject.action - the code to be run.context - the specific context in which the action is invoked.
if null a new AccessControlContext is
instantiated.Object returned when running the action.PrivilegedActionException - if running the action throws an exception.public boolean equals(Object obj)
Subjects is implied.equals in class Objectobj - the Object checked for equality with this Subject.true if the specified Subject is equal to this
one.Object.hashCode()public Set<Principal> getPrincipals()
Subject's Principal.Subject's Principal.public <T extends Principal> Set<T> getPrincipals(Class<T> c)
c - the Class as a criteria which the Principal
returned must satisfy.Subject's Principal. Modifications to the
returned set of Principals do not affect this Subject's set.public Set<Object> getPrivateCredentials()
Subject.Subject.public <T> Set<T> getPrivateCredentials(Class<T> c)
Subject's private credentials which are a subclass
of the Class provided.c - the Class as a criteria which the private credentials
returned must satisfy.Subject's private credentials. Modifications to the
returned set of credentials do not affect this Subject's
credentials.public Set<Object> getPublicCredentials()
Subject.Subject.public <T> Set<T> getPublicCredentials(Class<T> c)
Subject's public credentials which are a subclass of
the Class provided.c - the Class as a criteria which the public credentials
returned must satisfy.Subject's public credentials. Modifications to the
returned set of credentials do not affect this Subject's
credentials.public int hashCode()
Subject.hashCode in class ObjectSubject.Object.equals(java.lang.Object)public void setReadOnly()
Principal sets. After setting it to read-only this Subject can
not be made writable again. The destroy method on the credentials still
works though.public boolean isReadOnly()
Subject is read-only or not.Subject is read-only or not.public String toString()
String representation of this Subject.public static Subject getSubject(AccessControlContext context)
Subject that was last associated with the context provided as argument.context - the context that was associated with the
Subject.Subject that was last associated with the context provided as argument.