ES模块配置

依赖

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

配置文件

修改 application.yml

参考:https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.data.spring.elasticsearch.restclient.ssl.bundle

spring:
  elasticsearch:
    uris:
      - https://127.0.0.1:9200
    username: elastic
    password: xxxxx

忽略ES的SSL证书

新建一个类即可

package com.xxx.vulnweb.util;

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.boot.autoconfigure.elasticsearch.RestClientBuilderCustomizer;
import org.springframework.stereotype.Component;

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

@Component
public class RestClientBuilderCustomizerImpl implements RestClientBuilderCustomizer {
    @Override
    public void customize(RestClientBuilder builder) {

    }

    @Override
    public void customize(HttpAsyncClientBuilder builder) {
        SSLContextBuilder sscb = SSLContexts.custom();
        try {
            sscb.loadTrustMaterial((chain, authType) -> {
                // 在这里跳过证书信息校验
                //System.out.println("暂时isTrusted|" + authType + "|" + Arrays.toString(chain));
                return true;
            });
        } catch (NoSuchAlgorithmException | KeyStoreException e) {
            e.printStackTrace();
        }
        try {
            builder.setSSLContext(sscb.build());
        } catch (KeyManagementException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        // 这里跳过主机名称校验
        builder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
    }
}

基础使用

创建ES数据模型

根据ES中数据的结构创建模型,比如,类似mybatis的主要是辅助mapper接收对应的数据

package com.xxx.vulnweb.model.es;

import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.stereotype.Component;

@Component
@Data
@Document(indexName = "d4m1ts-scanner")
public class ESTestModel {
    private String id;
    private String 目标域名;
    private String 扫描类型;
}

编写mapper接口

继承ElasticsearchRepository即可,需要指定刚才创建的ES数据模型

package com.xxx.vulnweb.mapper.es;

import com.xxx.vulnweb.model.es.ESTestModel;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;

@Component
public interface ElasticTestMapper extends ElasticsearchRepository<ESTestModel, Long> {

}

自带了一些通用的接口,支持 JPA风格的查询方式,也可以用DSL语法,下面是补充的一些方法

package com.xxx.vulnweb.mapper.es;

import com.xxx.vulnweb.model.es.ESVulnScanResultModel;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
import org.springframework.data.domain.Page;


@Component
public interface ElasticTestMapper extends ElasticsearchRepository<ESVulnScanResultModel, Long> {
    Page<ESVulnScanResultModel> findAllBy扫描类型(String 扫描类型, Pageable pageable);

    ESVulnScanResultModel[] findAllBy扫描类型And目标域名(String 扫描类型, String 目标域名, int from, int size);

    // 分页查询,主要参数 pageable
    @Query("{\"bool\": {\"must\": [{\"match_all\": {}}], \"filter\": [], \"should\": [], \"must_not\": []}}")
    Page<ESVulnScanResultModel> findByCustomQueryWithLimitAndOffset(Pageable pageable);
}

其中findByCustomQueryWithLimitAndOffset 使用方法如下:

// PageRequest.of 第一个参数是 pageNumber,第二个参数是pageSize
Page<ESVulnScanResultModel> byCustomQueryWithLimitAndOffset = elasticTestMapper.findByCustomQueryWithLimitAndOffset(PageRequest.of(1, 5));

测试使用

参考:https://juejin.cn/post/7267487352457510971

ElasticsearchRepository 中有很多的方法了,大多数情况直接用里面的就行

package com.xxx.vulnweb.controller;

import com.xxx.vulnweb.mapper.es.ElasticTestMapper;
import com.xxx.vulnweb.model.es.ESVulnScanResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    ElasticTestMapper elasticTestMapper;

    @GetMapping("/test")
    public Object test(){
//        Iterable<ESVulnScanResultModel> all = elasticTestMapper.findAll();
//        Page<ESVulnScanResultModel> all = elasticTestMapper.findAllBy扫描类型("漏洞扫描", PageRequest.of(1, 5));
        Page<ESVulnScanResultModel> byCustomQueryWithLimitAndOffset = elasticTestMapper.findByCustomQueryWithLimitAndOffset(PageRequest.of(1, 5));
        return byCustomQueryWithLimitAndOffset;
    }
}

image-20240124上午114112592

Copyright © d4m1ts 2023 all right reserved,powered by Gitbook该文章修订时间: 2024-05-09 17:02:01

results matching ""

    No results matching ""