Springboot之整合ElasticSearch使用
- 2019 年 10 月 8 日
- 筆記
「 技術分享,不停更。除非老闆娘喊我。
引入
先說一起昨天發生的事件。
(來自infoq)據外媒報道,1月22日,美國一家網上賭場集團泄露了超過 1.08 億筆投注信息,包括客戶個人資料,存取款記錄、家庭住址、電話號碼、電子郵件地址、出生日期、網站用戶名、帳戶餘額、IP 地址、瀏覽器、操作系統信息、上次登錄信息和遊戲列表,甚至包含當前投注、獲勝、用於交易的銀行卡等詳細信息。
該服務器被安全研究員 Justin Paine 發現,數據泄露源頭竟然是 ElasticSearch 服務器,該服務器沒有密碼保護,不需要身份驗證且很明顯信息來源於在線投注門戶網站。
近年關於ElasticSearch 的安全事故頻發
ElasticSearch 是一個搜索引擎,企業一般用它來改進自有網絡內的數據索引和搜索功能,通常會裝在內部網絡用來處理公司機密信息,信息不會泄露在網上,因為通常處理的是公司內部最敏感的信息。
雖然 ElasticSearch 通常在公司內部運行,但近年因為其未加密而發生的數據泄露事件不在少數:
2017 年,白帽匯曾對全球使用 ElasticSearch 引擎發生的勒索事件進行監測,最終發現因被攻擊而刪除的數據至少 500 億條,被刪除數據規模至少 450TB。系統顯示,互聯網上公開可訪問的 ElasticSearch 服務器超過 68000 余台,受害總數達 9750 台。其中,美國 4380 台,中國第二為 944 台,其餘來自法國、愛爾蘭和新加坡等地。此次事件後,1% 的 Elasticsearch 啟用了驗證插件,另外有 2% 則關閉了 Elasticsearch。
2018 年 11 月份,美國還曾發生一起 ElasticSearch 服務器在沒有密碼的開放狀態下泄露了將近 5700 萬美國民眾個人信息的事件。當時共泄漏超過 73GB 數據,並且幾個數據庫被緩存在服務器內存中,其中一個數據庫包含的個人信息就達到了 56,934,021 份。
2018 年 12 月份,巴西最大的訂閱電視服務之一的 Sky Brasil 在沒有密碼的情況下將 ElasticSearch 服務器暴露在互聯網上,其 3200 萬客戶數據在網上暴露了很長時間,存儲數據包括客戶姓名、電子郵件地址、密碼、付費電視包數據、客戶端 IP 地址、個人地址、付款方式、設備型號等。
回顧幾起案例,相似之處在於 ElasticSearch 服務器均在沒有密碼保護的情況下遭到泄露,因此企業應該對該服務器進行加密,如果不喜歡付費軟件,網絡中也有很多開源工具可供選擇;其次,升級目前所用的 ElasticSearch 版本,較高版本暫時安全性更好;最後,如果選用了與 ElasticSearch 一起使用的集成工具,也需要檢查這些工具是否會存在漏洞並做好加密工作。
那為什麼不加密呢?我想有一個重要的原因,那還不是人家收費嘛。官方集成的Plugin – Xpack就有安全這一功能。但是!要收費,要收費,要收費。
扯了這麼多,那為啥我們還用它啊,因為它好用啊。

02
具體配置
關於安全,或者網關權限,業內有各式各樣的曲線救國辦法。比如Nginx中配置htpasswd等等,想了解的網上一搜就會有很多。今天,我們來說一個由Spring公司的spring-data-elasticsearch對ES數據的簡單集成和使用。所謂,站在巨人的肩膀上用現成的工具,從開發效率上講,可謂是事半功倍。
使用前提:
- JDK8+
- Springboot 2.1.1.RELEASE
- IDEA
- Mysql5.5+
- lombok
- Greenwich.RELEASE(如果有集成Cloud,建議使用)
1、pom.xml
1 <groupId>com.bboyHan</groupId> 2 <artifactId>demo-project</artifactId> 3 <packaging>pom</packaging> 4 <version>1.0-SNAPSHOT</version> 5 6 <parent> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-parent</artifactId> 9 <version>2.1.1.RELEASE</version> 10 </parent> 11 12 <dependencyManagement> 13 <dependencies> 14 <dependency> 15 <groupId>org.springframework.cloud</groupId> 16 <artifactId>spring-cloud-dependencies</artifactId> 17 <version>Greenwich.RELEASE</version> 18 <!--<version>Edgware.SR3</version>--> 19 <type>pom</type> 20 <scope>import</scope> 21 </dependency> 22 </dependencies> 23 </dependencyManagement> 24 25 <dependencies> 26 ... 27 28 <!-- 對應es 6.2.2 version--> 29 <dependency> 30 <groupId>org.springframework.data</groupId> 31 <artifactId>spring-data-elasticsearch</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-web</artifactId> 36 </dependency> 37 ... 38 </dependencies>
2、application.yml
1server: 2 port: 8080 3 4spring: 5 profiles: 6 active: dev 7 application: 8 name: @pom.artifactId@ 9 10 data: 11 elasticsearch: 12 cluster-name: my-application # 對應es.yml里的cluster.name 13 cluster-nodes: 127.0.0.1:9300 14 repositories: 15 enabled: true 16 17 datasource: 18 url: jdbc:mysql://localhost:3306/bboyhan?characterEncoding=UTF-8&useUnicode=true&useSSL=false 19 username: root 20 password: root 21 type: com.alibaba.druid.pool.DruidDataSource 22 driver-class-name: com.mysql.cj.jdbc.Driver 23
補充:
Mysql在升級6之後,Driver有了新的變化,使用的是「com.mysql.cj.jdbc.Driver」,之前的「com.mysql.jdbc.Driver」已經棄用。另外,如果大家使用了JDBC連接Mysql6 com.mysql.cj.jdbc.Driver, 需要指定時區serverTimezone。
1 datasource: 2 url: jdbc:mysql://localhost:3306/bboyHan?serverTimezone=UTC&useSSL=false
0
3
寫代碼
最近工作比較忙,小編代碼命名比較隨意了,就是傳統的MVC,大家湊合看哈。
3、Controller
1@RestController 2public class TestController { 3 4 @Autowired 5 private TestService testService; 6 7 @GetMapping("/test") 8 public Result test() { 9 return testService.test(); 10 } 11}
4、Service
1/** 2 * @Auther: bboyHan 3 * @Date: 2019/1/23 19:24 4 */ 5@Service 6public class TestService { 7 8 @Autowired 9 private TestEsRepo testEsRepo; 10 11 public Result test() { 12 TestEs entity = new TestEs(); 13 testEsRepo.save(entity); 14 return Result.success(save); 15 } 16}
5、repository
1/** 2 * @Auther: bboyHan 3 * @Date: 2019/1/23 19:20 4 */ 5@Repository 6public interface TestEsRepo extends ElasticsearchRepository<TestEs,Long>{ 7}
6、entity
1import lombok.Data; 2/** 3 * @Auther: bboyHan 4 * @Date: 2019/1/23 19:15 5 * @Description: 6 */ 7@Data 8public class TestEs { 9 10 private Long id; 11 private String name; 12 private String money; 13 14}
7、啟動類
1@SpringBootApplication 2@EnableElasticsearchRepositories 3public class SearchApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(SearchApplication.class, args); 7 } 8}
到這裡就完成了,親測可行。