4.2.2. 编程方法
编程方法在客户端代码中配置所有 Elytron 客户端配置:
//create your authentication configuration
AuthenticationConfiguration adminConfig =
AuthenticationConfiguration.empty()
.useProviders(() -> new Provider[] { new WildFlyElytronProvider() })
.setSaslMechanismSelector(SaslMechanismSelector.NONE.addMechanism("DIGEST-MD5"))
.useRealm("ManagementRealm")
.useName("administrator")
.usePassword("password1!");
//create your authentication context
AuthenticationContext context = AuthenticationContext.empty();
context = context.with(MatchRule.ALL.matchHost("127.0.0.1"), adminConfig);
//create your runnable for establishing a connection
Runnable runnable =
new Runnable() {
public void run() {
try {
//Establish your connection and do some work
} catch (Exception e) {
e.printStackTrace();
}
}
};
//use your authentication context to run your client
context.run(runnable);
在 AuthenticationConfiguration 和 Authentication Context 中添加配置详情时,每个方法调用都会返回该对象的新实例。例如,如果您在使用不同主机名连接时需要单独的配置,您可以执行以下操作:
//create your authentication configuration
AuthenticationConfiguration commonConfig =
AuthenticationConfiguration.empty()
.useProviders(() -> new Provider[] { new WildFlyElytronProvider() })
.setSaslMechanismSelector(SaslMechanismSelector.NONE.addMechanism("DIGEST-MD5"))
.useRealm("ManagementRealm");
AuthenticationConfiguration administrator =
commonConfig
.useName("administrator")
.usePassword("password1!");
AuthenticationConfiguration monitor =
commonConfig
.useName("monitor")
.usePassword("password1!");
//create your authentication context
AuthenticationContext context = AuthenticationContext.empty();
context = context.with(MatchRule.ALL.matchHost("127.0.0.1"), administrator);
context = context.with(MatchRule.ALL.matchHost("localhost"), monitor);表 4.2. 通用规则
| 规则 | 描述 |
|---|---|
| matchLocalSecurityDomain(String name) |
这与 配置文件方法中的 |
| matchNoUser() |
这与 配置文件方法 |
| matchPath(String pathSpec) |
这与 配置文件方法中的 |
| matchPort(int port) |
这与 配置文件方法中的 |
| matchProtocol(String protoName) |
这与 配置文件方法中的 |
| matchPurpose(String 目的) | 创建一个新规则,它与此规则相同,但也与给定目的名称匹配。 |
| matchUrnName(String name) |
这与 配置文件方法中的 |
| matchUser(String userSpec) |
这与 配置文件方法 |
另外,您可以使用 captureCurrent() 从当前配置的配置开始,而不是从空身份验证配置开始。
//create your authentication configuration AuthenticationConfiguration commonConfig = AuthenticationConfiguration.captureCurrent();
使用 captureCurrent() 将捕获任何之前建立的身份验证上下文,并将其用作您的新基础配置。通过 调用 run() 激活验证上下文后,就会建立验证上下文。如果名为 captureCurrent(),并且当前没有活跃上下文,它将尝试使用默认验证(如果可用)。您可以在以下部分找到有关此问题的更多详细信息:
AuthenticationConfiguration.empty() 应当仅用作在 上构建配置的基础,不应自行使用。它提供了一个配置,它使用 JVM 范围注册的提供程序并启用匿名身份验证。
在 AuthenticationConfiguration.empty() 配置上指定提供程序时,您可以指定自定义列表,但大多数用户应使用 WildFlyElytronProvider() 提供程序。
在创建身份验证上下文时,使用 context.with(…) 将创建一个新上下文,该上下文会将当前上下文中的规则和身份验证配置与所提供的规则和身份验证配置合并。提供的规则和身份验证配置将显示在当前上下文中的后面。