Browse Source

领域检索接口调整

xiaoqiao 6 months ago
parent
commit
cf629116e9

+ 5 - 0
.idea/jarRepositories.xml

@@ -21,5 +21,10 @@
       <option name="name" value="JBoss Community repository" />
       <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
     </remote-repository>
+    <remote-repository>
+      <option name="id" value="central" />
+      <option name="name" value="Central Repository" />
+      <option name="url" value="https://maven.aliyun.com/repository/public" />
+    </remote-repository>
   </component>
 </project>

+ 82 - 6
src/main/java/com/bowintek/practice/config/ElasticsearchConfig.java

@@ -1,38 +1,114 @@
 package com.bowintek.practice.config;
 
 import co.elastic.clients.elasticsearch.ElasticsearchClient;
+import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
+import co.elastic.clients.elasticsearch.indices.IndexState;
 import co.elastic.clients.json.jackson.JacksonJsonpMapper;
 import co.elastic.clients.transport.ElasticsearchTransport;
 import co.elastic.clients.transport.rest_client.RestClientTransport;
 import co.elastic.clients.util.ContentType;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.Header;
 import org.apache.http.HttpHeaders;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpResponseInterceptor;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
+import org.apache.http.impl.client.BasicCredentialsProvider;
 import org.apache.http.message.BasicHeader;
+import org.apache.http.ssl.SSLContextBuilder;
 import org.elasticsearch.client.RestClient;
+import org.elasticsearch.client.RestClientBuilder;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 import javax.annotation.Resource;
+import javax.net.ssl.SSLContext;
+import java.security.KeyManagementException;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
 import java.util.List;
+import java.util.Locale;
+import java.util.Map;
 
+@Slf4j
 @Configuration
 public class ElasticsearchConfig {
 
     @Resource
     EsConfig config;
 
+
     @Bean
     public ElasticsearchClient esClient() {
-        RestClient restClient = RestClient.builder(new HttpHost(config.getHosts(),config.getPort()))
-                .setHttpClientConfigCallback(httpClientBuilder
-                        ->httpClientBuilder.setDefaultHeaders(
-                                List.of(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())))
-                        .addInterceptorLast((HttpResponseInterceptor) (response, context)
-                                -> response.addHeader("X-Elastic-Product", "Elasticsearch"))).build();
+        SSLContext sslContext;
+        /*try {
+            sslContext =new SSLContextBuilder().loadTrustMaterial(null, TrustSelfSignedStrategy.INSTANCE)
+                    .build();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }*/
+        //System.setProperty("java.security.krb5.conf",config.getKrb5());
+        //System.setProperty("java.security.auth.login.config", config.getJaas());
+        //System.setProperty("java.security.auth.login.defaultConfigurationName", "EsClient");
+        //System.setProperty("java.security.krb5.debug","true");
+        /*RestClient restClient = RestClient.builder(new HttpHost(config.getHosts(),config.getPort(),config.getSchema()))
+                .setHttpClientConfigCallback(httpClientBuilder->
+                        httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
+                        .setSSLContext(sslContext)
+                        .setDefaultCredentialsProvider(getCredentialsProvider(config.getUser(), config.getPassword()))
+                        .setDefaultHeaders(List.of(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())))
+                        .addInterceptorLast((HttpResponseInterceptor) (response, context)-> response.addHeader("X-Elastic-Product", "Elasticsearch"))
+                ).build();*/
+
+        this.setSecConfig();
+        RestClient restClient = RestClient.builder(new HttpHost(config.getHosts(),config.getPort(),config.getSchema()))
+                .setHttpClientConfigCallback(httpClientBuilder->
+                        httpClientBuilder.setDefaultHeaders(List.of(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())))
+                ).build();
+        SmRestClientBuilder builder=  new SmRestClientBuilder(restClient.getNodes());
+        builder.setSslEnabled(true);
+        builder.setEsJaasConfFile(config.getJaas());
+        builder.authenticate(restClient);
+        //RestClient restClient = RestClient.builder(new HttpHost(config.getHosts(),config.getPort(),config.getSchema())).build();
         ElasticsearchTransport transport = new RestClientTransport(
                 restClient, new JacksonJsonpMapper());
         ElasticsearchClient client = new ElasticsearchClient(transport);
