14.5. 配置资源适配器以使用 Elytron 子系统


IronJacamar 中的资源适配器涉及服务器与资源适配器之间的两种类型的通信:

  • 一个类型是服务器打开资源适配器连接时。这可以由容器管理的登录服务保护,这需要在打开连接时将带有主体和凭证的 JAAS 主题传播到资源适配器。此登录服务可以委派给 Elytron。
  • 当资源适配器在向工作管理器提交工作或向同一 JBoss EAP 实例中的端点交付信息时,会建立安全信息。这个机制被称为安全流(security inflow)。

14.5.1. 使用 Elytron 管理的 Sign-On

为了使用 Elytron 实现容器管理的符号,则 elytron-enabled 属性需要设为 true。这会导致所有与资源适配器的连接由 Elytron 保护。

/subsystem=resource-adapters/resource-adapter=<RAR_NAME>/connection-definitions=<FACTORY_NAME>:write-attribute(name=elytron-enabled,value=true)
Copy to Clipboard Toggle word wrap

可以在 resource-adapters 子系统中将 elytron-enabled 属性设置为 true,使用管理 CLI 配置 elytron-enabled 属性。默认情况下,此属性设置为 false

authentication-context 属性定义将用于执行登录的 Elytron 身份验证上下文的名称。

Elytron authentication-context 属性可以包含一个或多个 authentication-configuration 元素,其中包含您要使用的凭证。

如果没有设置 authentication-context 属性,JBoss EAP 将使用当前的 authentication-context,这是打开连接的调用器代码使用的 authentication-context。

示例:创建 authentication-configuration

/subsystem=elytron/authentication-configuration=exampleAuthConfig:add(authentication-name=sa,credential-reference={clear-text=sa})
Copy to Clipboard Toggle word wrap

示例:使用上述配置创建 authentication-context

/subsystem=elytron/authentication-context=exampleAuthContext:add(match-rules=[{authentication-configuration=exampleAuthConfig}])
Copy to Clipboard Toggle word wrap

14.5.2. 使用 Elytron 的安全流

安全流(security inflow)使资源适配器能够在将工作提交到工作管理器或向同一 JBoss EAP 实例中的端点交付消息时建立安全信息。这允许在使用 Elytron 执行前对自己进行身份验证。如果身份验证成功,则会在生成的身份验证上下文中执行提交的工作。如果失败,则工作执行将被拒绝。

要启用 Elytron security inflow,请在配置资源适配器的工作管理器时设置 wm-elytron-security-domain 属性。

注意
  • 当资源适配器工作管理器配置为使用 Elytron 安全域时,wm-elytron-security-domain 则引用的工作管理器应将 elytron-enabled 属性设置为 true
/subsystem=jca/workmanager=customWM:add(name=customWM, elytron-enabled=true)
Copy to Clipboard Toggle word wrap
  • 如果使用 wm-security-domain 属性而不是 wm-elytron-security-domain 属性,则流中的安全由旧的 security 子系统执行。

在下面的 jca 子系统配置示例中,我们可以看到名为 ra-with-elytron-security-domain 的资源适配器的配置。此资源适配器将工作管理器安全性配置为使用 Elytron 安全域的 wm-realm

<subsystem xmlns="urn:jboss:domain:jca:5.0">
    <archive-validation enabled="true" fail-on-error="true" fail-on-warn="false"/>
    <bean-validation enabled="true"/>
    <default-workmanager>
        <short-running-threads>
            <core-threads count="50"/>
            <queue-length count="50"/>
            <max-threads count="50"/>
            <keepalive-time time="10" unit="seconds"/>
        </short-running-threads>
        <long-running-threads>
            <core-threads count="50"/>
            <queue-length count="50"/>
            <max-threads count="50"/>
            <keepalive-time time="10" unit="seconds"/>
        </long-running-threads>
    </default-workmanager>
    <workmanager name="customWM">
        <elytron-enabled>true</elytron-enabled>
        <short-running-threads>
            <core-threads count="20"/>
            <queue-length count="20"/>
            <max-threads count="20"/>
        </short-running-threads>
    </workmanager>
    <bootstrap-contexts>
        <bootstrap-context name="customContext" workmanager="customWM"/>
    </bootstrap-contexts>
    <cached-connection-manager/>
