在spring.xml中加密数据库密码

时间:2015-11-02 08:42:22

标签: java spring encryption

我正在尝试加密我在spring.xml文件中存储的密码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
    xmlns:context="http://www.springframework.org/schema/context">

<context:annotation-config/>


<context:component-scan base-package="com.prakash" />


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" alue="jdbc:postgresql://localhost:5432/HibernateStudy" />
    <property name="username" value="postgres" />
    <property name="password" value="mypassword" />
    <property name="initialSize" value="2" />
    <property name="maxActive" value="5" />
</bean>

我用谷歌搜索我知道这可以使用jasypt Package。但不知何故,我无法将其集成到我的应用程序中。 谁能帮帮我吗。

我知道我可以用<property name="password" value="mypassword" />

替换行<property name="password" value= ENC(2Cu5057YZbQcUQ8cUQQMinzMDD2GeSXh) />

那之后我应该做什么?我是否必须在java文件或其他东西中创建任何对象.. 任何人都可以给我这个jasypt包的工作流程及其x.ml文件和java文件。

谢谢

[更新] 大家好, 现在我很想读取使用加密密码连接数据库,但用于解密密码的密码是从环境变量传递的。有没有其他方法可以通过它。

我更新的spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">

<context:annotation-config/>


<context:component-scan base-package="com.prakash" />



<bean id="environmentVariablesConfiguration"
 class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
<property name="algorithm" value="PBEWithMD5AndDES" />
<property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD" />
</bean>

                                                                   -->
<bean id="configurationEncryptor"
 class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="config" ref="environmentVariablesConfiguration" />
</bean>


<bean id="propertyConfigurer" class="org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer">
<constructor-arg ref="configurationEncryptor" />
<property name="locations">
 <list>
   <value>database.properties</value>
 </list>
</property>

</bean>

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>database.properties</value>
    </property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/HibernateStudy" />
    <property name="username" value="${connection.username}" />
    <property name="password" value="${connection.encrypt}" />
    <property name="initialSize" value="2" />
    <property name="maxActive" value="5" />



</bean>

1 个答案:

答案 0 :(得分:-2)

您也可以在类文件中对密码进行硬编码并分配给bean。

    .. 
<bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
                   <property name="algorithm" value="PBEWithMD5AndDES"/>
                   <property name="password" value="#Key.keyValue}" />
         </bean>
    ..

其中Key.keyValue是Key类的静态方法。