Hashicorp 的 Vault可设置按需提供数据库密码;每个密码在更新之前可以使用一定的“租用”期限(例如 1 小时),并且可以设置最长使用期限,在此之后密码必须被丢弃并获取新密码。
在 Spring Boot 中,JDBC 连接是在应用程序启动时配置的,并且假设 JDBC 密码是在 application.properties 文件中编码的(或者,在应用程序引导时通过 Spring Cloud Config 或等效文件获取),并且永远使用。
问题:当连接尝试因密码过期而失败时,如何在 Spring Boot 中实现一种通过访问 Vault 重置 JDBC 密码的方法?
有没有办法设置某种处理程序,当由于旧密码导致连接失败时调用某种处理程序,并将其重置为新值?
最佳答案
查看 GitHub 上提供的这个开源项目;我想这可能正是您正在寻找的。注:从表面上看,这是一个Spring Cloud Incubator项目( future 有可能成为Spring官方认可的开源库),贡献者只有三个。您必须看看它是否“足够可靠”以满足您的需求。
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 Boot 应用程序 - 提供的示例只是一个主应用程序类:
@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
设置读取超时(以毫秒为单位)
关于spring-boot - 带密码租用/续订的 Spring boot JDBC(如 Vault 中所示),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38579168/