Cassandra authentication issue after upgrading to Spring boot 2.3.5.RELEASE

Viewed 2983

We are upgrading to Spring boot 2.3.5.RELEASE. Our application has cassandra as one of the databases and we have enabled authentication on the nodes. On trying to connect to the casssandra db after updating the spring boot version, I getting this exception and not able to start up the application.

Authentication error on node <IP:PORT> requires authentication (org.apache.cassandra.auth.PasswordAuthenticator), but no authenticator configured

My cassandra configuration is as follows:

   @Configuration
   @ConfigurationProperties("spring.data.cassandra.keyspaceName")
   @EnableCassandraRepositories(basePackages = { "com.company.project.feature.cassandra.repo" }, 
    cassandraTemplateRef = "dbTemplate")
  public class Config extends AbstractCassandraConfiguration {

@Autowired
ServerProperties serverProps;

protected String contactPoints;
protected Integer port;
protected String keyspaceName;
protected String localDataCenter;


@Override
public String getContactPoints() {
    return contactPoints;
}

/**
 * @param contactPoints
 */
public void setContactPoints(String contactPoints) {
    this.contactPoints = contactPoints;
}

/**
 * @param keyspaceName
 */
public void setKeyspaceName(String keyspaceName) {
    this.keyspaceName = keyspaceName;
}

@Override
public String getKeyspaceName() {
    return keyspaceName;
}

@Override
public int getPort() {
    return port;
}

public void setLocalDataCenter(String localDataCenter) {
    this.localDataCenter = localDataCenter;
}

@Override
public String getLocalDataCenter() {
    return localDataCenter;
}

public void setPort(Integer port) {
    this.port = port;
}

@Bean(name = "dbSession")
@Primary
public CqlSession session() {

    String containerIpAddress = getContactPoints();
    int containerPort = getPort();
    InetSocketAddress containerEndPoint = new InetSocketAddress(containerIpAddress, containerPort);
    
    CqlSessionBuilder builder = CqlSession.builder().withLocalDatacenter(getLocalDataCenter()).addContactPoint(containerEndPoint)
    .withAuthCredentials(serverProps.getCassandraUserName(), serverProps.getCassandraPassword())
    .withKeyspace(getKeyspaceName());
    
    
    return builder.build();
}


@Bean("dbOperations")
public CassandraOperations cassandraOperationsB() {
    return new CassandraTemplate(session(), cassandraConverter());
}

@Primary
@Bean(name = "dbTemplate")
@Override
public CassandraAdminTemplate cassandraTemplate() {
    return new CassandraAdminTemplate(session(), cassandraConverter());
}

}

I am providing the username and password while building the CqlSession but still facing this exception. Is there anything I am doing incorrectly?

1 Answers

in older Spring data versions you may use: CassandraClusterFactoryBean . But here is an example for future if some one else stumbles with spring data cassandra 3.0.1 with user name and password authentication as spring data document if you scroll to the end of the page then you will see CassandraClusterFactoryBean is removed, instead of we have now CqlSessionFactoryBean. So sample code with looks like

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import org.springframework.data.cassandra.config.CqlSessionFactoryBean;

@Configuration
@EnableCassandraRepositories(basePackages = {"com.some.package"})
@ConfigurationProperties(prefix = "spring.data.cassandra")
@Getter
@Setter
public class CassandraConfig extends AbstractCassandraConfiguration {

    private String keyspaceName;
    private String contactPoints;
    private int port;
    private String localDataCenter;
    private String username;
    private String password;

    @Bean
    @Override
    public CqlSessionFactoryBean cassandraSession() {
        CqlSessionFactoryBean cassandraSession = super.cassandraSession();//super session should be called only once
        cassandraSession.setUsername(username);
        cassandraSession.setPassword(password);
        return cassandraSession;
    }

}
Related