Java Low Level REST Client 官网说明-加密文档

本文档详细介绍了如何为访问使用TLS的Elasticsearch实例配置客户端。提供了使用p12和pem证书的信任store设置,以及在需要客户端TLS认证时,如何从p12 keystore加载证书和私钥。同时提到了在安全管理器环境下,可能需要调整JVM的主机名缓存策略。并给出了ApacheHttpAsyncClient的进一步配置参考。

加密文档

加密文档使用的TLS能通过HttpClientConfigCallback被配置,org.apache.http.impl.nio.client.HttpAsyncClientBuilder被作为一个参数接收。有很多方法去配置加密文档比如 按照最不重要的排序 方法有setSSLContext, setSSLSessionStrategy and setConnectionManager

当访问一个使用基于TLS的http组件,客户端需要去信任Elasticsearch使用的证书,下面的例子 就是一个设置client去信任Elasticsearch使用的一个被标注的认证的例子,当认证是p12证书的时候:

Path trustStorePath = Paths.get("/path/to/truststore.p12");

KeyStore truststore = KeyStore.getInstance("pkcs12");

try (InputStream is = Files.newInputStream(trustStorePath)) {

truststore.load(is, keyStorePass.toCharArray());

}

SSLContextBuilder sslBuilder = SSLContexts.custom()

.loadTrustMaterial(truststore, null);

final SSLContext sslContext = sslBuilder.build();

RestClientBuilder builder = RestClient.builder(

new HttpHost("localhost", 9200, "https"))

.setHttpClientConfigCallback(new HttpClientConfigCallback() {

@Override

public HttpAsyncClientBuilder customizeHttpClient(

HttpAsyncClientBuilder httpClientBuilder) {

return httpClientBuilder.setSSLContext(sslContext);

}

});

下面的例子 就是一个设置client去信任Elasticsearch使用的一个被标注的认证的例子,当认证是pem证书的时候:

Path caCertificatePath = Paths.get("/path/to/ca.crt");

CertificateFactory factory =

CertificateFactory.getInstance("X.509");

Certificate trustedCa;

try (InputStream is = Files.newInputStream(caCertificatePath)) {

trustedCa = factory.generateCertificate(is);

}

KeyStore trustStore = KeyStore.getInstance("pkcs12");

trustStore.load(null, null);

trustStore.setCertificateEntry("ca", trustedCa);

SSLContextBuilder sslContextBuilder = SSLContexts.custom()

.loadTrustMaterial(trustStore, null);

final SSLContext sslContext = sslContextBuilder.build();

RestClient.builder(

new HttpHost("localhost", 9200, "https"))

.setHttpClientConfigCallback(new HttpClientConfigCallback() {

@Override

public HttpAsyncClientBuilder customizeHttpClient(

HttpAsyncClientBuilder httpClientBuilder) {

return httpClientBuilder.setSSLContext(sslContext);

}

});

When Elasticsearch is configured to require client TLS authentication, for example when a PKI realm is configured, the client needs to provide a client certificate during the TLS handshake in order to authenticate. The following is an example of setting up the client for TLS authentication with a certificate and a private key that are stored in a PKCS#12 keystore.

当Elasticsearch 被配置要求TLS认证,比如当一个PKI域被配置,client需要去提供一个client证书在为了认证进行的TLS握手期间,下面是个例子,就是一个设置client为了TLS认证,使用一个证书和私有key存储在p12证书中:

Path trustStorePath = Paths.get("/path/to/your/truststore.p12");

Path keyStorePath = Paths.get("/path/to/your/keystore.p12");

KeyStore trustStore = KeyStore.getInstance("pkcs12");

KeyStore keyStore = KeyStore.getInstance("pkcs12");

try (InputStream is = Files.newInputStream(trustStorePath)) {

trustStore.load(is, trustStorePass.toCharArray());

}

try (InputStream is = Files.newInputStream(keyStorePath)) {

keyStore.load(is, keyStorePass.toCharArray());

}

SSLContextBuilder sslBuilder = SSLContexts.custom()

.loadTrustMaterial(trustStore, null)

.loadKeyMaterial(keyStore, keyStorePass.toCharArray());

final SSLContext sslContext = sslBuilder.build();

RestClientBuilder builder = RestClient.builder(

new HttpHost("localhost", 9200, "https"))

.setHttpClientConfigCallback(new HttpClientConfigCallback() {

@Override

public HttpAsyncClientBuilder customizeHttpClient(

HttpAsyncClientBuilder httpClientBuilder) {

return httpClientBuilder.setSSLContext(sslContext);

}

});

如果client 证书和key在证书中不合适而用的是PEM编码文件,你不能直接使用他们去构建一个SSLcontent,你必须依赖额外的libraries去转化PEM到一个私钥实例,或者你能拓展同居去从PEM中构建keystore,就像下面的例子

openssl pkcs12 -export -in client.crt -inkey private_key.pem \

-name "client" -out client.p12

If no explicit configuration is provided, the system default configuration will be used.

如果没有明确的配置被提供,那么将会使用系统默认配置

其他

对于任何需要其他需要的配置,可以访问Apache HttpAsyncClient 文档https://hc.apache.org/httpcomponents-asyncclient-4.1.x/

如果你的应用在安全管理器下运行,你可能受到jvm默认的缓存positive hostname不过期解决方案和negative hostname 十秒钟方案的影响。如果你使用client连接的host被解析的地址随时间变化

然后你可能想去修改磨人的jvm操作,那些能被通过增加networkaddress.cache.ttl=<timeout> and networkaddress.cache.negative.ttl=<timeout>到你的java安全策略来解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值