搜索

4.2. 创建加载默认 SSL 上下文的客户端示例

download PDF

以下示例演示了以编程方式注册 WildFlyElytronClientDefaultSSLContextProvider 提供程序,并使用 SSLContext.getDefault () 方法获取 Elytron 客户端初始化的 SSLContext。该示例使用静态客户端配置作为提供程序的参数提供。

4.2.1. 为 JBoss EAP 客户端创建 Maven 项目

要为部署到 JBoss EAP 的应用创建客户端,请创建一个具有所需依赖项和目录结构的 Maven 项目。

先决条件

流程

  1. 使用 mvn 命令设置 Maven 项目。该命令创建项目的目录结构以及 pom.xml 配置文件。

    $ mvn archetype:generate \
    -DgroupId=com.example.client \
    -DartifactId=client-ssl-context \
    -DarchetypeGroupId=org.apache.maven.archetypes \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DinteractiveMode=false
  2. 导航到应用程序根目录。

    $ cd client-ssl-context
  3. 将生成的 pom.xml 文件的内容替换为以下文本:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.example.client</groupId>
      <artifactId>client-ssl-context</artifactId>
      <version>1.0-SNAPSHOT</version>
    
      <name>client-ssl-context</name>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
      </properties>
    
      <repositories>
        <repository>
          <id>jboss-public-maven-repository</id>
          <name>JBoss Public Maven Repository</name>
          <url>https://repository.jboss.org/nexus/content/groups/public/</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
          </snapshots>
          <layout>default</layout>
        </repository>
        <repository>
          <id>redhat-ga-maven-repository</id>
          <name>Red Hat GA Maven Repository</name>
          <url>https://maven.repository.redhat.com/ga/</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
          </snapshots>
          <layout>default</layout>
        </repository>
      </repositories>
    
      <dependencies>
        <dependency>                                         1
          <groupId>org.wildfly.security</groupId>
          <artifactId>wildfly-elytron-client</artifactId>
          <version>2.0.0.Final-redhat-00001</version>
        </dependency>
        <dependency>                                         2
          <groupId>org.wildfly.client</groupId>
          <artifactId>wildfly-client-config</artifactId>
          <version>1.0.1.Final-redhat-00001</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.4.0</version>
            <configuration>
              <mainClass>com.example.client.App</mainClass>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    1
    wildfly-elytron-client 的依赖项。
    2
    wildfly-client-config 的依赖项。
  4. 删除 src/test 目录。

    $ rm -rf src/test/

验证

  • 在应用程序根目录中,输入以下命令:

    $ mvn install

    您会看到类似如下的输出:

    ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  1.682 s
    [INFO] Finished at: 2023-10-31T01:32:17+05:30
    [INFO] ------------------------------------------------------------------------

4.2.2. 创建载入默认 SSLContext的客户端

为部署到 JBoss EAP 的应用创建一个客户端,以使用 SSLContext.getDefault () 方法加载 SSLContext。

在此过程中,<application_home> 指向包含应用程序 pom.xml 配置文件的目录。

先决条件

流程

  1. 创建一个用于存储 Java 文件的目录。

    $ mkdir -p <application_home>/src/main/java/com/example/client
  2. 前往新目录。

    $ cd <application_home>/src/main/java/com/example/client
  3. 使用以下内容创建 Java 文件 App.java

    package com.example.client;
    
    import java.io.IOException;
    import java.net.URI;
    import java.net.http.HttpClient;
    import java.net.http.HttpRequest;
    import java.net.http.HttpResponse;
    import java.net.http.HttpResponse.BodyHandlers;
    import java.security.NoSuchAlgorithmException;
    import java.security.Security;
    import java.util.Properties;
    import javax.net.ssl.SSLContext;
    import org.wildfly.security.auth.client.WildFlyElytronClientDefaultSSLContextProvider;
    
    public class App  {
    
      public static void main( String[] args )  {
      String url = "https://localhost:8443/";                                                                                   1
      try {
        Security.insertProviderAt(new WildFlyElytronClientDefaultSSLContextProvider("src/wildfly-config-two-way-tls.xml"), 1);  2
        HttpClient httpClient = HttpClient.newBuilder().sslContext(SSLContext.getDefault()).build();
        HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(url))
                    .GET()
                    .build();
        HttpResponse<Void> httpRresponse = httpClient.send(request, BodyHandlers.discarding());
        String sslContext = SSLContext.getDefault().getProvider().getName();                                                    3
        System.out.println ("\nSSL Default SSLContext is: " + sslContext);
    
      } catch (NoSuchAlgorithmException | IOException | InterruptedException e) {
          e.printStackTrace();
        }
    
      System.exit(0);
      }
    }
    1
    定义 JBoss EAP 主页 URL。
    2
    注册安全供应商。1 定义此提供程序的优先级.要静态注册提供程序,您可以在 java.security 文件中添加提供程序:security.provider .1=org.wildfly.security.auth.client.WildFlyElytronClientDefaultSSLContextProvider < PATH> / <TO>/wildfly-config-two-way-tls.xml
    3
    获取默认的 SSL 上下文。
  4. 在 < application_home> /src 目录中创建名为 "wildfly-config-two-way-tls.xml" 的客户端配置文件。

    <?xml version="1.0" encoding="UTF-8"?>
    
    <configuration>
        <authentication-client xmlns="urn:elytron:client:1.7">
            <key-stores>
                <key-store name="truststore" type="PKCS12">
                    <file name="${path_to_client_truststore}/client.truststore.p12"/>
                    <key-store-clear-password password="secret"/>
                </key-store>
                <key-store name="keystore" type="PKCS12">
                    <file name="${path_to_client_keystore}/exampleclient.keystore.pkcs12"/>
                    <key-store-clear-password password="secret"/>
                </key-store>
            </key-stores>
            <ssl-contexts>
                <ssl-context name="client-context">
                    <trust-store key-store-name="truststore"/>
                    <key-store-ssl-certificate key-store-name="keystore" alias="exampleclientkeystore">
                        <key-store-clear-password password="secret"/>
                    </key-store-ssl-certificate>
                </ssl-context>
            </ssl-contexts>
            <ssl-context-rules>
                <rule use-ssl-context="client-context"/>
            </ssl-context-rules>
        </authentication-client>
    </configuration>

    将 holder 值替换为实际路径:

    • ${path_to_client_truststore}
    • ${path_to_client_keystore}

验证

  1. 进入 < application_home> 目录。
  2. 运行应用程序。

    $ mvn compile exec:java

    输出示例

    INFO: ELY00001: WildFly Elytron version 2.0.0.Final-redhat-00001
    
    SSL Default SSLContext is: WildFlyElytronClientDefaultSSLContextProvider

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

© 2024 Red Hat, Inc.