Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springboot Cassandra - CqlSessionFactoryBean with SSL

Small question regarding how to connect to a Cassandra cluster that is SSL enabled please.

Currently, I am connecting to a Cassandra cluster that is not SSL enabled by doing the following, and it is working perfectly fine.

@Configuration
public class BaseCassandraConfiguration extends AbstractReactiveCassandraConfiguration {

    @Value("${spring.data.cassandra.username}")
    private String username;
    @Value("${spring.data.cassandra.password}")
    private String passPhrase;
    @Value("${spring.data.cassandra.keyspace-name}")
    private String keyspace;
    @Value("${spring.data.cassandra.local-datacenter}")
    private String datacenter;
    @Value("${spring.data.cassandra.contact-points}")
    private String contactPoints;
    @Value("${spring.data.cassandra.port}")
    private int    port;
    
    @Bean
    @NonNull
    @Override
    public CqlSessionFactoryBean cassandraSession() {
        final CqlSessionFactoryBean cqlSessionFactoryBean = new CqlSessionFactoryBean();
        cqlSessionFactoryBean.setContactPoints(contactPoints);
        cqlSessionFactoryBean.setKeyspaceName(keyspace);
        cqlSessionFactoryBean.setLocalDatacenter(datacenter);
        cqlSessionFactoryBean.setPort(port);
        cqlSessionFactoryBean.setUsername(username);
        cqlSessionFactoryBean.setPassword(passPhrase);
        return cqlSessionFactoryBean;
    }

I have another Cassandra cluster, that is SSL enabled.

I was expecting to see something like cqlSessionFactoryBean.setSSLEnabled(true), something like that. Unfortunately, it seems there is no such.

May I ask what is the proper way to set up this bean in order to connect to a Cassandra with SSL please?

Thank you.

like image 591
PatPatPat Avatar asked Oct 11 '25 16:10

PatPatPat


1 Answers

The CqlSessionFactoryBean doesn't have a method for SSL connections, so you might have to change it and use CqlSession instead.

SSLContext sslContext = ...
CqlSession session = CqlSession.builder()
    .withSslContext(sslContext)
    .build();

or

SslEngineFactory yourFactory = ...
CqlSession session = CqlSession.builder()
    .withSslEngineFactory(yourFactory)
    .build();
like image 110
Tavo Sanchez Avatar answered Oct 14 '25 06:10

Tavo Sanchez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!