+
+        //restClient.getHttpClient().
+        try {
+            // 查看指定索引
+            GetIndexResponse getIndexResponse = client.indices().get(s -> s.index("test4"));
+            Map<String, IndexState> result = getIndexResponse.result();
+            result.forEach((k, v) -> log.info("key = {},value = {}", k, v));
+        }catch (Exception ex){
+            log.info(ex.getMessage());
+            ex.printStackTrace();
+        }
         return client;
     }
+
+    private void setSecConfig() {
+        try {
+            String userKeytabFile = config.getKeytab();
+            LoginUtil.setJaasFile(config.getUser(), userKeytabFile, config.getJaas());
+            LoginUtil.setKrb5Config(config.getKrb5());
+            System.setProperty("elasticsearch.kerberos.jaas.appname", "EsClient");
+            System.setProperty("es.security.indication", "true");
+            log.info(String.format(Locale.ENGLISH, "es.security.indication is %s.", System.getProperty("es.security.indication")));
+        } catch (Exception var3) {
+            Exception e = var3;
+            log.info("Failed to set security conf.", e);
+        }
+
+    }
+    private static CredentialsProvider getCredentialsProvider(String username, String password) {
+        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+        credentialsProvider.setCredentials(AuthScope.ANY,
+                new UsernamePasswordCredentials(username, password));
+        return credentialsProvider;
+    }
 }

+ 5 - 0
src/main/java/com/bowintek/practice/config/EsConfig.java

