2.2.3. 检索安全身份凭证
有些情况下,您可能需要检索要在传出调用中使用的身份凭据,如 HTTP 客户端。以下示例演示了如何以编程方式检索安全凭据。
import org.wildfly.security.auth.server.IdentityCredentials; import org.wildfly.security.auth.server.SecurityDomain; import org.wildfly.security.auth.server.SecurityIdentity; import org.wildfly.security.credential.PasswordCredential; import org.wildfly.security.password.interfaces.ClearPassword; SecurityIdentity securityIdentity = null; ClearPassword password = null; // Obtain the SecurityDomain for the current deployment. // The calling code requires the // org.wildfly.security.permission.ElytronPermission("getSecurityDomain") permission // if running with a security manager. SecurityDomain securityDomain = SecurityDomain.getCurrent(); if (securityDomain != null) { // Obtain the current security identity from the security domain. // This always returns an identity, but it could be the representation // of the anonymous identity if no authenticated identity is available. securityIdentity = securityDomain.getCurrentSecurityIdentity(); // The private credentials can be accessed to obtain any credentials delegated to the identity. // The calling code requires the // org.wildfly.security.permission.ElytronPermission("getPrivateCredentials") // permission if running with a security manager. IdentityCredentials credentials = securityIdentity.getPrivateCredentials(); if (credentials.contains(PasswordCredential.class)) { password = credentials.getCredential(PasswordCredential.class).getPassword(ClearPassword.class); } }
import org.wildfly.security.auth.server.IdentityCredentials;
import org.wildfly.security.auth.server.SecurityDomain;
import org.wildfly.security.auth.server.SecurityIdentity;
import org.wildfly.security.credential.PasswordCredential;
import org.wildfly.security.password.interfaces.ClearPassword;
SecurityIdentity securityIdentity = null;
ClearPassword password = null;
// Obtain the SecurityDomain for the current deployment.
// The calling code requires the
// org.wildfly.security.permission.ElytronPermission("getSecurityDomain") permission
// if running with a security manager.
SecurityDomain securityDomain = SecurityDomain.getCurrent();
if (securityDomain != null) {
// Obtain the current security identity from the security domain.
// This always returns an identity, but it could be the representation
// of the anonymous identity if no authenticated identity is available.
securityIdentity = securityDomain.getCurrentSecurityIdentity();
// The private credentials can be accessed to obtain any credentials delegated to the identity.
// The calling code requires the
// org.wildfly.security.permission.ElytronPermission("getPrivateCredentials")
// permission if running with a security manager.
IdentityCredentials credentials = securityIdentity.getPrivateCredentials();
if (credentials.contains(PasswordCredential.class)) {
password = credentials.getCredential(PasswordCredential.class).getPassword(ClearPassword.class);
}
}