- pom引入
- 代码
代码org.elasticsearch.client elasticsearch-rest-high-level-client6.3.2
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.RestHighLevelClient; public class SecurityRestClient { private static final int HTTP_PORT = 20019; private static final String IP = "10.10.10.13"; private static final String SecurityUser = "jiesi-6.3"; private static final String SecurityPassword = "***********"; public static RestHighLevelClient RestClient() { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost(IP, HTTP_PORT, "http") ).setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(init())) ); return client; } private static CredentialsProvider init() { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(SecurityUser, SecurityPassword)); return credentialsProvider; } }
注意回调方法setHttpClientConfigCallback内不要添加
httpClientBuilder.disableAuthCaching()
方法。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)