任何时候都不应该明文显示和存储密码”,我想这个原则是一个稍微有点安全常识的人都懂的,那么在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}