Java SDK authentication

Hi All,

I am new to Couchbase and need help in Java SDK authentication.
I have downloaded CB server comminity edition 6.0 and installed the same on my local machine.

I am trying to connect to the server using java SDK. I refer to following online documentation for the same.

https://docs.couchbase.com/java-sdk/current/start-using-sdk.html

    // Initialize the Connection
    Cluster cluster = CouchbaseCluster.create("localhost");
    cluster.authenticate("username", "password");
    Bucket bucket = cluster.openBucket("bucketname");

It seems the new SDK 2.7 needs Authenticator object to be passed as a parameter to cluster.ahthenticate method. Where as the docuemntation about states passing as user name and passwprd as string parameters. I could not find any documentation or example on how to use Authenticator object to pass on the authenticate method.

Can someone help?

@dilipapte The above code you pasted looks like a valid way to authenticate against your cluster. Is it not working for you?
If you must, you can use a PasswordAuthenticator object, see https://github.com/couchbase/couchbase-java-client/blob/8424282fb1d609d8f5df03341077def985c45c9b/src/integration/java/com/couchbase/client/java/auth/PasswordAuthenticatorTest.java#L78

Thanks David for your reply. No it gives an error. I made following change in the code and it worked.

bucket = cbCluster.authenticate(new Authenticator() {
@Override
public List getCredentials(CredentialContext credentialContext, String s) {
Credential cr = new Credential(bucketName,password);
List lst = new ArrayList<>();
lst.add(cr);
return lst;
}

        @Override
        public boolean isEmpty() {
            return false;
        }
    }).openBucket(bucketName);

@dilipapte The original code should work just fine. If it didn’t, then there is something else going on. I don’t see how changing the code would make it work. If you are happy with where you at now, then that’s good. Otherwise, if you want this looked into, I would suggest you provide the logs showing the error you get.