@@ -12,6 +12,11 @@ import org.springframework.stereotype.Component;
 public class EsConfig {
 
     private String hosts;
+    private String user;
+    private String password;
+    private String krb5;
+    private String jaas;
+    private String keytab;
     private int port;
     private String schema;
     private int connectTimeOut;

+ 170 - 0
src/main/java/com/bowintek/practice/config/LoginUtil.java

@@ -0,0 +1,170 @@
+package com.bowintek.practice.config;
+
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Locale;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+@Slf4j
+public class LoginUtil { 
+    private static final String LINE_SEPARATOR = System.getProperty("line.separator");
+    private static final String ES = "es.";
+    private static final String JAAS_POSTFIX = ".jaas.conf";
+    private static final String IBM_LOGIN_MODULE = "com.ibm.security.auth.module.Krb5LoginModule required";
+    private static final String SUN_LOGIN_MODULE = "com.sun.security.auth.module.Krb5LoginModule required";
+    public static final String JAVA_SECURITY_LOGIN_CONF_KEY = "java.security.auth.login.config";
+    private static final String JAVA_SECURITY_KRB5_CONF_KEY = "java.security.krb5.conf";
+    private static final boolean IS_IBM_JDK = System.getProperty("java.vendor").contains("IBM");
+    private static boolean writeFlag = false;
+    private static String esJaasConfPath;
+
+    public LoginUtil() {
+    }
+
+    static void setKrb5Config(String krb5ConfFile) throws IOException {
+        String ret = System.getProperty("java.security.krb5.conf");
+        if (ret != null && !ret.isEmpty()) {
+            if (krb5ConfFile != null && !krb5ConfFile.isEmpty() && !ret.equals(krb5ConfFile)) {
+                System.setProperty("java.security.krb5.conf", krb5ConfFile);
+            }
+        } else {
+            if (krb5ConfFile != null && !krb5ConfFile.isEmpty()) {
+                System.setProperty("java.security.krb5.conf", krb5ConfFile);
+                ret = System.getProperty("java.security.krb5.conf");
+            }
+
+            if (ret == null || ret.isEmpty() || !ret.equals(krb5ConfFile)) {
+                log.error(String.format(Locale.ENGLISH, "%s is null.", "java.security.krb5.conf"));
+                throw new IOException(String.format(Locale.ENGLISH, "%s is null.", "java.security.krb5.conf"));
+            }
+        }
+
+    }
+
+    static synchronized void setJaasFile(String principal, String keytabPath, String customJaasPath) throws IOException {
+        String jaasPath;
+        if (customJaasPath != null && !customJaasPath.trim().isEmpty()) {
+            jaasPath = customJaasPath;
+        } else {
+            if (keytabPath == null || keytabPath.isEmpty()) {
+                log.error("The user keytab file path is null or empty, please check the configuration.");
+                throw new IOException("The user keytab file path is null or empty, please check the configuration.");
+            }
+
+            String filePath = keytabPath.substring(0, keytabPath.lastIndexOf(File.separator));
+            jaasPath = filePath + File.separator + "es." + System.getProperty("user.name") + ".jaas.conf";
+            jaasPath = jaasPath.replace("\\", "\\\\");
+            keytabPath = keytabPath.replace("\\", "\\\\");
+            if ((new File(jaasPath)).exists()) {
+                if (!writeFlag) {
+                    deleteJaasFile(jaasPath);
+                    writeJaasFile(jaasPath, principal, keytabPath);
+                }
+            } else {
+                writeJaasFile(jaasPath, principal, keytabPath);
+            }
+        }
+
+        if (!writeFlag) {
+            System.setProperty("java.security.auth.login.config", jaasPath);
+            writeFlag = true;
+            log.debug(String.format(Locale.ENGLISH, "jaasPath is %s.", jaasPath));
+            log.debug(String.format(Locale.ENGLISH, "keytabPath is %s.", keytabPath));
+        }
+
+        esJaasConfPath = jaasPath;
+    }
+
+    static String getEsJaasConfPath() {
+        return esJaasConfPath;
+    }
+
+    private static void writeJaasFile(String jaasPath, String principal, String keytabPath) throws IOException {
+        try {
+            FileWriter writer = new FileWriter(new File(jaasPath));
+
+            try {
+                writer.write(getJaasConfContext(principal, keytabPath));
+                writer.flush();
+            } catch (Throwable var7) {
+                try {
+                    writer.close();
+                } catch (Throwable var6) {
+                    var7.addSuppressed(var6);
+                }
+
+                throw var7;
+            }
+
+            writer.close();
+        } catch (IOException var8) {
+            throw new IOException("Failed to create jaas.conf file");
+        }
+    }
+
+    private static void deleteJaasFile(String jaasPath) throws IOException {
+        File jaasFile = new File(jaasPath);
+        if (jaasFile.exists() && !jaasFile.delete()) {
+            throw new IOException("Failed to delete exists jaas file.");
+        }
+    }
+
+    private static String getJaasConfContext(String principal, String keytabPath) {
+        Module[] allModule = LoginUtil.Module.values();
+        StringBuilder builder = new StringBuilder();
+        Module[] var4 = allModule;
+        int var5 = allModule.length;
+
+        for(int var6 = 0; var6 < var5; ++var6) {
+            Module modlue = var4[var6];
+            builder.append(getModuleContext(principal, keytabPath, modlue));
+        }
+
+        return builder.toString();
+    }
+
+    private static String getModuleContext(String userPrincipal, String keyTabPath, Module module) {
+        StringBuilder builder = new StringBuilder();
+        if (IS_IBM_JDK) {
+            builder.append(module.getName()).append(" {").append(LINE_SEPARATOR);
+            builder.append("com.ibm.security.auth.module.Krb5LoginModule required").append(LINE_SEPARATOR);
+            builder.append("credsType=both").append(LINE_SEPARATOR);
+            builder.append("principal=\"").append(userPrincipal).append("\"").append(LINE_SEPARATOR);
+            builder.append("useKeytab=\"").append(keyTabPath).append("\"").append(LINE_SEPARATOR);
+            builder.append("debug=true;").append(LINE_SEPARATOR);
+            builder.append("};").append(LINE_SEPARATOR);
+        } else {
+            builder.append(module.getName()).append(" {").append(LINE_SEPARATOR);
+            builder.append("com.sun.security.auth.module.Krb5LoginModule required").append(LINE_SEPARATOR);
+            builder.append("useKeyTab=true").append(LINE_SEPARATOR);
+            builder.append("keyTab=\"").append(keyTabPath).append("\"").append(LINE_SEPARATOR);
+            builder.append("principal=\"").append(userPrincipal).append("\"").append(LINE_SEPARATOR);
+            builder.append("useTicketCache=false").append(LINE_SEPARATOR);
+            builder.append("storeKey=true").append(LINE_SEPARATOR);
+            builder.append("debug=true;").append(LINE_SEPARATOR);
+            builder.append("};").append(LINE_SEPARATOR);
+        }
+
+        return builder.toString();
+    }
+
+    public static enum Module {
+        Elasticsearch("EsClient");
+
+        private String name;
+
+        private Module(String name) {
+            this.name = name;
+        }
+
+        public String getName() {
+            return this.name;
+        }
+    }
+}
+

File diff suppressed because it is too large
+ 655 - 0
src/main/java/com/bowintek/practice/config/SmRestClientBuilder.java


+ 18 - 0
src/main/java/com/bowintek/practice/services/impl/EsQueryServiceImpl.java

@@ -15,6 +15,8 @@ import co.elastic.clients.elasticsearch.cat.IndicesResponse;
 import co.elastic.clients.elasticsearch.core.SearchRequest;
 import co.elastic.clients.elasticsearch.core.SearchResponse;
 import co.elastic.clients.elasticsearch.core.search.Hit;
+import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
+import co.elastic.clients.elasticsearch.indices.IndexState;
 import co.elastic.clients.elasticsearch.indices.PutMappingRequest;
 import co.elastic.clients.elasticsearch.indices.PutMappingResponse;
 import co.elastic.clients.json.JsonData;
@@ -125,10 +127,24 @@ public class EsQueryServiceImpl implements EsQueryService {
         }
         return queryList;
     }
