4.2. デフォルトの SSL コンテキストをロードするクライアントの作成例


以下の例は、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 をロードするクライアントの作成

SSLContext.getDefault() メソッドを使用して SSLContext をロードする JBoss EAP にデプロイされたアプリケーションのクライアントを作成します。

この手順では、<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>

    以下のプレースホルダーの値を、実際のパスに置き換えます。

    • ${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

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2026 Red Hat
トップに戻る