Hibernate configuration file. cfg. XML stores information related to our database connection, which is designed to contain sensitive information of many databases, such as the connection address, user name, and password, sometimes, when we release the database to a release group, we do not want them to see the connection password of the database. We need to encrypt some information in the hibernate configuration file.
There are many solutions to encrypt and decrypt the hibernate configuration file.
Step 1: create a custom sessionfactionbean and inherit annotationsessionfactorybean
Package com; import javax. SQL. datasource; import Org. apache. commons. DBCP. basicdatasource; import Org. springframework. orm. hibernate3.annotation. annotationsessionfactorybean; public class ztkjannotationsessionfactorybean extends annotationsessionfactorybean {@ overridepublic void setdatasource (datasource) {string Password = (basicdatasource) datasource ). getPassword (); // use the ztkjcryptutil class to decrypt string decrypassword = ztkjcryptutil. decodestr (password); // value (basicdatasource) datasource ). setpassword (decrypassword); super. setdatasource (datasource );}}
Ztkjcryptutil. Java is an encryption and decryption class (DES symmetric encryption, you can write it to me)
Step 2: Modify the spring configuration file
<! -- Hibernate configuration --> <bean id = "sessionfactory" class = "com. ztkjannotationsessionfactorybean "> <property name =" datasource "ref =" datasource "/> <property name =" namingstrategy "> <Bean class =" org. hibernate. cfg. improvednamingstrategy "/> </property> <property name =" hibernateproperties "> <props> <prop key =" hibernate. dialect ">$ {hibernate. dialect} </prop> <prop key = "hibernate. show_ SQL ">$ {hibernate. show_ SQL} </prop> <prop key = "hibernate. format_ SQL ">$ {hibernate. format_ SQL} </prop> <prop key = "hibernate. cache. provider_class "> Org. hibernate. cache. ehcacheprovider </prop> <prop key = "hibernate. cache. provider_configuration_file_resource_path "> ehcache/ehcache-hibernate-local.xml </prop> <prop key =" hibernate. connection. provider_class "> COM. ztkjdrivermanagerconnectionprovider </prop> </props> </property> <property name = "packagestoscan" value = "com. chinacnit "/> </bean>
(Note: only the first line of code class = "com. ztkjannotationsessionfactorybean" is modified ")
This completes the encryption configuration file, refer to the http://www.lupaworld.com/tutorial-view-aid-1847.html
If your password configuration is under the hibertate attribute, refer to the http://aguu125.iteye.com/blog/579402
There are still better methods, but unfortunately they are not available. Http://sklst.iteye.com/blog/283798 (solution 1)
The [connection provider] mentioned in this blog does not know that it is not configured, and it is always not built in its own [connection provider]