将加密属性传递给 spring 上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8933067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing encrypted properties to spring context
提问by Fabio
I never seen this but I wondering if somebody has come across. Having a web server which access a database. I want to pass the database password encrypted and have spring context decrypting it before setting the datasource. I know the spring security can do some of this like using a salt file in the web server, etc.
我从未见过这个,但我想知道是否有人遇到过。拥有访问数据库的 Web 服务器。我想传递加密的数据库密码,并在设置数据源之前让 spring 上下文对其进行解密。我知道 spring 安全可以做一些这样的事情,比如在 web 服务器中使用盐文件等。
The challenge here is that I don't want to give a clear user,password,url to the web server team. Just an encrypted password and have spring decrypted before using it.
这里的挑战是我不想给 web 服务器团队一个明确的用户、密码、url。只是一个加密的密码,并且在使用它之前已经解密。
Is there something like this already? I know I could code something but is it already done?
已经有这样的事情了吗?我知道我可以编码一些东西,但它已经完成了吗?
Thanks
谢谢
回答by Abhishek Ranjan
By using an org.jasypt.properties.EncryptableProperties object, an application would be able to correctly read and use a .properties file like this:
通过使用 org.jasypt.properties.EncryptableProperties 对象,应用程序将能够正确读取和使用 .properties 文件,如下所示:
datasource.driver=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost/reportsdb
datasource.username=reportsUser
datasource.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)
Note that the database password is encrypted (in fact, any other property could also be encrypted, be it related with database configuration or not).
请注意,数据库密码已加密(实际上,任何其他属性也可以加密,无论是否与数据库配置相关)。
More information :
更多信息 :
回答by Fabio
I actually found exactly what I was looking for in this thread:
我实际上在这个线程中找到了我正在寻找的东西:
How to use encrypted password in apache BasicDataSource?Here are the details from jasyp http://www.jasypt.org/spring3.html
如何在 apache BasicDataSource 中使用加密密码?以下是 jasyp http://www.jasypt.org/spring3.html的详细信息
回答by Kayalvizhi NoorulAmeen
This problem and solution to it is explained here..(link)
这里解释了这个问题及其解决方案..(链接)
db.Properties.
db. 属性。
#driverClassName=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@localhost:1521:XE
#username=ITEM_INVENTORY
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ITEM_INVENTORY?zeroDateTimeBehavior=convertToNull
username=root
Encrypt db.Properties
##password=cGFzc3dvcmQ= password=cm9vdA==
加密 db.Properties
##password=cGFzc3dvcmQ= password=cm9vdA==
The spring beans configuration for the datasource would look like this (here you may use only password part)
数据源的 spring bean 配置看起来像这样(这里你可以只使用密码部分)
spring-beans.xml
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="db#[driverClassName]" /> <property name="url" value="db#[url]" /> <property name="username" value="db#[username]" /> <property name="password" value="encryptedDb#[password]" /> </bean> <bean id="dbPropertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:db.properties</value> </list> </property> <property name="placeholderPrefix" value="db#[" /> <property name="placeholderSuffix" value="]" /> </bean> <bean id="encryptedDbPropertyPlaceholder" class="com.inventory.api.util.DecryptPropertyConfigurer"> <property name="locations"> <list> <value>classpath:encryped_db.properties</value> </list> </property> <property name="placeholderPrefix" value="encryptedDb#[" /> <property name="placeholderSuffix" value="]" /> </bean>
spring-beans.xml
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="db#[driverClassName]" /> <property name="url" value="db#[url]" /> <property name="username" value="db#[username]" /> <property name="password" value="encryptedDb#[password]" /> </bean> <bean id="dbPropertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:db.properties</value> </list> </property> <property name="placeholderPrefix" value="db#[" /> <property name="placeholderSuffix" value="]" /> </bean> <bean id="encryptedDbPropertyPlaceholder" class="com.inventory.api.util.DecryptPropertyConfigurer"> <property name="locations"> <list> <value>classpath:encryped_db.properties</value> </list> </property> <property name="placeholderPrefix" value="encryptedDb#[" /> <property name="placeholderSuffix" value="]" /> </bean>
And so on.. please refer given link for more information..
等等..请参阅给定的链接了解更多信息..

