Vir*_*raj 7 java spring connection-pooling apache-commons-dbcp hikaricp
我不知道,如何针对以下场景寻求解决方案.
我们有一个新的要求,即使用Jasypt库或其他算法加密,也要从属性中删除DB Password.
我们需要从Cyberark动态获取密码,而不是将密码存储在属性或LDAP中.
密码可能在一两天或一周或一个月内到期.这完全取决于密码过期策略.
我们有多个项目.有些是基于网络的,有些是独立的.我们想写一个通用的解决方案.
如何覆盖getConnection任何数据源的方法,如Spring数据源,Apache Basic数据源(它支持扩展类),C3P0,DBCP或HikariCP,而不影响他们的行为并在点击之前设置密码super.getConnection()?
super.getConnection(); // Here max attempt will be 3
Run Code Online (Sandbox Code Playgroud)
Spring支持方法替换,但我不知道会对连接池框架产生什么影响.
如果您需要更多详细信息,请告诉我们.
要解决您的问题,您可以使用spring-cloud-context库及其@RefreshScope注释。此外,您还需要进行一些发展。
1)你需要一个特殊的观察者bean来监视密码是否被更改。它将是这样的:
@Service
public class Watcher {
private final ContextRefresher refresher;
public Watcher(ContextRefresher refresher) {
this.refresher = refresher;
}
@Scheduled(fixedDelay = 10000L)
public void monitor() {
if (/* smth changed*/) {
refresher.refresh();
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当您调用时,refresher.refresh();所有带有注释的 bean@RefreshContext将在第一次访问它们后被释放并重新创建。
2) 使用@RefreshContext注释来注释您的数据源 bean。3) 您必须提供密码才能使用@ConfigurationProperties注释进行访问。您将需要创建 SourceLocator。会是这样的
@Order(0)
public class SourceLocator implements PropertySourceLocator {
@Override
public PropertySource<?> locate(Environment environment) {
//Load properties to hash map
return new MapPropertySource("props", new HashMap<>());
}
}
Run Code Online (Sandbox Code Playgroud)
另外,创建一个文件spring.factories并将以下数据放在那里:
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.test.YourSourceLocator
Run Code Online (Sandbox Code Playgroud)
4)创建属性类,您的数据库通行证将在其中保存和刷新。
@RefreshScope
@ConfigurationProperties(prefix="your.prefix")
public class Properties {
private String dbPassword;
}
Run Code Online (Sandbox Code Playgroud)
将此 bean 自动连接到您创建数据源的配置并使用其中的密码。
| 归档时间: |
|
| 查看次数: |
477 次 |
| 最近记录: |