encrypt参数

g9icjywg  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(429)

我使用的是spring data r2dbc,我可以使用以下代码在localhost中连接我的项目和数据库:

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);
    }
}

很明显,applicationdatabaseproperties有数据库连接的参数,但在其他环境中,我有这样的连接字符串: jdbc:sqlserver://[myhost]:1433;database=[mydatabase];user=[myUser];password={your_password_here};encrypt=true;trustServerCertificate=false;hostNameInCertificate=[myhostNameInCertificate];loginTimeout=30; 我使用了MSSQLSconnectionFactory(mssql),我可以查看主机、端口、数据库、用户和密码,但我无法将encrypt参数设置为true,是否有方法使用该类(MSSQLSconnectionFactory和MSSQLSconnectionConfiguration)设置此参数。
谢谢你的回答。

rseugnpd

rseugnpd1#

按照您可以创建的文档 ConnectionFactory 以及 Connection 以编程方式创建对象。

@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);
}

在这种情况下,您可以输入任何其他需要的参数,如:

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

相关问题