Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Subject

public final class Subject
extends Object implements Serializable

java.lang.Object
   ↳ javax.security.auth.Subject


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:

Summary

Public constructors

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.

Public methods

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.

<T extends Principal> Set<T> getPrincipals(Class<T> c)

Returns this Subject's Principal which is a subclass of the Class provided.

Set<Principal> getPrincipals()

Returns this Subject's Principal.

<T> Set<T> getPrivateCredentials(Class<T> c)

Returns this Subject's private credentials which are a subclass of the Class provided.

Set<Object> getPrivateCredentials()

Returns the private 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.

Set<Object> getPublicCredentials()

Returns the public credentials associated with this Subject.

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.

Inherited methods

From class java.lang.Object

Public constructors

Subject

Added in API level 1
Subject ()

The default constructor initializing the sets of public and private credentials and principals with the empty set.

Subject

Added in API level 1
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 boolean: true if this Subject is read-only, thus preventing any modifications to be done.
subjPrincipals Set: the set of Principals that are attributed to this Subject.
pubCredentials Set: the set of public credentials that distinguish this Subject.
privCredentials Set: the set of private credentials that distinguish this Subject.

Public methods

doAs

Added in API level 9
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.

Parameters
subject Subject: the distinguished Subject.
action PrivilegedAction: the code to be run.
Returns
T the Object returned when running the action.

doAs

Added in API level 9
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.

Parameters
subject Subject: the distinguished Subject.
action PrivilegedExceptionAction: the code to be run.
Returns
T the Object returned when running the action.
Throws
PrivilegedActionException if running the action throws an exception.

doAsPrivileged

Added in API level 9
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.

Parameters
subject Subject: the distinguished Subject.
action PrivilegedAction: the code to be run.
context AccessControlContext: the specific context in which the action is invoked. if null a new AccessControlContext is instantiated.
Returns
T the Object returned when running the action.

doAsPrivileged

Added in API level 9
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.

Parameters
subject Subject: the distinguished Subject.
action PrivilegedExceptionAction: the code to be run.
context AccessControlContext: the specific context in which the action is invoked. if null a new AccessControlContext is instantiated.
Returns
T the Object returned when running the action.
Throws
PrivilegedActionException if running the action throws an exception.

equals

Added in API level 1
boolean equals (Object obj)

Checks two Subjects for equality. More specifically if the principals, public and private credentials are equal, equality for two Subjects is implied.

Parameters
obj Object: the Object checked for equality with this Subject.
Returns
boolean true if the specified Subject is equal to this one.

getPrincipals

Added in API level 1
Set<T> getPrincipals (Class<T> c)

Returns this Subject's Principal which is a subclass of the Class provided.

Parameters
c Class: the Class as a criteria which the Principal returned must satisfy.
Returns
Set<T> this Subject's Principal. Modifications to the returned set of Principals do not affect this Subject's set.

getPrincipals

Added in API level 1
Set<Principal> getPrincipals ()

Returns this Subject's Principal.

Returns
Set<Principal> this Subject's Principal.

getPrivateCredentials

Added in API level 1
Set<T> getPrivateCredentials (Class<T> c)

Returns this Subject's private credentials which are a subclass of the Class provided.

Parameters
c Class: the Class as a criteria which the private credentials returned must satisfy.
Returns
Set<T> this Subject's private credentials. Modifications to the returned set of credentials do not affect this Subject's credentials.

getPrivateCredentials

Added in API level 1
Set<Object> getPrivateCredentials ()

Returns the private credentials associated with this Subject.

Returns
Set<Object> the private credentials associated with this Subject.

getPublicCredentials

Added in API level 1
Set<T> getPublicCredentials (Class<T> c)

Returns this Subject's public credentials which are a subclass of the Class provided.

Parameters
c Class: the Class as a criteria which the public credentials returned must satisfy.
Returns
Set<T> this Subject's public credentials. Modifications to the returned set of credentials do not affect this Subject's credentials.

getPublicCredentials

Added in API level 1
Set<Object> getPublicCredentials ()

Returns the public credentials associated with this Subject.

Returns
Set<Object> the public credentials associated with this Subject.

getSubject

Added in API level 1
Subject getSubject (AccessControlContext context)

Returns the Subject that was last associated with the context provided as argument.

Parameters
context AccessControlContext: the context that was associated with the Subject.
Returns
Subject the Subject that was last associated with the context provided as argument.

hashCode

Added in API level 1
int hashCode ()

Returns a hash code of this Subject.

Returns
int a hash code of this Subject.

isReadOnly

Added in API level 1
boolean isReadOnly ()

Returns whether this Subject is read-only or not.

Returns
boolean whether this Subject is read-only or not.

setReadOnly

Added in API level 1
void setReadOnly ()

Prevents from modifications being done to the credentials and 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.

toString

Added in API level 1
String toString ()

Returns a String representation of this Subject.

Returns
String a String representation of this Subject.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.