Spring 使用密码 lease/renew 启动 JDBC(如在 Vault 中)

Spring boot JDBC with password lease/renew (as in Vault)

Hashicorp 的 Vault 可以设置为按需提供数据库密码;每个密码在续订前可以使用一段时间(例如 1 小时),并且可以设置最长使用期限,超过该期限后必须丢弃密码并获取新密码。

在 Spring 引导中,JDBC 连接在应用程序启动时配置,并且假定 JDBC 密码编码在 application.properties 文件中(或,或者,在应用程序 bootstrap 时间通过 Spring Cloud Config 或等效设备获得)并永久使用。

问题:当由于密码过期导致连接尝试失败时,我如何在 Spring 引导中通过访问 Vault 来重置 JDBC 密码?

有没有办法设置在由于旧密码导致连接失败时调用某种处理程序并将其重置为新值?

查看 GitHub 上可用的这个开源项目;我认为这可能正是您要寻找的。注:目前看来,这是一个Spring Cloud Incubator项目(未来有可能成为Spring官方认可的开源库),贡献者只有三个。您必须看看它是否 "reliable enough" 适合您的需要。

https://github.com/spring-cloud-incubator/spring-cloud-vault-config

--- 这是有用信息的快速摘要 ---

将以下依赖项添加到 pom.xml:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-vault-starter-config</artifactId>
    <version>x.y.z</version>
</dependency>

创建标准 Spring 引导应用程序 - 提供的示例只是一个主应用程序 class:

@SpringBootApplication
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

When it runs it will pick up the external configuration from the default local Vault server on port 8200 if it is running. To modify the startup behavior you can change the location of the Vault server using bootstrap.properties (like application.properties but for the bootstrap phase of an application context), e.g.

bootstrap.yml:

spring.cloud.vault:
    host: localhost
    port: 8200
    scheme: http
    connection-timeout: 5000
    read-timeout: 15000

host 设置 Vault 主机的主机名。主机名将用于 SSL 证书验证

port 设置 Vault 端口

scheme 将方案设置为 http 将使用纯 HTTP。支持的方案是 http 和 https。

connection-timeout 以毫秒为单位设置连接超时

read-timeout 以毫秒为单位设置读取超时