7.4. 迁移应用程序客户端


7.4.1. 将命名客户端配置迁移到 Elytron

本节论述了如何使用 org.jboss.naming.remote.client.InitialContext 类迁移执行远程 JNDI 查找的客户端应用,该类由 org.jboss.naming.remote.client.InitialContextFactory 类支持。

以下示例假定 InitialContextFactory 类是通过指定用户凭据的属性及其连接的命名提供商的 URL 来创建的。

示例:之前版本中使用的 InitialContext Code

Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
properties.put(Context.PROVIDER_URL,"http-remoting://127.0.0.1:8080");
properties.put(Context.SECURITY_PRINCIPAL, "bob");
properties.put(Context.SECURITY_CREDENTIALS, "secret");
InitialContext context = new InitialContext(properties);
Bar bar = (Bar) context.lookup("foo/bar");
...
Copy to Clipboard Toggle word wrap

您可以从以下迁移方法之一进行选择:

7.4.1.1. 使用配置文件方法迁移命名客户端

按照以下步骤,使用配置方法将您的命名客户端迁移到 Elytron。

  1. 在客户端应用 META -INF/ 目录中创建 wildfly- config.xml 文件。该文件应包含要在建立与命名提供商的连接时使用的用户凭据。

    示例: wildfly-config.xml 文件

    <configuration>
      <authentication-client xmlns="urn:elytron:client:1.2">
        <authentication-rules>
          <rule use-configuration="namingConfig">
            <match-host name="127.0.0.1"/>
          </rule>
        </authentication-rules>
        <authentication-configurations>
          <configuration name="namingConfig">
            <set-user-name name="bob"/>
            <credentials>
              <clear-password password="secret"/>
            </credentials>
          </configuration>
        </authentication-configurations>
      </authentication-client>
    </configuration>
    Copy to Clipboard Toggle word wrap

  2. 如以下示例中所示,创建一个 InitialContext。请注意,InitialContextorg.wildfly.naming.client.WildFlyInitialContextFactory 类支持。

    示例: InitialContext Code

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.wildfly.naming.client.WildFlyInitialContextFactory");
    properties.put(Context.PROVIDER_URL,"remote+http://127.0.0.1:8080");
    InitialContext context = new InitialContext(properties);
    Bar bar = (Bar) context.lookup("foo/bar");
    ...
    Copy to Clipboard Toggle word wrap

7.4.1.2. 使用编程方法迁移命名客户端

使用此方法时,您可以提供用于直接在应用程序代码中建立与命名供应商的连接的用户凭据。

示例:使用编程方法代码

// Create the authentication configuration
AuthenticationConfiguration namingConfig = AuthenticationConfiguration.empty().useName("bob").usePassword("secret");

// Create the authentication context
AuthenticationContext context = AuthenticationContext.empty().with(MatchRule.ALL.matchHost("127.0.0.1"), namingConfig);

// Create a callable that creates and uses an InitialContext
Callable<Void> callable = () -> {
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.wildfly.naming.client.WildFlyInitialContextFactory");
    properties.put(Context.PROVIDER_URL,"remote+http://127.0.0.1:8080");
    InitialContext context = new InitialContext(properties);
    Bar bar = (Bar) context.lookup("foo/bar");
    ...
    return null;
};

// Use the authentication context to run the callable
context.runCallable(callable);
Copy to Clipboard Toggle word wrap

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat