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 。
也可以看看:
Public constructors |
|
|---|---|
Subject() 创建一个 |
|
Subject(boolean readOnly, Set<? extends Principal> principals, Set<?> pubCredentials, Set<?> privCredentials) 使用Principals和凭证创建 |
|
公共方法(Public methods) |
|
|---|---|
static <T> T |
doAs(Subject subject, PrivilegedExceptionAction<T> action) 以特定的 |
static <T> T |
doAs(Subject subject, PrivilegedAction<T> action) 以特定的 |
static <T> T |
doAsPrivileged(Subject subject, PrivilegedExceptionAction<T> action, AccessControlContext acc) 执行特殊工作作为特定的 |
static <T> T |
doAsPrivileged(Subject subject, PrivilegedAction<T> action, AccessControlContext acc) 执行特殊工作作为特定的 |
boolean |
equals(Object o) 将指定的Object与此 |
Set<Principal> |
getPrincipals() 返回 |
<T extends Principal> Set<T> |
getPrincipals(Class<T> c) 返回与此 |
<T> Set<T> |
getPrivateCredentials(Class<T> c) 返回 |
Set<Object> |
getPrivateCredentials() 返回 |
Set<Object> |
getPublicCredentials() 返回 |
<T> Set<T> |
getPublicCredentials(Class<T> c) 返回 |
static Subject |
getSubject(AccessControlContext acc) 获取与提供的 |
int |
hashCode() 返回此 |
boolean |
isReadOnly() 查询这个 |
void |
setReadOnly() 将此 |
String |
toString() 返回此 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
Subject ()
使用 Subject的空白 Set创建 Subject的实例,并清空公共和专用凭证的集合。
新构建的集合检查此Subject在允许后续修改之前是否已设置为只读。 新创建的套件还通过确保呼叫者拥有足够的权限来防止非法修改。
要修改校长集,来电者必须有AuthPermission("modifyPrincipals") 。 要修改公共证书Set,来电者必须有AuthPermission("modifyPublicCredentials") 。 要修改私人凭证Set,来电者必须有AuthPermission("modifyPrivateCredentials") 。
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. |
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. |
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. |
T doAsPrivileged (Subject subject, PrivilegedExceptionAction<T> action, AccessControlContext acc)
执行特殊工作作为特定的 Subject 。
此方法的行为与Subject.doAs完全相同,不同之处在于不使用检索当前线程的AccessControlContext ,而是使用提供的AccessControlContext 。 如果提供的AccessControlContext是null ,则此方法使用一个空的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. |
T doAsPrivileged (Subject subject, PrivilegedAction<T> action, AccessControlContext acc)
执行特殊工作作为特定的 Subject 。
此方法的行为与Subject.doAs完全相同,只是它不使用检索当前线程的AccessControlContext ,而是使用提供的AccessControlContext 。 如果提供的AccessControlContext为null ,则此方法使用一个空的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. |
boolean equals (Object o)
将指定的Object与此Subject进行比较以获得相等性。 如果给定对象也是一个主题和两个返回true Subject实例是等价的。 更正式地说,两名Subject如果它们的实例都是平等Principal个Credential集合相等。
| 参数(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. |
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. |
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. |
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. |
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. |
Set<Object> getPublicCredentials ()
退还 Set的公共证书的 Subject 。
返回的Set由此主题的内部公共凭证Set 。 对返回的Set进行的任何修改Set影响内部公共证书Set 。
| 返回(Returns) | |
|---|---|
Set<Object> |
A Set of public credentials held by this Subject. |
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. |
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. |
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. |
boolean isReadOnly ()
Query whether this Subject is read-only.
| 返回(Returns) | |
|---|---|
boolean |
true if this Subject is read-only, false otherwise. |
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. |
String toString ()
返回此 Subject的字符串表示 Subject 。
| 返回(Returns) | |
|---|---|
String |
the String representation of this Subject. |