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

标签 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>

最佳答案

您还可以在类文件中对密码进行硬编码并分配给 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类的静态方法。

关于java - 在 spring.xml 中加密数据库密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33473656/

相关文章:

java - java中stringbuilder中Fortify的拒绝服务问题

java - 如何更新表模型多行列?

java - Spring多个配置相同接口(interface)的bean

java - Spring in Action 第 6 章,项目未启动

java - Autowiring 在 junit 测试中不起作用

php - Perl/PHP/ColdFusion 中的 TripleDES

encryption - RSA如何保持认证和不可否认性

java - 编译我的客户端时,它说 org.apache.commons.lang3.time.StopWatch 在我的项目中不存在,这是怎么回事?

c - 如何使用 API 并正确初始化 openssl?

java - 如何循环SQL语句?