Wildfly / Elytron - 从 ejb 访问身份属性

Wildfly / Elytron - access identity attributes from ejb

我正在将一项旧的遗留服务迁移到 EAP 7.1,它目前通过 LDAP 对用户进行身份验证和授权,我想将其移至它所属的位置 - AS 上的安全域。

我有一个由 LDAP 领域支持的工作安全域,如下所示:

/subsystem=elytron/ldap-realm=***censored***:query
{
  "outcome" => "success",
  "result" => {
    "allow-blank-password" => false,
    "dir-context" => "***censored***",
    "direct-verification" => true,
    "identity-mapping" => {
      "rdn-identifier" => "sAMAccountName",
      "use-recursive-search" => true,
      "search-base-dn" => "***censored***",
      "attribute-mapping" => [
        {
          "from" => "cn",
          "to" => "roles",
          "filter" => "(&(objectClass=group)(member={1}))",
          "filter-base-dn" => "***censored***"
        },
        {"from" => "company", "to" => "company"},
        {"from" => "givenName", "to" => "firstName"},
        {"from" => "sn", "to" => "lastName"},
        {"from" => "mail", "to" => "email"}
      ],
      "user-password-mapper" => {"from" => "userPassword"}
    }
  }
}

授权是针对从 LDAP 属性映射中获取的身份属性 "roles" 完成的。

既然这可行,我想从 LDAP 中获取更多属性(公司、名字、姓氏、电子邮件,可能还有更多)。这些已经被上面的配置拾取并且 Elytron 以我认为我应该期望的方式处理它们:

 <TS> DEBUG [org.wildfly.security] (default task-6) [javax.naming.ldap.InitialLdapContext@5c7e4fb4] successfully created. Connection established to LDAP server.
 <TS> DEBUG [org.wildfly.security] (default task-6) Trying to create identity for principal [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6) Executing search [(sAMAccountName={0})] in context [*****] with arguments [*****]. Returning attributes are [MAIL, SN, GIVENNAME, COMPANY]. Binary attributes are [null].
 <TS> DEBUG [org.wildfly.security] (default task-6) Found entry [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6) Identity for principal [*****] found at [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6) Executing search [(&(objectClass=group)(member={1}))] in context [*****] with arguments [*****, *****]. Returning attributes are [null, CN]. Binary attributes are [null].
 <TS> DEBUG [org.wildfly.security] (default task-6) Found entry [*****].
 .
 . bunch of group memberships
 .
 <TS> DEBUG [org.wildfly.security] (default task-6) Found entry [*****].
 <TS> TRACE [org.wildfly.security] (default task-6) Identity iterating - pagination not supported - end of list
 <TS> DEBUG [org.wildfly.security] (default task-6) Obtaining authorization identity attributes for principal [*****]:
 <TS> DEBUG [org.wildfly.security] (default task-6) Identity [*****] attributes are:
 <TS> DEBUG [org.wildfly.security] (default task-6)     Attribute [lastName] value [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6)     Attribute [firstName] value [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6)     Attribute [roles] value [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6)     Attribute [roles] value [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6)     Attribute [roles] value [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6)     Attribute [roles] value [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6)     Attribute [roles] value [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6)     Attribute [roles] value [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6)     Attribute [roles] value [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6)     Attribute [company] value [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6)     Attribute [email] value [*****].
 <TS> DEBUG [org.wildfly.security] (default task-6) Context [javax.naming.ldap.InitialLdapContext@5c7e4fb4] was closed. Connection closed or just returned to the pool.

我想做的是从受保护的 EJB 中的代码中获取这些身份属性。当然可以注入目录上下文并从代码本身查找属性,但这将是特定于部署的。我非常希望让 AS 通过 SessionContext 或等价物公开这些属性。

我不知道该怎么做。网络上提供的大部分 Elytron material 都是仿效的,它们只是复制 cred 的官方示例,而只是一遍又一遍地重复相同的 Hello World 示例。

所以。总结一下。不要介意这是一个 LDAP 领域。我需要的属性在 Identity 对象中可用。 Identity 后来被转换为没有这些属性的 Principal,据我所知,Principal 是我可以通过 EJB 会话上下文访问的东西。

有什么方法可以让我从 EJB 获得标识,或者至少是它的视图?最好以与实现无关的方式?

此致!

/马格努斯·德鲁格

要从 EJB 获取当前安全标识,可以使用以下代码:

SecurityDomain.getCurrent().getCurrentSecurityIdentity()

可在此处找到有关 SecurityDomain 的更多详细信息:

http://wildfly-security.github.io/wildfly-elytron/master/org/wildfly/security/auth/server/SecurityDomain.html

寻找自定义角色解码器以获取 Wildfly 中的属性。 然后将这些属性存储在某个地方,您可以在其中使用您的委托人来获取它们。