2.3. 使用 Elytron 进行身份切换
2.3.1. 在服务器到服务器 EJB 调用中切换身份 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
默认情况下,当您远程调用部署到应用服务器的 EJB 时,用于远程服务器上身份验证的身份与源服务器上所用的相同。在某些情况下,您可能希望在不同身份的安全上下文中运行远程受保护的 EJB。
您可以使用 Elytron API 在服务器到服务器 EJB 调用中切换身份。执行此操作时,通过连接接收的请求将作为新请求执行,使用 API 调用中以编程方式指定的身份。
以下代码示例演示了如何切换用于远程 EJB 身份验证的身份。securityDomain.authenticate() 方法传递的 参数是用于目标服务器上身份验证的身份凭据。
remoteUsername 和 remotePassword
示例:在服务器到服务器 EJB 调用中切换身份
SecurityDomain securityDomain = SecurityDomain.getCurrent();
Callable<T> forwardIdentityCallable = () -> {
return AuthenticationContext.empty()
.with(MatchRule.ALL,
AuthenticationConfiguration.empty()
.setSaslMechanismSelector(SaslMechanismSelector.ALL)
.useForwardedIdentity(securityDomain))
.runCallable(callable);
};
securityDomain.authenticate(remoteUsername, new PasswordGuessEvidence(remotePassword.toCharArray())).runAs(forwardIdentityCallable);