Most visited

Recently visited

Added in API level 1

Subject

public final class Subject
extends Object implements Serializable

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


A Subject代表单个实体(例如人)的相关信息的分组。 这些信息包括主体的身份以及与安全相关的属性(例如密码和加密密钥)。

受试者可能有多个身份。 每个身份在Principal内表示为Subject 委托人只需将名称绑定到Subject 例如,碰巧是一个人Alice的Subject可能有两个负责人:一个将“Alice Bar”(她的驾驶执照上的Subject绑定到Subject ,另一个绑定“999-99-9999” ,她的学生证上的号码是Subject 尽管每个Subject都有不同的名字,但两位委托人都提及相同的Subject

Subject也可能拥有与安全相关的属性,这些属性被称为凭证。 需要特殊保护的敏感凭证(如私人密码钥匙)存储在私人凭证Set 旨在共享的证书(如公钥证书或Kerberos服务器票证)存储在公共证书Set 访问和修改不同的凭证集需要不同的权限。

要检索与Subject关联的所有主体,请调用getPrincipals方法。 要检索属于Subject所有公用或专用凭证,请分别调用getPublicCredentials方法或getPrivateCredentials方法。 要修改Principals和凭证的返回Set ,请使用Set类中定义的方法。 例如:

      Subject subject;
      Principal principal;
      Object credential;

      // add a Principal and credential to the Subject
      subject.getPrincipals().add(principal);
      subject.getPublicCredentials().add(credential);
 

Subject类实现Serializable 在与Subject相关联的Subject序列化时,与Subject相关的凭证不是。 请注意, java.security.Principal类不执行Serializable 因此,所有的混凝土Principal与主题相关的实现都必须实现Serializable

也可以看看:

Summary

Public constructors

Subject()

创建一个 Subject的实例,其中包含空白的 Set Principals以及空的公共和专用凭证集。

Subject(boolean readOnly, Set<? extends Principal> principals, Set<?> pubCredentials, Set<?> privCredentials)

使用Principals和凭证创建 Subject的实例。

Public methods

static <T> T doAs(Subject subject, PrivilegedExceptionAction<T> action)

以特定的 Subject执行工作 Subject

static <T> T doAs(Subject subject, PrivilegedAction<T> action)

以特定的 Subject执行工作 Subject

static <T> T doAsPrivileged(Subject subject, PrivilegedExceptionAction<T> action, AccessControlContext acc)

执行特殊工作作为特定的 Subject

static <T> T doAsPrivileged(Subject subject, PrivilegedAction<T> action, AccessControlContext acc)

执行特殊工作作为特定的 Subject

boolean equals(Object o)

将指定的Object与此 Subject进行比较以获得相等性。

Set<Principal> getPrincipals()

返回 Set与此相关的校长 Subject

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

返回与此 Subject关联的 Set个主体,这些 Subject是指定的 Class实例或子类。

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

返回 Set与此 Subject关联的私人凭证,这些 Subject是指定的 Class实例或子类。

Set<Object> getPrivateCredentials()

返回 Set持有的 Subject凭证。

Set<Object> getPublicCredentials()

返回 Set本公开证书 Subject

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

返回 Set与此 Subject关联的公共凭证,它们是指定的 Class实例或子类。

static Subject getSubject(AccessControlContext acc)

获取与提供的 Subject关联的 AccessControlContext

int hashCode()

返回此 Subject的哈希 Subject

boolean isReadOnly()

查询这个 Subject是否是只读的。

void setReadOnly()

将此 Subject设置为只读。

String toString()

返回此 Subject的字符串表示 Subject

Inherited methods

From class java.lang.Object

Public constructors

Subject

Added in API level 1
Subject ()

使用 Subject的空白 Set创建 Subject的实例,并清空公共和专用凭证的集合。

新构建的集合检查此Subject在允许后续修改之前是否已设置为只读。 新创建的套件还通过确保呼叫者拥有足够的权限来防止非法修改。

要修改校长集,来电者必须有AuthPermission("modifyPrincipals") 要修改公共证书Set,来电者必须有AuthPermission("modifyPublicCredentials") 要修改私人凭证Set,来电者必须有AuthPermission("modifyPrivateCredentials")

Subject

Added in API level 1
Subject (boolean readOnly, 
                Set<? extends Principal> principals, 
                Set<?> pubCredentials, 
                Set<?> privCredentials)

使用Principals和凭证创建 Subject的实例。

来自指定集合的委托人和凭证被复制到新构建的集合中。 这些新创建的集合检查此Subject在允许后续修改之前是否已设置为只读。 新创建的套件还通过确保呼叫者拥有足够的权限来防止非法修改。

要修改校长集,来电者必须有AuthPermission("modifyPrincipals") 要修改公共证书Set,来电者必须有AuthPermission("modifyPublicCredentials") 要修改私人证书Set,来电者必须有AuthPermission("modifyPrivateCredentials")

Parameters
readOnly boolean: true if the Subject is to be read-only, and false otherwise.

principals Set: the Set of Principals to be associated with this Subject.

pubCredentials Set: the Set of public credentials to be associated with this Subject.

privCredentials Set: the Set of private credentials to be associated with this Subject.
Throws
NullPointerException if the specified principals, pubCredentials, or privCredentials are null.

Public methods

doAs

Added in API level 9
T doAs (Subject subject, 
                PrivilegedExceptionAction<T> action)

以特定的 Subject执行工作 Subject

此方法首先通过AccessController.getContext检索当前线程的AccessControlContext ,然后使用检索的上下文和新的SubjectDomainCombiner (使用提供的Subject )实例化新的AccessControlContext 最后,这个方法调用AccessController.doPrivileged ,传递它提供的PrivilegedExceptionAction ,以及新构建的AccessControlContext

Parameters
subject Subject: the Subject that the specified action will run as. This parameter may be null.

action PrivilegedExceptionAction: the code to be run as the specified Subject.

Returns
T the value returned by the PrivilegedExceptionAction's run method.
Throws
PrivilegedActionException if the PrivilegedExceptionAction.run method throws a checked exception.

NullPointerException if the specified PrivilegedExceptionAction is null.

SecurityException if the caller does not have permission to invoke this method.

doAs

Added in API level 9
T doAs (Subject subject, 
                PrivilegedAction<T> action)

以特定的 Subject执行工作 Subject

此方法首先通过AccessController.getContext检索当前线程的AccessControlContext ,然后使用检索到的上下文和新的SubjectDomainCombiner (使用提供的Subject )实例化新的AccessControlContext 最后,这个方法调用AccessController.doPrivileged ,传递它提供的PrivilegedAction ,以及新构建的AccessControlContext

Parameters
subject Subject: the Subject that the specified action will run as. This parameter may be null.

action PrivilegedAction: the code to be run as the specified Subject.

Returns
T the value returned by the PrivilegedAction's run method.
Throws
NullPointerException if the PrivilegedAction is null.

SecurityException if the caller does not have permission to invoke this method.

doAsPrivileged

Added in API level 9
T doAsPrivileged (Subject subject, 
                PrivilegedExceptionAction<T> action, 
                AccessControlContext acc)

执行特殊工作作为特定的 Subject

此方法的行为与Subject.doAs完全相同,不同之处在于不使用检索当前线程的AccessControlContext ,而是使用提供的AccessControlContext 如果提供的AccessControlContextnull ,则此方法使用一个空的ProtectionDomain集实例化一个新的AccessControlContext

Parameters
subject Subject: the Subject that the specified action will run as. This parameter may be null.

action PrivilegedExceptionAction: the code to be run as the specified Subject.

acc AccessControlContext: the AccessControlContext to be tied to the specified subject and action.

Returns
T the value returned by the PrivilegedExceptionAction's run method.
Throws
PrivilegedActionException if the PrivilegedExceptionAction.run method throws a checked exception.

NullPointerException if the specified PrivilegedExceptionAction is null.

SecurityException if the caller does not have permission to invoke this method.

doAsPrivileged

Added in API level 9
T doAsPrivileged (Subject subject, 
                PrivilegedAction<T> action, 
                AccessControlContext acc)

执行特殊工作作为特定的 Subject

此方法的行为与Subject.doAs完全相同,只是它不使用检索当前线程的AccessControlContext ,而是使用提供的AccessControlContext 如果提供的AccessControlContextnull ,则此方法使用一个空的ProtectionDomain集实例化新的AccessControlContext

Parameters
subject Subject: the Subject that the specified action will run as. This parameter may be null.

action PrivilegedAction: the code to be run as the specified Subject.

acc AccessControlContext: the AccessControlContext to be tied to the specified subject and action.

Returns
T the value returned by the PrivilegedAction's run method.
Throws
NullPointerException if the PrivilegedAction is null.

SecurityException if the caller does not have permission to invoke this method.

equals

Added in API level 1
boolean equals (Object o)

将指定的Object与此Subject进行比较以获得相等性。 如果给定对象也是一个主题和两个返回true Subject实例是等价的。 更正式地说,两名Subject如果它们的实例都是平等PrincipalCredential集合相等。

Parameters
o Object: Object to be compared for equality with this Subject.
Returns
boolean true if the specified Object is equal to this Subject.
Throws
SecurityException if the caller does not have permission to access the private credentials for this Subject, or if the caller does not have permission to access the private credentials for the provided Subject.

getPrincipals

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

返回与此Subject相关的Set负责Subject 每个Principal代表这个Subject的身份。

返回的Set由此主题的内部Principal Set 对返回的Set进行的任何修改Set影响内部Principal Set

Returns
Set<Principal> The Set of Principals associated with this Subject.

getPrincipals

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

返回 Set与此 Subject关联的Principals的 Subject ,它们是指定的 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.

Parameters
c Class: the returned Set of Principals will all be instances of this class.
Returns
Set<T> a Set of Principals that are instances of the specified Class.
Throws
NullPointerException if the specified Class is null.

getPrivateCredentials

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

返回与此 Subject关联的 Set个私人凭证,它们是指定的 Class实例或子类。

调用者必须有权访问所有请求的凭证,否则将抛出 SecurityException

返回的Set不受本主题的内部专用证书Set 为每个方法调用创建并返回一个新的Set 对返回的Set修改不会影响内部专用证书Set

Parameters
c Class: the returned Set of private credentials will all be instances of this class.
Returns
Set<T> a Set of private credentials that are instances of the specified Class.
Throws
NullPointerException if the specified Class is null.

getPrivateCredentials

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

返回 Set的私人凭证的 Subject

返回的Set由此主题的内部专用证书Set 对返回的Set任何修改Set影响到内部私人凭证Set

来电者需要权限才能访问返回的Set的凭证,或者要修改Set本身。 如果调用者没有适当的权限,则会引发SecurityException

While iterating through the Set, a SecurityException is thrown if the caller does not have permission to access a particular Credential. The Iterator is nevertheless advanced to next element in the Set.

Returns
Set<Object> A Set of private credentials held by this Subject.

getPublicCredentials

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

退还 Set的公共证书的 Subject

返回的Set由此主题的内部公共凭证Set 对返回的Set进行的任何修改Set影响内部公共证书Set

Returns
Set<Object> A Set of public credentials held by this Subject.

getPublicCredentials

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

返回一个 Set与此相关的公开证书 Subject是实例或指定的子类 Class

返回的Set不支持此主题的内部公共凭证Set 为每个方法调用创建并返回一个新的Set 对返回的Set修改不会影响公共证书Set

Parameters
c Class: the returned Set of public credentials will all be instances of this class.
Returns
Set<T> a Set of public credentials that are instances of the specified Class.
Throws
NullPointerException if the specified Class is null.

getSubject

Added in API level 1
Subject getSubject (AccessControlContext acc)

获取与提供的 Subject关联的 AccessControlContext

AccessControlContext可能包含许多主题(来自嵌套的doAs调用)。 在这种情况下,最近Subject与相关AccessControlContext返回。

Parameters
acc AccessControlContext: the AccessControlContext from which to retrieve the Subject.
Returns
Subject the Subject associated with the provided AccessControlContext, or null if no Subject is associated with the provided AccessControlContext.
Throws
SecurityException if the caller does not have permission to get the Subject.

NullPointerException if the provided AccessControlContext is null.

hashCode

Added in API level 1
int hashCode ()

返回此 Subject的哈希 Subject

Returns
int a hashcode for this Subject.
Throws
SecurityException if the caller does not have permission to access this Subject's private credentials.

isReadOnly

Added in API level 1
boolean isReadOnly ()

Query whether this Subject is read-only.

Returns
boolean true if this Subject is read-only, false otherwise.

setReadOnly

Added in API level 1
void setReadOnly ()

将此 Subject设置为只读。

本主题的Principal Set和凭证集的修改(添加和删除)将被禁止。 对该主题凭据的destroy操作仍将被允许。

随后尝试修改主题的Principal和凭证集将导致引发IllegalStateException 另外,一旦Subject是只读的,它就不能被重置为可再次写入。

Throws
SecurityException if the caller does not have permission to set this Subject to be read-only.

toString

Added in API level 1
String toString ()

返回此 Subject的字符串表示 Subject

Returns
String the String representation of this Subject.

Hooray!