使用密码认证,无法连接Java客户端到Cassandra

7
我在我的Macbook上进行了Datastax企业版的默认安装。我能够创建我的键空间并设置所有应用程序,包括使用solr。
我正在尝试开发一组步骤,以为我们的开发群集启用密码身份验证。
到目前为止,我已经更新了"/usr/local/dse/resources/cassandra/conf/cassandra.yaml"并更改了以下属性:
authenticator: PasswordAuthenticator
authorizer: CassandraAuthorizer

我重新启动了节点,可以使用cqlsh登录并查询我的keyspace:
cqlsh -u cassandra -p cassandra

此时我尝试在Session构建器上设置凭证: 主机是:cassandra.host=localhost
Session session = keyspaceToSessionMap.get(keyspace);
    if( session == null){
        Cluster cluster = Cluster.builder().addContactPoints(hosts)
                .withCredentials(username, password)
                //.withSSL()
                .build();
        session = cluster.connect(keyspace);
        keyspaceToSessionMap.put(keyspace,session);
    }

然而,我无法成功连接。因此,我添加了一个新的用户,并能够再次通过cqlsh登录,但仍无法让Java驱动程序连接。
cqlsh -u username -p password
Connected to LocalCluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 2.1.8.689 | DSE 4.7.3 | CQL spec 3.2.0 | Native protocol v3]

我正在使用Gradle引入'com.datastax.cassandra:cassandra-driver-dse:2.1.9'驱动。
我总是得到以下的堆栈跟踪,通过调试可以看到用户名和密码已正确设置:
Caused by: com.datastax.driver.core.exceptions.AuthenticationException: Authentication error on host localhost/127.0.0.1:9042: Username and/or password are incorrect
at com.datastax.driver.core.Connection$8.apply(Connection.java:376)
at com.datastax.driver.core.Connection$8.apply(Connection.java:346)

这似乎应该很简单,但我被难住了。
与Cassandra驱动程序相关的我的依赖关系图如下:
+--- com.datastax.cassandra:cassandra-driver-dse:2.1.9
|    \--- com.datastax.cassandra:cassandra-driver-core:2.1.9 -> 2.1.8
|         +--- io.netty:netty-handler:4.0.27.Final
|         |    +--- io.netty:netty-buffer:4.0.27.Final
|         |    |    \--- io.netty:netty-common:4.0.27.Final
|         |    +--- io.netty:netty-transport:4.0.27.Final
|         |    |    \--- io.netty:netty-buffer:4.0.27.Final (*)
|         |    \--- io.netty:netty-codec:4.0.27.Final
|         |         \--- io.netty:netty-transport:4.0.27.Final (*)
|         +--- com.google.guava:guava:14.0.1 -> 18.0
|         \--- com.codahale.metrics:metrics-core:3.0.2
|              \--- org.slf4j:slf4j-api:1.7.5 -> 1.7.12

英译中:

我创建了以下通过的测试。

Cluster cluster = Cluster.builder().addContactPoints("localhost")
                    .withCredentials("username", "password")
                    //.withSSL()
                    .build();
            Session session = cluster.connect("keyspace");
            Assert.assertNotNull(session);

我所能看出的这两者之间的唯一区别是,“localhost”现在是一个常量,而不再是大小为1的数组。

你能提供一个更完整的代码示例吗?你提供的所有内容对我来说都是正确的,但也许代码中有一些细微之处可以揭示问题。 - Andy Tolbert
1个回答

8

发现我的代码末尾有一个空格,这就是根本原因。

Cluster cluster = Cluster.builder().addContactPoints(hosts)
                .withCredentials(username.trim(), password.trim())
                //.withSSL()
                .build();

啊,这就解释了!谢谢分享解决方案,让我有一瞬间担心驱动程序可能存在错误 :) - Andy Tolbert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接