Skip to main content
 首页 » 编程设计

cassandra中登录集群时,Datastax Java 驱动程序无法针对 Cassandra PasswordAuthenticator 进行身份验证

2024年11月01日8cloudgamer

使用如下所示的小型测试 Java 程序,我可以登录到单个 Cassandra 2.0.1 节点并执行查询,不会出现错误:

$ java -classpath .:cassandra-driver-core-1.0.4.jar DriverTester 127.0.0.1 myusername mypassword 
Got test query result 1ba51260-5b6e-11e3-8f1c-7dd8c1a15c4b 

正如预期的那样,提供错误的密码会失败,并出现“用户名和/或密码不正确”错误。

但是,当使用正确的凭据对属于三节点集群的节点尝试相同的程序时,我收到了一条有点令人困惑的“缺少必需的 key “用户名””消息:

$ java -classpath .:cassandra-driver-core-1.0.4.jar DriverTester cluster-node1 myusername mypassword 
com.datastax.driver.core.exceptions.AuthenticationException: Authentication error on host /62.142.90.104: Required key 'username' is missing 
    at com.datastax.driver.core.Connection.initializeTransport(Connection.java:171) 
    at com.datastax.driver.core.Connection.<init>(Connection.java:132) 
    at com.datastax.driver.core.Connection.<init>(Connection.java:60) 
    at com.datastax.driver.core.Connection$Factory.open(Connection.java:419) 
    at com.datastax.driver.core.ControlConnection.tryConnect(ControlConnection.java:205) 
    at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:168) 
    at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:81) 
    at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:773) 
    at com.datastax.driver.core.Cluster$Manager.access$100(Cluster.java:706) 
    at com.datastax.driver.core.Cluster.<init>(Cluster.java:79) 
    at com.datastax.driver.core.Cluster.<init>(Cluster.java:66) 
    at com.datastax.driver.core.Cluster$Builder.build(Cluster.java:687) 
    at fi.elisa.viihde.stats.DriverTester.openDatastaxSession(DriverTester.java:40) 
    at fi.elisa.viihde.stats.DriverTester.doTest(DriverTester.java:28) 
    at fi.elisa.viihde.stats.DriverTester.main(DriverTester.java:15) 

为什么会发生这种情况?

凭据是正确的,因为向 cqlsh 提供相同的凭据可以正常工作:

$ cqlsh cluster-node1 -u myusername -p mypassword 
Connected to cluster-node1 at xx.xx.xx.xx:9160. 
[cqlsh 4.0.1 | Cassandra 2.0.1 | CQL spec 3.1.1 | Thrift protocol 19.37.0] 
Use HELP for help. 
cqlsh>  

测试程序代码:

import com.datastax.driver.core.Cluster; 
import com.datastax.driver.core.Session; 
 
public class DriverTester { 
    public static void main(String... args) { 
        try { 
            doTest(args); 
        } catch (Throwable t) { 
            t.printStackTrace(); 
            System.exit(1); 
        } 
        System.exit(0); 
    } 
 
    public static void doTest(String... args) { 
        String[] hostnames = args[0].split(","); 
        String username = args[1]; 
        String password = args[2]; 
 
        Cluster.Builder clusterBuilder = Cluster.builder() 
                .addContactPoints(hostnames).withPort(9042) 
                .withCredentials(username, password); 
        Session session = clusterBuilder.build().connect(); 
        System.out.println("Got test query result " + 
                session.execute("select now() from system.local").one().getUUID(0)); 
    } 
} 

请您参考如下方法:

升级到 Datastax 驱动程序 2.0.0-rc2 可以解决此问题,将 Cassandra 从 2.0.1 升级到 2.0.3(或可能是 2.0.2)也可以解决此问题。对于后者,请参阅:https://issues.apache.org/jira/browse/CASSANDRA-6233