如何在你的应用中使用Jasypt来保护你的数据库用户名和密码

“任何时候都不应该明文显示和存储密码”,我想这个原则是一个稍微有点安全常识的人都懂的,那么在Java应用中,如何最简单最方便地保护你的应用的数据呢?

本文我们以数据库的用户名和密码为例子,来讲解在APDPlat中我们如何使用开源项目Jasypt来实现加解密。

首先,我们引入依赖库,使用Maven方式如下:

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.5</version>
</dependency>

其次,接下来我们看看如何加密:

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;

/**
 *把密文放到配置文件中的时候要注意:
 * ENC(密文)
 * @author 杨尚川
 */
public class ConfigEncryptUtils {
    public static void main(String[] args){
        //加密工具
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //加密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        //自己在用的时候更改此密码
        config.setPassword("apdplat");
        //应用配置
        encryptor.setConfig(config);
        String plaintext="root";
        //加密
        String ciphertext=encryptor.encrypt(plaintext);
        System.out.println(plaintext + " : " + ciphertext);
    }
}

运行输出结果如下:

    root : azL9Cyp9H62r3eUgZ+TESw==

再次,接下来我们看看如何解密:

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;

/**
 *把密文放到配置文件中的时候要注意:
 * ENC(密文)
 * @author 杨尚川
 */
public class ConfigEncryptUtils {
    public static void main(String[] args){
        //加密工具
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //加密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        //自己在用的时候更改此密码
        config.setPassword("apdplat");
        //应用配置
        encryptor.setConfig(config);
        String ciphertext="azL9Cyp9H62r3eUgZ+TESw==";
        //解密
        String plaintext=encryptor.decrypt(ciphertext);
        System.out.println(ciphertext + " : " + plaintext);
    }
}

运行输出结果如下:

   azL9Cyp9H62r3eUgZ+TESw== : root

从上面我们可以看到,加密和解密的代码的唯一差别是encrypt和decrypt。

最后我们来看看如何和spring集成,在spring配置文件中加入如下配置,这样当spring读取到的值是加过密的值就会自动解密,那么spring是如何判断一个值是否加密过了呢?是根据特定的前缀ENC(和后缀)来判断的。

<!-- Spring属性文件解密组件  -->
<bean id="propertyConfigurer" class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer">
    <constructor-arg ref="configurationEncryptor" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:/org/apdplat/config.properties</value>
            <value>classpath:config.local.properties</value>
            <value>classpath:/org/apdplat/db.properties</value>
            <value>classpath:db.local.properties</value>
        </list>
    </property>
</bean>

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

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

配置好spring后,我们就可以在指定的配置文件classpath:config.local.properties中使用加密过后的数据库连接使用的用户名和密码了,别忘了特定的前缀ENC(和后缀)哦:

#数据库配置文件

#mysql
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/${module.short.name}?useUnicode=true&characterEncoding=UTF-8&createDatabaseIfNotExist=true&autoReconnect=true
db.username=ENC(qPWWR8YEmQE63EYywEBKaQ==)
db.password=ENC(qPWWR8YEmQE63EYywEBKaQ==)
jpa.database=MYSQL
db.backup.command=mysqldump  -u${db.username} -p${db.password} ${module.short.name}
db.restore.command=mysql -u${db.username} -p${db.password} ${module.short.name}

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个开源的Java框架,可帮助开发者更快速地构建应用程序。Jasypt3是Spring Boot一种常用的加密库,可用于加密敏感数据,如数据库密码。下面简要介绍如何使用Spring Boot和Jasypt3来加密数据库密码: 1. 添加依赖:在pom.xml添加Jasypt3的依赖,如下所示: ```xml <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> ``` 2. 配置加密算法和密钥: 在Spring Boot的配置文件(application.properties或application.yml)添加以下配置: ```properties jasypt.encryptor.algorithm=算法 jasypt.encryptor.password=密钥 ``` 3. 加密数据库密码: 在配置文件使用Jasypt3提供的加密语法将数据库密码进行加密。例如,假设我们要加密密码是"password",可以使用以下语法: ```properties encryptor.encrypt(密码) ``` 4. 使用加密密码: 在项目的数据源配置文件(如application.properties)使用加密后的密码。例如: ```properties spring.datasource.username=用户名 spring.datasource.password=ENC(加密密码) ``` 5. 运行应用程序: 启动Spring Boot应用程序,它将自动使用配置的密钥解密密码,然后使用解密后的密码连接数据库。 通过以上步骤,我们可以使用Spring Boot和Jasypt3来实现数据库密码加密。这样可以保护敏感数据的安全性,同时提供了一种方便的方法来管理加密密钥和加密算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值