</subsystem>
Copy to Clipboard Toggle word wrap

然后,使用 resource-adapter 子系统中的 boostrap 上下文来引用工作管理器。

<subsystem xmlns="urn:jboss:domain:resource-adapters:5.0">
    <resource-adapters>
        <resource-adapter id="ra-with-elytron-security-domain">
            <archive>
                ra-with-elytron-security-domain.rar
            </archive>
            <bootstrap-context>customContext</bootstrap-context>
            <transaction-support>NoTransaction</transaction-support>
            <workmanager>
                <security>
                    <elytron-security-domain>wm-realm</elytron-security-domain>
                    <default-principal>wm-default-principal</default-principal>
                    <default-groups>
                        <group>
                            wm-default-group
                        </group>
                    </default-groups>
                </security>
            </workmanager>
        </resource-adapter>
    </resource-adapters>
</subsystem>
Copy to Clipboard Toggle word wrap

示例:安全域的配置

/subsystem=elytron/properties-realm=wm-properties-realm:add(users-properties={path=/security-dir/users.properties, plain-text=true}, groups-properties={path=/security-dir/groups.properties})

/subsystem=elytron/simple-role-decoder=wm-role-decoder:add(attribute=groups)

/subsystem=elytron/constant-permission-mapper=wm-permission-mapper:add(permissions=[{class-name="org.wildfly.security.auth.permission.LoginPermission"}])

/subsystem=elytron/security-domain=wm-realm:add(default-realm=wm-properties-realm, permission-mapper=wm-permission-mapper, realms=[{role-decoder=wm-role-decoder, realm=wm-properties-realm}])
Copy to Clipboard Toggle word wrap

Work 类负责在指定域下为 Elytron 进行身份验证提供凭证。为此,它必须实现 jakarta.resource.spi.work.WorkContextProvider

public interface WorkContextProvider {
   /**
    * Gets an instance of <code>WorkContexts</code> that needs to be used
    * by the <code>WorkManager</code> to set up the execution context while
    * executing a <code>Work</code> instance.
    *
    * @return an <code>List</code> of <code>WorkContext</code> instances.
    */
   List<WorkContext> getWorkContexts();
}
Copy to Clipboard Toggle word wrap

此界面允许 Work Context 使用 WorkContext 配置要在其上执行工作的上下文的一些方面。这些方面之一就是安全性 inflow。为此,List<WorkContext> getWorkContexts 方法必须提供 jakarta.resource.spi.work.SecurityContext。此上下文将使用 jakarta.security.auth.callback.Callback 对象,如 Jakarta Authentication 所定义。

示例:使用上下文创建回调

public class ExampleWork implements Work, WorkContextProvider {

    private final String username;
    private final String role;

    public MyWork(TestBean bean, String username, String role) {
        this.principals = null;
        this.roles = null;
        this.bean = bean;
        this.username = username;
        this.role = role;
    }

    public List<WorkContext> getWorkContexts() {
        List<WorkContext> l = new ArrayList<>(1);
        l.add(new MySecurityContext(username, role));
        return l;
    }

    public void run() {
        ...
    }

    public void release() {
        ...
    }

    public class ExampleSecurityContext extends SecurityContext {

        public void setupSecurityContext(CallbackHandler handler, Subject executionSubject, Subject serviceSubject) {
            try {
                List<jakarta.security.auth.callback.Callback> cbs = new ArrayList<>();
                cbs.add(new CallerPrincipalCallback(executionSubject, new SimplePrincipal(username)));
                cbs.add(new GroupPrincipalCallback(executionSubject, new String[]{role}));
                handler.handle(cbs.toArray(new jakarta.security.auth.callback.Callback[cbs.size()]));
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    }
Copy to Clipboard Toggle word wrap

在上例中,ExampleWork 实现 WorkContextProvider 接口来提供 ExampleSecurityContext。该上下文将创建提供在工作执行时由 Elytron 验证的安全信息所需的回调。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat