javasql-serverspring-data-r2dbc

encrypt parameter in sql server and sprig data r2dbc


I'm using Spring data R2DBC and I can connect in localhost my project with my database using this code:

public class R2dbcConfig extends AbstractR2dbcConfiguration {

    private final ApplicationDatabaseProperties applicationDatabaseProperties;

    @Bean
    @Override
    public ConnectionFactory connectionFactory() {
        return new MssqlConnectionFactory(
                MssqlConnectionConfiguration.builder()
                        .host(applicationDatabaseProperties.getHost())
                        .port(applicationDatabaseProperties.getPort())
                        .username(applicationDatabaseProperties.getUsername())
                        .password(applicationDatabaseProperties.getPassword())
                        .database(applicationDatabaseProperties.getDatabase())
                        .build());
    }

    @Bean
    public DatabaseClient databaseClient(ConnectionFactory connectionFactory) {
        return DatabaseClient.create(connectionFactory);
    }
}

Obvioulsy a applicationDatabaseProperties has parameters of database's connection, but in other enviroment I have a connection string like this: jdbc:sqlserver://[myhost]:1433;database=[mydatabase];user=[myUser];password={your_password_here};encrypt=true;trustServerCertificate=false;hostNameInCertificate=[myhostNameInCertificate];loginTimeout=30;

I've used MssqlConnectionFactory (mssql) and I could view host, port, database, user and password but I couldn't set encrypt parameter in true, is there a way to set this parameter using that class (MssqlConnectionFactory and MssqlConnectionConfiguration).

Thanks for your answers.


Solution

  • Following the documentation you can create ConnectionFactory and Connection objects programmatically.

    @Bean
    @Override
    public ConnectionFactory connectionFactory() {
    
        final ConnectionFactoryOptions options = builder()
                .option(DRIVER, "sqlserver")
                .option(HOST, applicationDatabaseProperties.getHost())
                .option(PORT, applicationDatabaseProperties.getPort())
                .option(USER, applicationDatabaseProperties.getUsername())
                .option(PASSWORD, applicationDatabaseProperties.getPassword())
                .option(DATABASE, applicationDatabaseProperties.getDatabase())
                .option(Option.valueOf("[ANOTHER_OPTION]","[ANOTHER_VALUE"))
                .build();
    
        return ConnectionFactories.get(options);
    }
    

    In this case you can put any other parameret that you need like:

                .option(Option.valueOf("encrypt", Boolean.TRUE))