Solution for encrypting and decrypting hibernate configuration files

Source: Internet
Author: User

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]

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.