hibernate.cfg.xml中,用戶和密碼是明文存放的,存放某些安全問題,想了一個(gè)辦法加密些用戶和密碼的信息。
首先創(chuàng)建一個(gè)連接供應(yīng)器,配置文件里的參數(shù)解釋都是此類負(fù)責(zé),所以,只要在此類中進(jìn)行密文解密即可。
- 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);
- }
- }
再寫一個(gè)類,使用AES負(fù)責(zé)字符串的加密與解密
- /**
- * AES加密工具
- *
- * @author Bany
- *
- * @version 創(chuàng)建時(shí)間:2008-8-5 上午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);
- }
- /**
- * 加密
- * @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;
- }
- /**
- * 解密
- * @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;
- }
- }
調(diào)用SecUtil.encrypt的方法,把用戶密碼加密生成密文,然后根據(jù)密文修改hibernate.cfg.xml文件
- <property name="connection.username">c59cd98</property>
- <property name="connection.password">68e32593ea5943a6a</property>
- <property name="connection.provider_class">bany.CustomDriverManagerConnectionProvider</property>
第一二行是加密后的密文,第三行是使用自定義的連接器
PS:如果使用第三方的連接器,CustomDriverManagerConnectionProvider則需要繼承于相應(yīng)的連接器,如C3P0ConnectionProvider
hibernate.cfg.xml中,用戶和密碼是明文存放的,存放某些安全問題,想了一個(gè)辦法加密些用戶和密碼的信息。
首先創(chuàng)建一個(gè)連接供應(yīng)器,配置文件里的參數(shù)解釋都是此類負(fù)責(zé),所以,只要在此類中進(jìn)行密文解密即可。
- 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);
- }
- }
再寫一個(gè)類,使用AES負(fù)責(zé)字符串的加密與解密
- /**
- * AES加密工具
- *
- * @author Bany
- *
- * @version 創(chuàng)建時(shí)間:2008-8-5 上午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);
- }
- /**
- * 加密
- * @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;
- }
- /**
- * 解密
- * @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;
- }
- }
調(diào)用SecUtil.encrypt的方法,把用戶密碼加密生成密文,然后根據(jù)密文修改hibernate.cfg.xml文件
- <property name="connection.username">c59cd98</property>
- <property name="connection.password">68e32593ea5943a6a</property>
- <property name="connection.provider_class">bany.CustomDriverManagerConnectionProvider</property>
第一二行是加密后的密文,第三行是使用自定義的連接器
PS:如果使用第三方的連接器,CustomDriverManagerConnectionProvider則需要繼承于相應(yīng)的連接器,如C3P0ConnectionProvider
|