java spring - 覆盖从属性文件接收的属性值

标签 java spring encryption properties

我有一个场景,我从属性文件中获取编码的密码值。我有自己的解密实现,因此想要从我的 java 类中解密密码,然后想要在其引用的地方进一步使用该解密值。

例如 <bean id="myDataSource" class="org.apache.common.dbcp.BasicDataSource" ... // more attributed set from properties like user, hostname etc. p:password="${myPropertey.password}" >

上面的代码我需要实现如下所示的内容,针对特定于密码属性,因为其余属性都很好,但密码需要在使用之前解密。

(下面的实现是错误的,但我刚刚提到是为了提供更多和清晰的想法)

<bean id="myDataSource" class="org.apache.common.dbcp.BasicDataSource" ... // more properties p:password="myDecryptBean.decryptMyPassword(${myPropertey.password})" >

基本上,我需要解密从属性文件中获得的密码,然后再使用它建立数据库连接。

感谢您的时间和任何帮助!!

最佳答案

由于您使用的是 Spring,我强烈建议您查看 PropertyResourceConfigurer 类。 请参阅Official Documentation .

特别要查看 convertProperty(String propertyName, String propertyValue) 方法,并考虑创建一个重写它​​的类。然后,您将能够将自定义解密逻辑添加到重写方法中,该方法将在每次 Spring 访问该属性时运行。

直接来自文档:

Allows for configuration of individual bean property values from a property resource, i.e. a properties file. Useful for custom config files ... that override bean properties configured in the application context.

关于java spring - 覆盖从属性文件接收的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35231414/

相关文章:

c# - 加密大字节数组的高效加密算法

java - 函数运行时无法启动 - 无法加载类型 'Microsoft.Azure.ServiceBus.Management.ManagementClient'

java - 安卓代码质量

java - 在 JBOSS 6.4 EAP + spring + java 应用程序中配置 Oracle Wallet

生成唯一且随机的六个字母数字代码的 Java 程序

ubuntu - 将ffmpeg的输出重定向到openssl

java继承与泛型的比较

java - Apache cxf 3.1 SOAP 请求错误:“编码错误:INVALID_CHARACTER_ERR:指定了无效或非法的XML字符

java - 如何在 Spring Boot JUnit 测试中排除 *AutoConfiguration 类?

java - @ExceptionHandler 不会捕获从 Spring Formatters 抛出的异常