update ES的URL增加Basic auth认证
This commit is contained in:
+98
-14
@@ -1,5 +1,6 @@
|
|||||||
package com.jero.common.es;
|
package com.jero.common.es;
|
||||||
|
|
||||||
|
import cn.hutool.http.HttpRequest;
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -32,6 +33,11 @@ public class JeroElasticsearchTemplate {
|
|||||||
// ElasticSearch 最大可返回条目数
|
// ElasticSearch 最大可返回条目数
|
||||||
public static final int ES_MAX_SIZE = 10000;
|
public static final int ES_MAX_SIZE = 10000;
|
||||||
|
|
||||||
|
@Value("${jero.elasticsearch.username}")
|
||||||
|
private String username;
|
||||||
|
@Value("${jero.elasticsearch.password}")
|
||||||
|
private String password;
|
||||||
|
|
||||||
public JeroElasticsearchTemplate(@Value("${jero.elasticsearch.cluster-nodes}") String baseUrl, @Value("${jero.elasticsearch.check-enabled}") boolean checkEnabled) {
|
public JeroElasticsearchTemplate(@Value("${jero.elasticsearch.cluster-nodes}") String baseUrl, @Value("${jero.elasticsearch.check-enabled}") boolean checkEnabled) {
|
||||||
log.debug("JeroElasticsearchTemplate BaseURL:" + baseUrl);
|
log.debug("JeroElasticsearchTemplate BaseURL:" + baseUrl);
|
||||||
if (StringUtils.isNotEmpty(baseUrl)) {
|
if (StringUtils.isNotEmpty(baseUrl)) {
|
||||||
@@ -40,7 +46,11 @@ public class JeroElasticsearchTemplate {
|
|||||||
if (checkEnabled) {
|
if (checkEnabled) {
|
||||||
try {
|
try {
|
||||||
this.getElasticsearchVersion();
|
this.getElasticsearchVersion();
|
||||||
RestUtil.get(this.getBaseUrl().toString());
|
String basicAuth = this.getBasicAuth();
|
||||||
|
HttpRequest.get(this.getBaseUrl().toString())
|
||||||
|
.header("Authorization", basicAuth)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
log.info("ElasticSearch 服务连接成功");
|
log.info("ElasticSearch 服务连接成功");
|
||||||
log.info("ElasticSearch version: " + this.version);
|
log.info("ElasticSearch version: " + this.version);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -56,8 +66,12 @@ public class JeroElasticsearchTemplate {
|
|||||||
*/
|
*/
|
||||||
private void getElasticsearchVersion() {
|
private void getElasticsearchVersion() {
|
||||||
if (this.version == null) {
|
if (this.version == null) {
|
||||||
String url = this.getBaseUrl().toString();
|
String basicAuth = this.getBasicAuth();
|
||||||
JSONObject result = RestUtil.get(url);
|
String response = HttpRequest.get(this.getBaseUrl().toString())
|
||||||
|
.header("Authorization", basicAuth)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
|
JSONObject result = JSONObject.parseObject(response);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
JSONObject v = result.getJSONObject("version");
|
JSONObject v = result.getJSONObject("version");
|
||||||
this.version = v.getString("number");
|
this.version = v.getString("number");
|
||||||
@@ -65,6 +79,13 @@ public class JeroElasticsearchTemplate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getBasicAuth(){
|
||||||
|
String authString = username + ":" + password;
|
||||||
|
String encode = Base64.getEncoder().encodeToString(authString.getBytes());
|
||||||
|
String authHeader = "Basic " + encode;
|
||||||
|
return authHeader;
|
||||||
|
}
|
||||||
|
|
||||||
public StringBuilder getBaseUrl(String indexName, String typeName) {
|
public StringBuilder getBaseUrl(String indexName, String typeName) {
|
||||||
typeName = typeName.trim().toLowerCase();
|
typeName = typeName.trim().toLowerCase();
|
||||||
return this.getBaseUrl(indexName).append("/").append(typeName);
|
return this.getBaseUrl(indexName).append("/").append(typeName);
|
||||||
@@ -79,6 +100,59 @@ public class JeroElasticsearchTemplate {
|
|||||||
return new StringBuilder("http://").append(this.baseUrl);
|
return new StringBuilder("http://").append(this.baseUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cat 查询ElasticSearch系统数据,返回json
|
||||||
|
*/
|
||||||
|
public JSONArray _cat(String urlAfter) {
|
||||||
|
String url = this.getBaseUrl().append("/_cat").append(urlAfter).append("?").append(FORMAT_JSON).toString();
|
||||||
|
String basicAuth = this.getBasicAuth();
|
||||||
|
String response = HttpRequest.get(url)
|
||||||
|
.header("Authorization", basicAuth)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
|
JSONArray jsonArray = JSONArray.parseArray(response);
|
||||||
|
return jsonArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONObject put(String url){
|
||||||
|
String basicAuth = this.getBasicAuth();
|
||||||
|
String response = HttpRequest.put(url)
|
||||||
|
.header("Authorization", basicAuth)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
public JSONObject put(String url, String params){
|
||||||
|
String basicAuth = this.getBasicAuth();
|
||||||
|
String response = HttpRequest.put(url)
|
||||||
|
.body(params)
|
||||||
|
.header("Authorization", basicAuth)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
public JSONObject post(String url, JSONObject params){
|
||||||
|
String basicAuth = this.getBasicAuth();
|
||||||
|
String response = HttpRequest.post(url)
|
||||||
|
.body(params.toJSONString())
|
||||||
|
.header("Authorization", basicAuth)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
public JSONObject delete(String url){
|
||||||
|
String basicAuth = this.getBasicAuth();
|
||||||
|
String response = HttpRequest.delete(url)
|
||||||
|
.header("Authorization", basicAuth)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* cat 查询ElasticSearch系统数据,返回json
|
* cat 查询ElasticSearch系统数据,返回json
|
||||||
*/
|
*/
|
||||||
@@ -107,7 +181,7 @@ public class JeroElasticsearchTemplate {
|
|||||||
if (!StringUtils.isEmpty(indexName)) {
|
if (!StringUtils.isEmpty(indexName)) {
|
||||||
urlAfter.append("/").append(indexName.trim().toLowerCase());
|
urlAfter.append("/").append(indexName.trim().toLowerCase());
|
||||||
}
|
}
|
||||||
return _cat(urlAfter.toString(), JSONArray.class).getBody();
|
return _cat(urlAfter.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -139,7 +213,12 @@ public class JeroElasticsearchTemplate {
|
|||||||
public JSONObject getDataById(String indexName, String typeName, String dataId) {
|
public JSONObject getDataById(String indexName, String typeName, String dataId) {
|
||||||
String url = this.getBaseUrl(indexName, typeName).append("/").append(dataId).toString();
|
String url = this.getBaseUrl(indexName, typeName).append("/").append(dataId).toString();
|
||||||
log.info("url:" + url);
|
log.info("url:" + url);
|
||||||
JSONObject result = RestUtil.get(url);
|
String basicAuth = this.getBasicAuth();
|
||||||
|
String response = HttpRequest.get(this.getBaseUrl().toString())
|
||||||
|
.header("Authorization", basicAuth)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
|
JSONObject result = JSONObject.parseObject(response);
|
||||||
boolean found = result.getBoolean("found");
|
boolean found = result.getBoolean("found");
|
||||||
if (found) {
|
if (found) {
|
||||||
return result.getJSONObject("_source");
|
return result.getJSONObject("_source");
|
||||||
@@ -157,7 +236,7 @@ public class JeroElasticsearchTemplate {
|
|||||||
String url = this.getBaseUrl(indexName).toString();
|
String url = this.getBaseUrl(indexName).toString();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return RestUtil.put(url).getBoolean("acknowledged");
|
return this.put(url).getBoolean("acknowledged");
|
||||||
} catch (org.springframework.web.client.HttpClientErrorException ex) {
|
} catch (org.springframework.web.client.HttpClientErrorException ex) {
|
||||||
if (HttpStatus.BAD_REQUEST == ex.getStatusCode()) {
|
if (HttpStatus.BAD_REQUEST == ex.getStatusCode()) {
|
||||||
log.warn("索引创建失败:" + indexName + " 已存在,无需再创建");
|
log.warn("索引创建失败:" + indexName + " 已存在,无需再创建");
|
||||||
@@ -176,7 +255,7 @@ public class JeroElasticsearchTemplate {
|
|||||||
public boolean removeIndex(String indexName) {
|
public boolean removeIndex(String indexName) {
|
||||||
String url = this.getBaseUrl(indexName).toString();
|
String url = this.getBaseUrl(indexName).toString();
|
||||||
try {
|
try {
|
||||||
return RestUtil.delete(url).getBoolean("acknowledged");
|
return this.delete(url).getBoolean("acknowledged");
|
||||||
} catch (org.springframework.web.client.HttpClientErrorException ex) {
|
} catch (org.springframework.web.client.HttpClientErrorException ex) {
|
||||||
if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
|
if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
|
||||||
log.warn("索引删除失败:" + indexName + " 不存在,无需删除");
|
log.warn("索引删除失败:" + indexName + " 不存在,无需删除");
|
||||||
@@ -204,7 +283,13 @@ public class JeroElasticsearchTemplate {
|
|||||||
}
|
}
|
||||||
log.info("getIndexMapping-url:" + url);
|
log.info("getIndexMapping-url:" + url);
|
||||||
try {
|
try {
|
||||||
return RestUtil.get(url);
|
String basicAuth = this.getBasicAuth();
|
||||||
|
String response = HttpRequest.get(this.getBaseUrl().toString())
|
||||||
|
.header("Authorization", basicAuth)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
|
JSONObject result = JSONObject.parseObject(response);
|
||||||
|
return result;
|
||||||
} catch (org.springframework.web.client.HttpClientErrorException e) {
|
} catch (org.springframework.web.client.HttpClientErrorException e) {
|
||||||
String message = e.getMessage();
|
String message = e.getMessage();
|
||||||
if (message != null && message.contains("404 Not Found")) {
|
if (message != null && message.contains("404 Not Found")) {
|
||||||
@@ -291,7 +376,7 @@ public class JeroElasticsearchTemplate {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
String result = RestUtil.put(url, data).getString("result");
|
String result = this.put(url, data.toJSONString()).getString("result");
|
||||||
return "created".equals(result) || "updated".equals(result);
|
return "created".equals(result) || "updated".equals(result);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage() + "\n-- url: " + url + "\n-- data: " + data.toJSONString());
|
log.error(e.getMessage() + "\n-- url: " + url + "\n-- data: " + data.toJSONString());
|
||||||
@@ -327,8 +412,7 @@ public class JeroElasticsearchTemplate {
|
|||||||
bodySB.append(data.toJSONString()).append("\n");
|
bodySB.append(data.toJSONString()).append("\n");
|
||||||
}
|
}
|
||||||
log.info("+-+-+-: bodySB.toString(): " + bodySB.toString());
|
log.info("+-+-+-: bodySB.toString(): " + bodySB.toString());
|
||||||
HttpHeaders headers = RestUtil.getHeaderApplicationJson();
|
this.put(url,bodySB.toString());
|
||||||
RestUtil.request(url, HttpMethod.PUT, headers, null, bodySB, JSONObject.class);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,7 +424,7 @@ public class JeroElasticsearchTemplate {
|
|||||||
public boolean delete(String indexName, String typeName, String dataId) {
|
public boolean delete(String indexName, String typeName, String dataId) {
|
||||||
String url = this.getBaseUrl(indexName, typeName).append("/").append(dataId).toString();
|
String url = this.getBaseUrl(indexName, typeName).append("/").append(dataId).toString();
|
||||||
try {
|
try {
|
||||||
return "deleted".equals(RestUtil.delete(url).getString("result"));
|
return "deleted".equals(this.delete(url).getString("result"));
|
||||||
} catch (org.springframework.web.client.HttpClientErrorException ex) {
|
} catch (org.springframework.web.client.HttpClientErrorException ex) {
|
||||||
if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
|
if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
|
||||||
return false;
|
return false;
|
||||||
@@ -362,7 +446,7 @@ public class JeroElasticsearchTemplate {
|
|||||||
String url = this.getBaseUrl(indexName, typeName).append("/_search").toString();
|
String url = this.getBaseUrl(indexName, typeName).append("/_search").toString();
|
||||||
|
|
||||||
log.info("url:" + url + " ,search: " + queryObject.toJSONString());
|
log.info("url:" + url + " ,search: " + queryObject.toJSONString());
|
||||||
JSONObject res = RestUtil.post(url, queryObject);
|
JSONObject res = this.post(url, queryObject);
|
||||||
log.info("url:" + url + " ,return res: \n" + res.toJSONString());
|
log.info("url:" + url + " ,return res: \n" + res.toJSONString());
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -486,7 +570,7 @@ public class JeroElasticsearchTemplate {
|
|||||||
String url = this.getBaseUrl(indexName, typeName).append("/_delete_by_query").toString();
|
String url = this.getBaseUrl(indexName, typeName).append("/_delete_by_query").toString();
|
||||||
|
|
||||||
log.info("url:" + url + " ,delete: " + queryObject.toJSONString());
|
log.info("url:" + url + " ,delete: " + queryObject.toJSONString());
|
||||||
JSONObject res = RestUtil.post(url, queryObject);
|
JSONObject res = this.post(url, queryObject);
|
||||||
log.info("url:" + url + " ,return res: \n" + res.toJSONString());
|
log.info("url:" + url + " ,return res: \n" + res.toJSONString());
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -259,6 +259,8 @@ jero:
|
|||||||
staticDomain: jerodev
|
staticDomain: jerodev
|
||||||
# ElasticSearch 6设置
|
# ElasticSearch 6设置
|
||||||
elasticsearch:
|
elasticsearch:
|
||||||
|
username: grp
|
||||||
|
password: Grp2023.08
|
||||||
cluster-name: jero-ES
|
cluster-name: jero-ES
|
||||||
cluster-nodes: 10.10.10.45:7900
|
cluster-nodes: 10.10.10.45:7900
|
||||||
check-enabled: false
|
check-enabled: false
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
server:
|
server:
|
||||||
port: 8080
|
port: 8184
|
||||||
tomcat:
|
tomcat:
|
||||||
max-swallow-size: -1
|
max-swallow-size: -1
|
||||||
error:
|
error:
|
||||||
@@ -230,8 +230,10 @@ jero:
|
|||||||
staticDomain: jerodev
|
staticDomain: jerodev
|
||||||
# ElasticSearch 6设置
|
# ElasticSearch 6设置
|
||||||
elasticsearch:
|
elasticsearch:
|
||||||
|
username: grp
|
||||||
|
password: Grp2023.08
|
||||||
cluster-name: jero-ES
|
cluster-name: jero-ES
|
||||||
cluster-nodes: 127.0.0.1:9200
|
cluster-nodes: 10.186.43.11:9200
|
||||||
check-enabled: false
|
check-enabled: false
|
||||||
# 表单设计器配置
|
# 表单设计器配置
|
||||||
desform:
|
desform:
|
||||||
|
|||||||
Reference in New Issue
Block a user