为Hibernate配置文件加密的三套解决方案(二)
提要:本文将详细介绍使用Jasypt的加密功能为Hibernate配置文件加密
首先来了解什么是Jasypt
encrypt input=明文(可以是密码) password=密钥
上面的明文和密钥都不要加引号,output就是需要的密文,将其记录下来
得到密文以后,工作就很简单了
首先在你的项目中添加需要的包(jasypt-1.5.jar, commons-codec-1.1.jar, commons-lang-2.1.jar, icu4j-4_0.zip)
之后修改hibernate.cfg.xml文件
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.provider_class"> org.jasypt.hibernate.connectionprovider.EncryptedPasswordDriverManagerConnectionProvider </property> <!-- 这个是添加的provider类 --> <property name="connection.encryptor_registered_name"> configurationHibernateEncryptor </property> <!-- 这里是加密密钥 --> <property name="connection.url">jdbc:mysql://localhost/reportsdb</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">reportsUser</property> <property name="connection.password">ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)</property><!-- 这里的格式是ENC(密文) --> <property name="connection.pool_size">12</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Mappings etc... --> </session-factory></hibernate-configuration>