+    void getIndex()
+    {
+        try {
+            // 查看指定索引
+            GetIndexResponse getIndexResponse = esClient.indices().get(s -> s.index("test4"));
+            Map<String, IndexState> result = getIndexResponse.result();
+            result.forEach((k, v) -> log.info("key = {},value = {}", k, v));
+        }catch (Exception ex){
+            log.info(ex.getMessage());
+            ex.printStackTrace();
+        }
+    }
 
     @Override
     public Map<String, Object> query(List<EsQueryText> queryList, List<ComparisonResult> limiters,
                                      int page, int limit, String orderType, String orderBy) {
+        getIndex();
+
         //[1]需要返回的结果map
         Map<String, Object> result = new HashMap<>();
         result.put("total", 0);
@@ -198,6 +214,7 @@ public class EsQueryServiceImpl implements EsQueryService {
 
             log.info("dsl:" + request.toString());
             SearchResponse<ObjectNode> response = esClient.search(request, ObjectNode.class);
+            log.info("response:" + response.toString());
 
             //[6]转换结果,可以对不同的index做出参数输出
             List<Map<String, Object>> rows = searchResponse2List(response);
@@ -211,6 +228,7 @@ public class EsQueryServiceImpl implements EsQueryService {
             result.put("SearchRequest", stringToNodeJson(jsonStrings[1]));
             System.out.println(response.hits().total() + " " + request.toString());
         } catch (Exception ex) {
+            log.info(ex.getMessage());
             result.put("Message", ex.getMessage());
             result.put("StackTrace", ex.getStackTrace());
         }

+ 10 - 4
src/main/resources/application.yml

@@ -12,7 +12,7 @@ spring:
     type: com.alibaba.druid.pool.DruidDataSource
     driver-class-name: com.mysql.cj.jdbc.Driver
     #基本属性
-    url: jdbc:mysql://192.168.0.68:3306/smartSearchDB?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
+    url: jdbc:mysql://office.bowintek.com:3306/smartSearchDB?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
     #url: jdbc:mysql://office.bowintek.com:3306/practicedb?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
     username: root
     password: bowin@123
@@ -20,7 +20,7 @@ spring:
       # 数据源基本配置
       username: root
       password: bowin@123
-      url: jdbc:mysql://192.168.0.68:3306/smartSearchDB?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
+      url: jdbc:mysql://office.bowintek.com:3306/smartSearchDB?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
       driver-class-name: com.mysql.jdbc.Driver
     postgre:
       # 数据源基本配置
@@ -87,9 +87,14 @@ spring:
       date-time: yyyy-MM-dd HH:mm:ss
 
 elasticsearch:
-  hosts: 192.168.0.68
+  hosts: office.bowintek.com
   port: 9200
-  schema: http
+  user: hyzc_default
+  password: BBBBAAAAYwAAAAAAAAAJAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAABAAAACdw1GRyLaxi89wQGFTEmBgMy+ly28Z2loKSoRaEAAAAAAQAAAQAAAAAAAAAjtQGieqqki1A4xVkfYeZGms/klXk6kZQjTO898+I61DTughO/3UUIAVjdcN3hPIPmqPhra2Y0p8gFNG/cC3PiN677bQ==
+  schema: https
+  krb5: E:\es_pro\out\config\krb5.conf
+  jaas: E:\es_pro\out\config\es.pfl.jaas.conf
+  keytab: E:\es_pro\out\config\user.keytab
   connectTimeOut: 1000
   socketTimeOut: 30000
   connectionRequestTimeOut: 500
@@ -97,6 +102,7 @@ elasticsearch:
   maxConnectPerRoute: 100
   preStr: es_
 
+
 logging:
   config: classpath:logback-spring.xml
 mybatis:

+ 10 - 4
target/classes/application.yml

@@ -12,7 +12,7 @@ spring:
     type: com.alibaba.druid.pool.DruidDataSource
     driver-class-name: com.mysql.cj.jdbc.Driver
     #基本属性
-    url: jdbc:mysql://192.168.0.68:3306/smartSearchDB?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
+    url: jdbc:mysql://office.bowintek.com:3306/smartSearchDB?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
     #url: jdbc:mysql://office.bowintek.com:3306/practicedb?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
     username: root
     password: bowin@123
@@ -20,7 +20,7 @@ spring:
       # 数据源基本配置
       username: root
       password: bowin@123
-      url: jdbc:mysql://192.168.0.68:3306/smartSearchDB?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
+      url: jdbc:mysql://office.bowintek.com:3306/smartSearchDB?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
       driver-class-name: com.mysql.jdbc.Driver
     postgre:
       # 数据源基本配置
@@ -87,9 +87,14 @@ spring:
       date-time: yyyy-MM-dd HH:mm:ss
 
 elasticsearch:
-  hosts: 192.168.0.68
+  hosts: office.bowintek.com
   port: 9200
-  schema: http
+  user: hyzc_default
+  password: BBBBAAAAYwAAAAAAAAAJAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAABAAAACdw1GRyLaxi89wQGFTEmBgMy+ly28Z2loKSoRaEAAAAAAQAAAQAAAAAAAAAjtQGieqqki1A4xVkfYeZGms/klXk6kZQjTO898+I61DTughO/3UUIAVjdcN3hPIPmqPhra2Y0p8gFNG/cC3PiN677bQ==
+  schema: https
+  krb5: E:\es_pro\out\config\krb5.conf
+  jaas: E:\es_pro\out\config\es.pfl.jaas.conf
+  keytab: E:\es_pro\out\config\user.keytab
   connectTimeOut: 1000
   socketTimeOut: 30000
   connectionRequestTimeOut: 500
@@ -97,6 +102,7 @@ elasticsearch:
   maxConnectPerRoute: 100
   preStr: es_
 
+
 logging:
   config: classpath:logback-spring.xml
 mybatis: