How to connect to Cassandra 5.1 with Authentication from Java

1k views Asked by At

I'm trying to connect to the DSE 5.1 Cluster with Authentication from Java client. The Cassandra driver I have used is

<dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-core</artifactId>
        <version>3.1.4</version>
    </dependency>

Have added a simple Test which tries to connect to a particular KeySpace:

@Test
public void testConnection() {
    String user = "****";
    String pswd = "*******";
    String host = "********"
    Cluster cluster = Cluster.builder().addContactPoints(host)
            .withPort(9042)
            .withCredentials(user.trim(), pswd.trim())
            .build();
    Session session = cluster.connect(QueryBuilder.quote("TestKS"));
    Assert.assertNotNull(session);
}

I end up with com.datastax.driver.core.exceptions.AuthenticationException

From the terminal, I'm able to connect to this node with the same credentials but from my Java code, I'm unable to get past this exception.

Stacktrace

com.datastax.driver.core.exceptions.AuthenticationException: Authentication error on host /<host>:9042: Failed to login. Please re-try.
    at com.datastax.driver.core.Connection$8.apply(Connection.java:390)
    at com.datastax.driver.core.Connection$8.apply(Connection.java:359)
    at com.google.common.util.concurrent.AbstractTransformFuture$AsyncTransformFuture.doTransform(AbstractTransformFuture.java:211)

Please advise on what I'm missing.

1

There are 1 answers

0
Erick Ramirez On BEST ANSWER

You didn't provide the full stack trace so it's difficult to know exactly why your code is failing.

In any case Java driver 3.1.4 is 4 years old and if you're just starting out with a new app, we recommend that you use the latest Java driver 4 which is compatible with DSE 5.1 (by the way, there is no Cassandra 5.1).

You can configure authentication in the driver programatically but the recommended way is to specify the credentials in a configuration file. For example:

datastax-java-driver {
  advanced.auth-provider {
    class = PlainTextAuthProvider
    username = user
    password = pass
  }
}

For details, see the Authentication page in the driver docs.

If you're new to developing apps for Cassandra, we have sample apps on Astra where you can see the full source code so you can get started really quickly. Cheers!