一/设置用户名密码
版本elasticsearch 7.6.1
elasticsearch/config/elasticsearch.yaml文件中添加如下内容
# ---------------------------------- Auth -------------------------------------
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
在elasticsearch bin目录下使用如下命令
./elasticsearch-setup-passwords interactive
设置密码,必须大于6位
elastic: admin1
apm_system: admin1
kibana: admin1
logstash_system: admin1
beats_system: admin1
remote_monitoring_user: admin1
进入kibana/config/kibana.yaml文件中修改
elasticsearch.username = elastic
elasticsearch.password = admin1
二/java代码访问
java代码中,配置一个config,使用如下代码,可以全局配置RestHighLevelClient,也可以RestHighLevelClient,执行crud时来进行设置.
import cn.hutool.core.codec.Base64;
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.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticClientConfig {
@Value("${elasticsearch.clientIps}")
private String clientIps;
@Value("${elasticsearch.httpPort}")
private int httpPort;
@Value("${elasticsearch.username}")
private String username;
@Value("${elasticsearch.password}")
private String password;
@Value("${elasticsearch.heapSize}")
private Integer heapSize;
public static RequestOptions COMMON_OPTIONS=null;
void initRequestOptions (){
String TOKEN = Base64.encode(username+":"+password) ;
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
builder.addHeader("Authorization","Basic "+TOKEN);
builder.setHttpAsyncResponseConsumerFactory(
new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(heapSize*1024*1024));
COMMON_OPTIONS = builder.build();
}
private HttpHost[] getHttpHosts(String clientIps, int esHttpPort) {
String[] clientIpList = clientIps.split(",");
HttpHost[] httpHosts = new HttpHost[clientIpList.length];
for (int i = 0; i < clientIpList.length; i++) {
httpHosts[i] = new HttpHost(clientIpList[i], esHttpPort, "http");
}
return httpHosts;
}
/**
* 创建带HTTP Basic Auth认证rest客户端
*/
@Bean
public RestHighLevelClient restHighLevelClient() {
initRequestOptions ();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
return new RestHighLevelClient(RestClient.builder(getHttpHosts(clientIps, httpPort))
.setHttpClientConfigCallback(
(HttpAsyncClientBuilder httpAsyncClientBuilder) ->
httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
);
}
//不带用户名密码验证
// @Bean
public RestHighLevelClient restClient() {
initRequestOptions ();
return new RestHighLevelClient(RestClient.builder(getHttpHosts(clientIps, httpPort)));
}
}
文章来源:https://www.toymoban.com/news/detail-757469.html
配置完以后使用elastic用户登录kibana,并在左侧能看到安全性这个选项,其中定义用户和角色可以对用户和角色做修改文章来源地址https://www.toymoban.com/news/detail-757469.html
到了这里,关于elasticsearch 7.6.1 的用户名和密码,kibana修改密码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!