In Hibernate.cfg.xml, users and passwords are stored in plaintext, storing certain security issues and thinking of a way to encrypt information about users and passwords.
First create a connection provider, the parameter interpretation in the configuration file is responsible for this class, so as long as the ciphertext decryption in this class. Public class customdrivermanagerconnectionprovider extends provider { public customdrivermanagerconnectionprovider () { super (); /* * (Non-javadoc) * * @see Org.hibernate.connection.drivermanagerconnectionprovider#configure (java.util.Properties) */ @Override public void configure ( Properties props) throws hibernateexception { string user = props.getproperty (Environment.user); string password = props.getproPerty (Environment.pass); props.setproperty (Environment.user, secutil.decrypt (USER) ); props.setproperty (Environment.pass, secutil.decrypt ( password)); super.configure (props); }}
Write a class that uses AES to encrypt and decrypt strings/** * aes encryption tools * * @author bany * * @version creation time:2008-8-5 am 10:58:16 * */public class secutil { private static byte[] keybytes = { 0x31, 0x32, ...... }; public static void main (String[] args) throws Exception { string e1 = encrypt ("NewPassword");         SYSTEM.OUT.PRINTLN (E1); string e2 = decrypt (E1);         SYSTEM.OUT.PRINTLN (E2); } /** * Encryption * @param value * @return */ public static string encrypt (String value) { string s=null; int mode = Cipher.ENCRYPT_MODE; try { cipher cipher = initcipher (mode); byte[] outBytes = Cipher.dofinal (Value.getbytes ()); s = string.valueof ( Hex.encodehex (outbytes)); } catch (exception e) { e.printstacktrace (); } return s; } /** * decryption * @param value * @return */ public static string decrypt (string value) { String s = null; int mode = Cipher.DECRYPT_MODE; try { cipher cipher = initcipher (mode); byte[] outBytes = Cipher.dofinal (Hex.decodehex (Value.tochararray ())); s = new String (outbytes); } catch (exception e) { e.printstacktrace (); } return s ; } private static Cipher Initcipher (Int mode) throws NoSuchAlgorithmException, NoSuchPaddingException, invalidkeyexception{ Cipher cipher = Cipher.getinstance ("aes/ecb/pkcs5padding"); key key = new secretkeyspec (Keybytes, "AES"); cipher.init (Mode, key); return cipher; }}
Call Secutil.encrypt method, the user password encryption to generate ciphertext, and then modify the Hibernate.cfg.xml file according to ciphertext
<property name= "Connection.username" >c59cd98</property> <property name= "Connection.password" > 68e32593ea5943a6a</property> <property name= "Connection.provider_class" >bany. Customdrivermanagerconnectionprovider</property>
Line 12th is the ciphertext after encryption, and the third line uses the custom connector
PS: If you use a third-party connector, the Customdrivermanagerconnectionprovider needs to inherit from the appropriate connector, such as C3p0connectionprovider