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)
可以在 resource-adapters 子系统中将 elytron-enabled 属性设置为 true,使用管理 CLI 配置 elytron-enabled 属性。默认情况下,此属性设置为 false。
authentication-context 属性定义将用于执行登录的 Elytron 身份验证上下文的名称。
Elytron authentication-context 属性可以包含一个或多个 authentication-configuration 元素,其中包含您要使用的凭证。
如果没有设置 属性,JBoss EAP 将使用当前的 authentication-context authentication-context,这是打开连接的调用器代码使用的 authentication-context。
示例:创建 authentication-configuration
/subsystem=elytron/authentication-configuration=exampleAuthConfig:add(authentication-name=sa,credential-reference={clear-text=sa})
示例:使用上述配置创建 authentication-context
/subsystem=elytron/authentication-context=exampleAuthContext:add(match-rules=[{authentication-configuration=exampleAuthConfig}])
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)
-
如果使用
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>
然后,使用 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>
示例:安全域的配置
/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}])
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();
}
此界面允许 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);
}
}
}
在上例中,ExampleWork 实现 WorkContextProvider 接口来提供 ExampleSecurityContext。该上下文将创建提供在工作执行时由 Elytron 验证的安全信息所需的回调。
其他资源