4.2. デフォルトの SSL コンテキストをロードするクライアントの作成例
以下の例は、WildFlyElytronClientDefaultSSLContextProvider プロバイダーをプログラムで登録し、SSLContext.getDefault() メソッドを使用して Elytron クライアントによって初期化された SSLContext を取得する方法を示しています。この例では、プロバイダーへの引数として提供された静的クライアント設定を使用します。
4.2.1. JBoss EAP クライアントの Maven プロジェクトの作成 リンクのコピーリンクがクリップボードにコピーされました!
JBoss EAP にデプロイされたアプリケーションのクライアントを作成するには、必要な依存関係とディレクトリー構造で Maven プロジェクトを作成します。
前提条件
- Maven がインストールされている。詳細は、Downloading Apache Maven を参照してください。
手順
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アプリケーションのルートディレクトリーに移動します。
$ cd client-ssl-context生成された
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>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 設定ファイルが含まれるディレクトリーを参照します。
前提条件
双方向 TLS で JBoss EAP にデプロイされたアプリケーションを保護している。
これを実行するには、以下の手順を実行します。
Maven プロジェクトを作成している。
詳細は、JBoss EAP クライアントの Maven プロジェクトの作成 を参照してください。
- JBoss EAP が実行している。
手順
Java ファイルを保存するディレクトリーを作成します。
$ mkdir -p <application_home>/src/main/java/com/example/client新しいディレクトリーに移動します。
$ cd <application_home>/src/main/java/com/example/client以下の内容で 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 コンテキストを取得します。
<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}
検証
- <application_home> ディレクトリーに移動します。
アプリケーションを実行します。
$ mvn compile exec:java出力例
INFO: ELY00001: WildFly Elytron version 2.0.0.Final-redhat-00001 SSL Default SSLContext is: WildFlyElytronClientDefaultSSLContextProvider