Commit 2c6bc4ca by sunxiwang

es添加密码验证

1 parent a90b6219
...@@ -150,6 +150,8 @@ xss: ...@@ -150,6 +150,8 @@ xss:
elasticsearch: elasticsearch:
schema: http schema: http
address: ${ES_ADDRESS:localhost} address: ${ES_ADDRESS:localhost}
userName: ""
password: ""
connectTimeout: 10000 connectTimeout: 10000
socketTimeout: 10000 socketTimeout: 10000
connectionRequestTimeout: 10000 connectionRequestTimeout: 10000
......
package com.linkwechat.common.config; package com.linkwechat.common.config;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
...@@ -30,6 +34,11 @@ public class ElasticSearchConfig { ...@@ -30,6 +34,11 @@ public class ElasticSearchConfig {
@Value("${elasticsearch.address}") @Value("${elasticsearch.address}")
private String address; private String address;
@Value("${elasticsearch.userName}")
private String userName;
@Value("${elasticsearch.password}")
private String password;
/** /**
* 连接超时时间 * 连接超时时间
*/ */
...@@ -70,6 +79,8 @@ public class ElasticSearchConfig { ...@@ -70,6 +79,8 @@ public class ElasticSearchConfig {
String port = addr.split(":")[1]; String port = addr.split(":")[1];
hostLists.add(new HttpHost(host, Integer.parseInt(port), schema)); hostLists.add(new HttpHost(host, Integer.parseInt(port), schema));
} }
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
// 转换成 HttpHost 数组 // 转换成 HttpHost 数组
HttpHost[] httpHost = hostLists.toArray(new HttpHost[]{}); HttpHost[] httpHost = hostLists.toArray(new HttpHost[]{});
// 构建连接对象 // 构建连接对象
...@@ -85,6 +96,7 @@ public class ElasticSearchConfig { ...@@ -85,6 +96,7 @@ public class ElasticSearchConfig {
builder.setHttpClientConfigCallback(httpClientBuilder -> { builder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setMaxConnTotal(maxConnectNum); httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute); httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
return httpClientBuilder; return httpClientBuilder;
}); });
return new RestHighLevelClient(builder); return new RestHighLevelClient(builder);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!