使用jasypt加密properties文件密码

package example;
import org.jasypt.util.text.BasicTextEncryptor;
/**
 * 加密解密类
 * @author Administrator
 *
 */
public class PasswordEncryptorTest {
    public static void main(String[] args) {
        //加密 
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); 
        textEncryptor.setPassword("root");
        String newPassword = textEncryptor.encrypt("123456");
        System.out.println(newPassword);
//        解密 
        BasicTextEncryptor textEncryptor2 = new BasicTextEncryptor(); 
        textEncryptor2.setPassword("root"); 
        String oldPassword = textEncryptor2.decrypt(newPassword);   
        System.out.println(oldPassword);
    }
}


spring配置文件

 <bean id="environmentVariablesConfiguration"
  class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
  <property name="algorithm" value="PBEWithMD5AndDES" />
  <property name="password" value="root" />
 </bean>
 <bean id="configurationEncryptor"
  class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
  <property name="config" ref="environmentVariablesConfiguration" />
 </bean>
 <bean id="propertyConfigurer"
  class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer">
  <constructor-arg ref="configurationEncryptor" />
  <property name="locations">
   <list>
    <value>/WEB-INF/app-config/jdbc.properties</value>
   </list>
  </property>
  <property name="fileEncoding" value="utf-8" />
 </bean>


jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/flyhigher?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=ENC(ssTkzys6BnDAh8JabCprCw==)
hibernate.dialect=org.hibernate.dialect.MySQLDialect

你可能感兴趣的:(spring,mysql,Web,Hibernate,jdbc)