java以SSL方式连ES

先做准备工作,浏览器方式访问 ES7.X url https://127.0.0.1:8027 弹出用户名和密码
输入后在浏览器得到
{
“name” : “DTCNPEMS04”,
“cluster_name” : “cnp-es-cluster”,
“cluster_uuid” : “wb0So_FqQBOKqtXnsqofTg”,
“version” : {
“number” : “7.14.1”,
“build_flavor” : “default”,
“build_type” : “docker”,
“build_hash” : “66b55ebfa59c92c15db3f69a335d500018b3331e”,
“build_date” : “2021-08-26T09:01:05.390870785Z”,
“build_snapshot” : false,
“lucene_version” : “8.9.0”,
“minimum_wire_compatibility_version” : “6.8.0”,
“minimum_index_compatibility_version” : “6.0.0-beta1”
},
“tagline” : “You Know, for Search”
}
说明浏览器可以获得SSL证书,然后用户可以按用户名/密码访问

后续思路就是从浏览器导出证书(略过),导入到JDK的security目录下,然后在代码里调用嵌入用户名和密码,SSL访问即可。

1) 假设浏览器导出证书为
es-devtest

2) 将证书转换为cer格式
openssl x509 -outform der -in es-devtest -out es-devtest.cer

3) 将证书导入到JDK的security目录下
keytool -import -alias es-devtest -keystore $HOME/java/jdk-8u291-linux-x64/jdk1.8.0_291/jre/lib/security/cacerts -file es-devtest.cer -trustcacerts -storepass changeit

4) 代码

String username="elastic";
String password="123456789";

        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

        esClient = new RestHighLevelClient(
                RestClient.builder(HttpHost.create("https://127.0.0.1:8027"))
                        .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                                .setDefaultCredentialsProvider(credentialsProvider))
        );

连接不报错,就是成功,之后可以操作索引

要连接开启了SSLElasticsearch集群,需要使用Java的TransportClient,并使用SSLContext来设置SSL连接。以下是一个简单的示例代码: ```java import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.transport.client.PreBuiltTransportClient; import javax.net.ssl.SSLContext; import java.net.InetAddress; import java.security.KeyStore; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; public class ESClient { public static void main(String[] args) throws Exception { Settings settings = Settings.builder() .put("cluster.name", "myClusterName") .put("xpack.security.user", "myUsername:myPassword") .put("xpack.security.transport.ssl.enabled", true) .put("xpack.security.transport.ssl.verification_mode", "certificate") .put("xpack.security.transport.ssl.keystore.path", "/path/to/keystore.jks") .put("xpack.security.transport.ssl.truststore.path", "/path/to/truststore.jks") .build(); SSLContext sslContext = SSLContext.getInstance("TLS"); KeyStore keyStore = KeyStore.getInstance("jks"); KeyStore trustStore = KeyStore.getInstance("jks"); keyStore.load(ESClient.class.getResourceAsStream("/path/to/keystore.jks"), "keystore_password".toCharArray()); trustStore.load(ESClient.class.getResourceAsStream("/path/to/truststore.jks"), "truststore_password".toCharArray()); TrustManager[] trustManagers = new TrustManager[] { new X509TrustManager() { public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {} public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }}; sslContext.init(null, trustManagers, new SecureRandom()); TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300)) .setSSLContext(sslContext); XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject().field("name", "John").endObject(); client.prepareIndex("myindex", "mytype", "1") .setSource(builder) .get(); client.close(); } } ``` 在这个示例代码中,我们首先使用Elasticsearch的TransportClient创建了一个连接,然后设置了连接ES集群所需的一些参数。其中,`xpack.security.user`参数是用于认证的用户名和密码,`xpack.security.transport.ssl.enabled`参数设置为`true`表示开启SSL连接,`xpack.security.transport.ssl.verification_mode`参数设置为`certificate`表示只信任CA签署的证书。最后,我们设置了keystore和truststore的路径,并使用SSLContext将其加载。 接下来,我们使用TransportClient发送了一个简单的索引请求,将一个JSON文档插入到名为`myindex`、类型为`mytype`、ID为`1`的索引中。 注意,这只是一个简单的示例,实际应用中还需要根据实际情况进行配置和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhyuli

您的鼓励是对我付出努力一种赞赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值