java与es8实战之五:SpringBoot应用中操作es8(带安全检查:https、账号密码 ...

打印 上一主题 下一主题

主题 928|帖子 928|积分 2784

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
本篇概览


  • 本篇是《java与es8实战》系列的第五篇,总体目标明确:实战在SpringBoot应用中操作elasticsearch8,今天的重点是SpringBoot应用连接带有安全检查的elasticsearch8服务端
  • 连接需要安全检查的elasticsearch8是为了更接近真实环境,首先,连接是基于自签证书的https协议,其次,认证方式有两种

  • 第一种是账号密码
  • 第二种是es服务端授权的API Key



  • 今天的实战总体目标可以拆解为两个子任务

  • 在SpringBoot中连接elasticsearch8
  • 在SpringBoot中使用elasticsearch8官方的Java API Client


  • 接下来直接开始
部署elasticsearch集群(需要安全检查)

创建API Key


  • 除了账号密码,ES还提供了一种安全的访问方式:API Key,java应用持有es签发的API Key也能顺利发送指令到es,接下来咱们先生成API Key,再在应用中使用此API Key
  • 《docker-compose快速部署elasticsearch-8.x集群+kibana》一文中,的咱们将自签证书从容器中复制出来了,现在在证书所在目录执行以下命令,注意参数expiration代表这个ApiKey的有效期,我这里随意设置为10天
  1. curl -X POST "https://localhost:9200/_security/api_key?pretty" \
  2. --cacert es01.crt \
  3. -u elastic:123456 \
  4. -H 'Content-Type: application/json' \
  5. -d'
  6. {
  7.   "name": "my-api-key-10d",
  8.   "expiration": "10d"
  9. }
  10. '
复制代码

  • 会收到以下响应,其中的encoded字段就是API Key
  1. {
  2.   "id" : "eUV1V4EBucGIxpberGuJ",
  3.   "name" : "my-api-key-10d",
  4.   "expiration" : 1655893738633,
  5.   "api_key" : "YyhSTh9ETz2LKBk3-Iy2ew",
  6.   "encoded" : "ZVVWMVY0RUJ1Y0dJeHBiZXJHdUo6WXloU1RoOUVUejJMS0JrMy1JeTJldw=="
  7. }
复制代码
Java应用连接elasticsearch的核心套路


  • 不论是直连,还是带安全检查的连接,亦或是与SpringBoot的集成使之更方便易用,都紧紧围绕着一个不变的核心套路,该套路由两部分组成,掌握了它们就能在各种条件下成功连接es

  • 首先,是builder pattern,连接es有关的代码,各种对象都是其builder对象的build方法创建的,建议您提前阅读《java与es8实战之一》一文,看完后,满屏的builder代码可以从丑变成美...
  • 其次,就是java应用能向es发请求的关键:ElasticsearchClient对象,该对象的创建是有套路的,如下图,先创建RestClient,再基于RestClient创建ElasticsearchTransport,最后基于ElasticsearchTransport创建ElasticsearchClient,这是个固定的套路,咱们后面的操作都是基于此的,可能会加一点东西,但不会改变流程和图中的对象



  • 准备完毕,开始写代码
新建子工程


  • 为了便于管理依赖库版本和源码,《java与es8实战》系列的所有代码都以子工程的形式存放在父工程elasticsearch-tutorials
  • 《java与es8实战之二:实战前的准备工作》一文说明了创建父工程的详细过程
  • 在父工程elasticsearch-tutorials中新建名为crud-with-security的子工程,其pom.xml内容如下
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.    
  5.     <parent>
  6.         <artifactId>elasticsearch-tutorials</artifactId>
  7.         <groupId>com.bolingcavalry</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.         <relativePath>../pom.xml</relativePath>
  10.     </parent>
  11.     <modelVersion>4.0.0</modelVersion>
  12.     <groupId>com.bolingcavalry</groupId>
  13.    
  14.     <artifactId>crud-with-security</artifactId>
  15.     <packaging>jar</packaging>
  16.    
  17.     <name>crud-with-security</name>
  18.     <version>1.0-SNAPSHOT</version>
  19.     <url>https://github.com/zq2599</url>
  20.    
  21.     <dependencyManagement>
  22.         <dependencies>
  23.             <dependency>
  24.                 <groupId>org.springframework.boot</groupId>
  25.                 <artifactId>spring-boot-dependencies</artifactId>
  26.                 <version>${springboot.version}</version>
  27.                 <type>pom</type>
  28.                 <scope>import</scope>
  29.             </dependency>
  30.         </dependencies>
  31.     </dependencyManagement>
  32.     <dependencies>
  33.         <dependency>
  34.             <groupId>org.springframework.boot</groupId>
  35.             <artifactId>spring-boot-starter-actuator</artifactId>
  36.         </dependency>
  37.         
  38.         <dependency>
  39.             <groupId>org.springframework.boot</groupId>
  40.             <artifactId>spring-boot-configuration-processor</artifactId>
  41.             <optional>true</optional>
  42.         </dependency>
  43.         <dependency>
  44.             <groupId>org.projectlombok</groupId>
  45.             <artifactId>lombok</artifactId>
  46.         </dependency>
  47.         <dependency>
  48.             <groupId>org.springframework.boot</groupId>
  49.             <artifactId>spring-boot-starter-web</artifactId>
  50.         </dependency>
  51.         <dependency>
  52.             <groupId>org.springframework.boot</groupId>
  53.             <artifactId>spring-boot-starter-test</artifactId>
  54.             <scope>test</scope>
  55.             
  56.             <exclusions>
  57.                 <exclusion>
  58.                     <groupId>junit</groupId>
  59.                     <artifactId>junit</artifactId>
  60.                 </exclusion>
  61.             </exclusions>
  62.         </dependency>
  63.         
  64.         <dependency>
  65.             <groupId>org.junit.jupiter</groupId>
  66.             <artifactId>junit-jupiter-api</artifactId>
  67.             <scope>test</scope>
  68.         </dependency>
  69.         <dependency>
  70.             <groupId>org.junit.jupiter</groupId>
  71.             <artifactId>junit-jupiter-engine</artifactId>
  72.             <scope>test</scope>
  73.         </dependency>
  74.         
  75.         <dependency>
  76.             <groupId>co.elastic.clients</groupId>
  77.             <artifactId>elasticsearch-java</artifactId>
  78.         </dependency>
  79.         <dependency>
  80.             <groupId>com.fasterxml.jackson.core</groupId>
  81.             <artifactId>jackson-databind</artifactId>
  82.         </dependency>
  83.         
  84.         <dependency>
  85.             <groupId>jakarta.json</groupId>
  86.             <artifactId>jakarta.json-api</artifactId>
  87.         </dependency>
  88.         <dependency>
  89.             <groupId>org.springframework.boot</groupId>
  90.             <artifactId>spring-boot-starter-web</artifactId>
  91.         </dependency>
  92.     </dependencies>
  93.     <build>
  94.         <plugins>
  95.             
  96.             <plugin>
  97.                 <groupId>org.apache.maven.plugins</groupId>
  98.                 <artifactId>maven-surefire-plugin</artifactId>
  99.                 <version>3.0.0-M4</version>
  100.                 <configuration>
  101.                     <skipTests>false</skipTests>
  102.                 </configuration>
  103.             </plugin>
  104.             <plugin>
  105.                 <groupId>org.springframework.boot</groupId>
  106.                 <artifactId>spring-boot-maven-plugin</artifactId>
  107.                 <configuration>
  108.                     <excludes>
  109.                         <exclude>
  110.                             <groupId>org.projectlombok</groupId>
  111.                             <artifactId>lombok</artifactId>
  112.                         </exclude>
  113.                     </excludes>
  114.                 </configuration>
  115.             </plugin>
  116.         </plugins>
  117.         <resources>
  118.             <resource>
  119.                 <directory>src/main/resources</directory>
  120.                 <includes>
  121.                     <include>**/*.*</include>
  122.                 </includes>
  123.             </resource>
  124.         </resources>
  125.     </build>
  126. </project>
复制代码
配置文件


  • 为了成功连接es,需要两个配置文件:SpringBoot常规的配置application.yml和es的自签证书
  • 首先是application.yml,如下所示,因为本篇要验证两种授权方式,所以账号、密码、apiKey全部填写在配置文件中,如下所示
  1. elasticsearch:
  2.   username: elastic
  3.   passwd: 123456
  4.   apikey: ZVVWMVY0RUJ1Y0dJeHBiZXJHdUo6WXloU1RoOUVUejJMS0JrMy1JeTJldw==
  5.   # 多个IP逗号隔开
  6.   hosts: 127.0.0.1:9200
复制代码
编码:启动类


  • SpringBoot启动类,平淡无奇的那种
  1. @SpringBootApplication
  2. public class SecurityApplication {
  3.     public static void main(String[] args) {
  4.         SpringApplication.run(SecurityApplication.class, args);
  5.     }
  6. }
复制代码
编码:配置文件


  • 接下来是全文的重点:通过Config类向Spring环境注册服务bean,这里有这两处要注意的地方
  • 第一个要注意的地方:向Spring环境注册的服务bean一共有两个,它们都是ElasticsearchClient类型,一个基于账号密码认证,另一个基于apiKey认证
  • 第二个要注意的地方:SpringBoot向es服务端发起的是https请求,这就要求在建立连接的时候使用正确的证书,也就是刚才咱们从容器中复制出来再放入application.yml所在目录的es01.crt文件,使用证书的操作发生在创建ElasticsearchTransport对象的时候,属于前面总结的套路步骤中的一步,如下图红框所示


  • 配置类的详细代码如下,有几处需要注意的地方稍后会说明
  1. package com.bolingcavalry.security.config;
  2. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  3. import co.elastic.clients.json.jackson.JacksonJsonpMapper;
  4. import co.elastic.clients.transport.ElasticsearchTransport;
  5. import co.elastic.clients.transport.rest_client.RestClientTransport;
  6. import lombok.Setter;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.apache.http.Header;
  9. import org.apache.http.HttpHost;
  10. import org.apache.http.auth.AuthScope;
  11. import org.apache.http.auth.UsernamePasswordCredentials;
  12. import org.apache.http.client.CredentialsProvider;
  13. import org.apache.http.conn.ssl.NoopHostnameVerifier;
  14. import org.apache.http.impl.client.BasicCredentialsProvider;
  15. import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
  16. import org.apache.http.message.BasicHeader;
  17. import org.apache.http.ssl.SSLContextBuilder;
  18. import org.apache.http.ssl.SSLContexts;
  19. import org.elasticsearch.client.RestClient;
  20. import org.elasticsearch.client.RestClientBuilder;
  21. import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
  22. import org.springframework.boot.context.properties.ConfigurationProperties;
  23. import org.springframework.context.annotation.Bean;
  24. import org.springframework.context.annotation.Configuration;
  25. import org.springframework.core.io.ClassPathResource;
  26. import org.springframework.util.StringUtils;
  27. import javax.net.ssl.SSLContext;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.nio.file.Files;
  31. import java.nio.file.Path;
  32. import java.nio.file.Paths;
  33. import java.security.KeyManagementException;
  34. import java.security.KeyStore;
  35. import java.security.KeyStoreException;
  36. import java.security.NoSuchAlgorithmException;
  37. import java.security.cert.Certificate;
  38. import java.security.cert.CertificateException;
  39. import java.security.cert.CertificateFactory;
  40. @ConfigurationProperties(prefix = "elasticsearch") //配置的前缀
  41. @Configuration
  42. @Slf4j
  43. public class ClientConfig {
  44.     @Setter
  45.     private String hosts;
  46.     @Setter
  47.     private String username;
  48.     @Setter
  49.     private String passwd;
  50.     @Setter
  51.     private String apikey;
  52.     /**
  53.      * 解析配置的字符串,转为HttpHost对象数组
  54.      * @return
  55.      */
  56.     private HttpHost[] toHttpHost() {
  57.         if (!StringUtils.hasLength(hosts)) {
  58.             throw new RuntimeException("invalid elasticsearch configuration");
  59.         }
  60.         String[] hostArray = hosts.split(",");
  61.         HttpHost[] httpHosts = new HttpHost[hostArray.length];
  62.         HttpHost httpHost;
  63.         for (int i = 0; i < hostArray.length; i++) {
  64.             String[] strings = hostArray[i].split(":");
  65.             httpHost = new HttpHost(strings[0], Integer.parseInt(strings[1]), "https");
  66.             httpHosts[i] = httpHost;
  67.         }
  68.         return httpHosts;
  69.     }
  70.     @Bean
  71.     public ElasticsearchClient clientByPasswd() throws Exception {
  72.         ElasticsearchTransport transport = getElasticsearchTransport(username, passwd, toHttpHost());
  73.         return new ElasticsearchClient(transport);
  74.     }
  75.     private static SSLContext buildSSLContext() {
  76.         ClassPathResource resource = new ClassPathResource("es01.crt");
  77.         SSLContext sslContext = null;
  78.         try {
  79.             CertificateFactory factory = CertificateFactory.getInstance("X.509");
  80.             Certificate trustedCa;
  81.             try (InputStream is = resource.getInputStream()) {
  82.                 trustedCa = factory.generateCertificate(is);
  83.             }
  84.             KeyStore trustStore = KeyStore.getInstance("pkcs12");
  85.             trustStore.load(null, null);
  86.             trustStore.setCertificateEntry("ca", trustedCa);
  87.             SSLContextBuilder sslContextBuilder = SSLContexts.custom()
  88.                     .loadTrustMaterial(trustStore, null);
  89.             sslContext = sslContextBuilder.build();
  90.         } catch (CertificateException | IOException | KeyStoreException | NoSuchAlgorithmException |
  91.                  KeyManagementException e) {
  92.             log.error("ES连接认证失败", e);
  93.         }
  94.         return sslContext;
  95.     }
  96.     private static ElasticsearchTransport getElasticsearchTransport(String username, String passwd, HttpHost...hosts) {
  97.         // 账号密码的配置
  98.         final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  99.         credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, passwd));
  100.         // 自签证书的设置,并且还包含了账号密码
  101.         HttpClientConfigCallback callback = httpAsyncClientBuilder -> httpAsyncClientBuilder
  102.                 .setSSLContext(buildSSLContext())
  103.                 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
  104.                 .setDefaultCredentialsProvider(credentialsProvider);
  105.         // 用builder创建RestClient对象
  106.         RestClient client = RestClient
  107.                            .builder(hosts)
  108.                            .setHttpClientConfigCallback(callback)
  109.                            .build();
  110.         return new RestClientTransport(client, new JacksonJsonpMapper());
  111.     }
  112.     private static ElasticsearchTransport getElasticsearchTransport(String apiKey, HttpHost...hosts) {
  113.         // 将ApiKey放入header中
  114.         Header[] headers = new Header[] {new BasicHeader("Authorization", "ApiKey " + apiKey)};
  115.         // es自签证书的设置
  116.         HttpClientConfigCallback callback = httpAsyncClientBuilder -> httpAsyncClientBuilder
  117.                 .setSSLContext(buildSSLContext())
  118.                 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
  119.         // 用builder创建RestClient对象
  120.         RestClient client = RestClient
  121.                            .builder(hosts)
  122.                            .setHttpClientConfigCallback(callback)
  123.                            .setDefaultHeaders(headers)
  124.                            .build();
  125.         return new RestClientTransport(client, new JacksonJsonpMapper());
  126.     }
  127.     @Bean
  128.     public ElasticsearchClient clientByApiKey() throws Exception {
  129.         ElasticsearchTransport transport = getElasticsearchTransport(apikey, toHttpHost());
  130.         return new ElasticsearchClient(transport);
  131.     }
  132. }
