Spring4 中使用 jasypt 加密数据密码

1.加载类jar

org.jasypt
jasypt
1.9.2



org.jasypt
jasypt-spring31
1.9.2


2.使用工具生成加密后的密码

package gjp.test;

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

/**
* Created by gjp on 2017/7/5.
* 把密文放到配置文件中的时候要注意:
* ENC(密文)
*/
public class ConfigEncryptUtils {

/**jasypt 加密算法
* Pwd.
*/
public static void enpwd(){
//加密工具
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
//加密配置
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm("PBEWithMD5AndDES");
//自己在用的时候更改此密码
config.setPassword("123456");
//应用配置
encryptor.setConfig(config);
String plaintext="shiro123";
//加密
String ciphertext=encryptor.encrypt(plaintext);
System.out.println(plaintext + " : " + ciphertext);
}


/**jasypt 解密算法
* De pwd.
*/
public static void dePwd(){

//加密工具
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
//加密配置
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm("PBEWithMD5AndDES");
//自己在用的时候更改此密码
config.setPassword("123456");
//应用配置
encryptor.setConfig(config);
String ciphertext="PovZgl9pg6IXlalIyavYG6HQBq4NyM96";
//解密
String plaintext=encryptor.decrypt(ciphertext);
System.out.println(ciphertext + " : " + plaintext);
}
public static void main(String[] args){


ConfigEncryptUtils.enpwd();
}
}


生成加密后的密码: rgsjYM2QG/lSuSUSuIvlXyD51NfWaEWb

3.配置文件: jdbcOracle_test.properties

#jdbc settings
jdbc.driverUrl=jdbc:oracle:thin:@192.168.6.24:1521:YUNBOCE
jdbc.username=shiro
jdbc.password=ENC(rgsjYM2QG/lSuSUSuIvlXyD51NfWaEWb)
#shiro123
jdbc.driver=oracle.jdbc.OracleDriver
#注意加密后的密码一定要使用ENC()

配置spring



xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">


class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">










class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">




class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">



classpath:jdbcOracle_test.properties







init-method="init" destroy-method="close">

${jdbc.driver}

/>

/>

你可能感兴趣的:(spring)