4.2. 创建加载默认 SSL 上下文的客户端示例
以下示例演示了以编程方式注册 WildFlyElytronClientDefaultSSLContextProvider
提供程序,并使用 SSLContext.getDefault ()
方法获取 Elytron 客户端初始化的 SSLContext。该示例使用静态客户端配置作为提供程序的参数提供。
4.2.1. 为 JBoss EAP 客户端创建 Maven 项目
要为部署到 JBoss EAP 的应用创建客户端,请创建一个具有所需依赖项和目录结构的 Maven 项目。
先决条件
- 您已安装了 Maven。如需更多信息,请参阅 下载 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
的客户端
为部署到 JBoss EAP 的应用创建一个客户端,以使用
方法加载 SSLContext。
SSLContext
.getDefault ()
在此过程中,<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); } }
在 <
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}
验证
- 进入 < application_home> 目录。
运行应用程序。
$ mvn compile exec:java
输出示例
INFO: ELY00001: WildFly Elytron version 2.0.0.Final-redhat-00001 SSL Default SSLContext is: WildFlyElytronClientDefaultSSLContextProvider