复制代码

  • 上述代码有以下几处需要注意

  • 这个配置类为业务代码提供了两个服务bean,作用是操作es,这两个服务bean分别由clientByPasswdclientByApiKey两个方法负责提供
  • 名为getElasticsearchTransport的方法有两个,分别负责配置两种鉴权方式:账号密码和apiKey
  • 设置证书的操作被封装在buildSSLContext方法中,在创建ElasticsearchTransport对象的时候会用到
编码:业务类


  • 既然两个ElasticsearchClient对象都已经注册到Spring环境,那么只要在业务类中注入就能用来操作es了
  • 新建业务类ESService.java,如下,可见通过Resource注解选择了账号密码鉴权的ElasticsearchClient对象
  1. package com.bolingcavalry.security.service;
  2. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import javax.annotation.Resource;
  6. import java.io.IOException;
  7. @Service
  8. public class ESService {
  9.     @Resource(name="clientByPasswd")
  10.     private ElasticsearchClient elasticsearchClient;
  11.     public void addIndex(String name) throws IOException {
  12.         elasticsearchClient.indices().create(c -> c.index(name));
  13.     }
  14.     public boolean indexExists(String name) throws IOException {
  15.         return elasticsearchClient.indices().exists(b -> b.index(name)).value();
  16.     }
  17.     public void delIndex(String name) throws IOException {
  18.         elasticsearchClient.indices().delete(c -> c.index(name));
  19.     }
  20. }
