1.加载类jar
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jasypt/jasypt-spring31 -->
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt-spring31</artifactId>
<version>1.9.2</version>
</dependency>
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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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
">
<bean id="environmentVariablesConfiguration"
class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
<property name="algorithm" value="PBEWithMD5AndDES" />
<property name="password" value="123456" />
<!-- <property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD" />
密钥passwordEnvName使用环境变量APP_ENCRYPTION_PASSWORD 中设置的值
-->
</bean>
<!-- -->
<!-- The will be the encryptor used for decrypting configuration values. -->
<!-- -->
<bean id="configurationEncryptor"
class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="config" ref="environmentVariablesConfiguration" />
</bean>
<bean id="propertyConfigurer"
class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
<constructor-arg ref="configurationEncryptor" />
<property name="locations">
<list>
<value>classpath:jdbcOracle_test.properties</value>
</list>
</property>
</bean>
</beans>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url" value="${jdbc.driverUrl}"
/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"
/>
</bean>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jasypt/jasypt-spring31 -->
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt-spring31</artifactId>
<version>1.9.2</version>
</dependency>
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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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
">
<bean id="environmentVariablesConfiguration"
class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
<property name="algorithm" value="PBEWithMD5AndDES" />
<property name="password" value="123456" />
<!-- <property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD" />
密钥passwordEnvName使用环境变量APP_ENCRYPTION_PASSWORD 中设置的值
-->
</bean>
<!-- -->
<!-- The will be the encryptor used for decrypting configuration values. -->
<!-- -->
<bean id="configurationEncryptor"
class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="config" ref="environmentVariablesConfiguration" />
</bean>
<bean id="propertyConfigurer"
class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
<constructor-arg ref="configurationEncryptor" />
<property name="locations">
<list>
<value>classpath:jdbcOracle_test.properties</value>
</list>
</property>
</bean>
</beans>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url" value="${jdbc.driverUrl}"
/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"
/>
</bean>