文章来源:https://www.toymoban.com/news/detail-522724.html
文章来源地址https://www.toymoban.com/news/detail-522724.html
@Bean(name = "highLevelClient")
public RestHighLevelClient highLevelClient(@Autowired RestClientBuilder restClientBuilder) {
try{
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
Path keyStorePath = Paths.get(keystorePath); //密钥存储库
String keyStorePass = ""; //库的密码
KeyStore truststore = KeyStore.getInstance("jks");
try (InputStream is = Files.newInputStream(keyStorePath)) {
try {
truststore.load(is, keyStorePass.toCharArray());
} catch (CertificateException e) {
log.error("认证加载失败");
throw new RuntimeException();
}
} catch (IOException e) {
log.error("=============== 认证文件不存在 ==============");
}
//SSL认证
SSLContextBuilder sslBuilder = null;
sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null);
final SSLContext sslContext = sslBuilder.build();
restClientBuilder.setRequestConfigCallback(
new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(
RequestConfig.Builder requestConfigBuilder) {
return requestConfigBuilder.setSocketTimeout(TIME_OUT);
}
});
restClientBuilder.setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider) //校验用户名和密码
.setSSLContext(sslContext) //校验证书
);
//TODO 此处可以进行其它操作
return new RestHighLevelClient(restClientBuilder);
}catch (Exception e){
log.error("es连接异常");
throw new RuntimeException();
}
}
到了这里,关于JAVA ElasticSearch中配置用户名密码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!