复制代码

  • 至此,基本功能算是开发完成了,接下来编写单元测试代码,验证能否成功操作es8
编码:单元测试


  • 新增单元测试类ESServiceTest.java,如下,功能是调用业务类ESService执行创建、删除、查找等索引操作
  1. package com.bolingcavalry.security.service;
  2. import org.junit.jupiter.api.Assertions;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. @SpringBootTest
  7. class ESServiceTest {
  8.     @Autowired
  9.     ESService esService;
  10.     @Test
  11.     void addIndex() throws Exception {
  12.         String indexName = "test_index";
  13.         Assertions.assertFalse(esService.indexExists(indexName));
  14.         esService.addIndex(indexName);
  15.         Assertions.assertTrue(esService.indexExists(indexName));
  16.         esService.delIndex(indexName);
  17.         Assertions.assertFalse(esService.indexExists(indexName));
  18.     }
  19. }
复制代码

  • 编码完成,开始验证
验证:账号密码鉴权


  • 现在ESService中使用的es服务类是账号密码鉴权的,运行单元测试,看看是否可以成功操作ES,如下图,符合预期

验证:ApiKey鉴权


  • 再来试试ApiKey鉴权操作es,修改ESService.java源码,改动如下图红框所示

  • 为了检查创建的索引是否符合预期,注释掉单元测试类中删除索引的代码,如下图,如此一来,单元测试执行完成后,新增的索引还保留在es环境中

  • 再执行一次单元测试,依旧符合预期

  • 用eshead查看,可见索引创建成功

  • 至此,SpringBoot操作带有安全检查的elasticsearch8的实战就完成了,在SpringData提供elasticsearch8操作的库之前,基于es官方原生client库的操作是常见的elasticsearch8访问方式,希望本文能给您一些参考
源码下载

名称链接备注项目主页https://github.com/zq2599/blog_demos该项目在GitHub上的主页git仓库地址(https)https://github.com/zq2599/blog_demos.git该项目源码的仓库地址,https协议git仓库地址(ssh)git@github.com:zq2599/blog_demos.git该项目源码的仓库地址,ssh协议

  • 这个git项目中有多个文件夹,本次实战的源码在elasticsearch-tutorials文件夹下,如下图红框

  • elasticsearch-tutorials是个父工程,里面有多个module,本篇实战的module是crud-with-security,如下图红框

欢迎关注博客园:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

郭卫东

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表