Spring Data Elasticsearch 和 x-pack 用户名/密码验证链接

使用Spring Data Elasticsearch链接elasticsearch时,正常状况下只须要在application.properites文件中添加以下配置便可链接:java

spring.data.elasticsearch.repositories.enabled = true
spring.data.elasticsearch.cluster-name = es-cluster
spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300

以看到Spring Data Elasticsearch链接elasticsearch很简单。node

上面说的是正常状况,可是有些状况下链接须要验证。好比安装x-pack(旧版本的Shield、Marvel等)以后,访问elasticsearch就须要验证(未开启匿名访问),默认用户名/密码是:elastic/changeme,参考官方文档了解更多细节。spring

若是不作相应的修改就会报错app

org.elasticsearch.transport.RemoteTransportException: [i9Vl_pG][10.111.27.202:9300][cluster:monitor/nodes/liveness]
Caused by: org.elasticsearch.ElasticsearchSecurityException: missing authentication token for action [cluster:monitor/nodes/liveness]
	at org.elasticsearch.xpack.security.support.Exceptions.authenticationError(Exceptions.java:36) ~[na:na]
	at org.elasticsearch.xpack.security.authc.DefaultAuthenticationFailureHandler.missingToken(DefaultAuthenticationFailureHandler.java:74) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$AuditableTransportRequest.anonymousAccessDenied(AuthenticationService.java:541) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lambda$handleNullToken$17(AuthenticationService.java:357) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.handleNullToken(AuthenticationService.java:362) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.consumeToken(AuthenticationService.java:277) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lambda$extractToken$7(AuthenticationService.java:249) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.extractToken(AuthenticationService.java:266) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lambda$null$0(AuthenticationService.java:201) ~[na:na]
	at org.elasticsearch.action.ActionListener$1.onResponse(ActionListener.java:60) ~[elasticsearch-5.6.8.jar:5.6.8]
	at org.elasticsearch.xpack.security.authc.TokenService.getAndValidateToken(TokenService.java:239) ~[na:na]

在这样的状况下,Spring Data Elasticsearch 该如何设置才能链接到 elasticsearch?目前最新版的 Spring Data Elasticsearch 2.1.6.Release 还不能经过配置的形式来完成。那要如何作,更换TransportClient便可!elasticsearch

修改依赖

<repository>
    <id>elasticsearch-releases</id>
    <url>https://artifacts.elastic.co/maven</url>
    <releases>
        <enabled>true</enabled>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>
...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>x-pack-transport</artifactId>
    <version>5.5.0</version>
</dependency>

x-pack-transport 的版本号保持与elasticsearch一致。maven

从新实例化 TransportClient

@Bean
public TransportClient transportClient() throws UnknownHostException {
    TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
            .put("cluster.name", "es-cluster")
            .put("xpack.security.user", "elastic:changeme")
            .build())
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
    return client;
    }

就这么多,以前属性文件中的配置能够删除了,这样就能保证链接验证经过。spring-boot

相关文章
相关标签/搜索