分享

Hibernate的配置文件中用户和密码的加密

 yetao_study 2014-09-27

 hibernate.cfg.xml中,用户和密码是明文存放的,存放某些安全问题,想了一个办法加密些用户和密码的信息。

 

首先创建一个连接供应器,配置文件里的参数解释都是此类负责,所以,只要在此类中进行密文解密即可。

  1. public class CustomDriverManagerConnectionProvider extends
  2.         Provider {
  3.     public CustomDriverManagerConnectionProvider() {
  4.         super();
  5.     }
  6.     /*
  7.      * (non-Javadoc)
  8.      * 
  9.      * @see org.hibernate.connection.DriverManagerConnectionProvider#configure(java.util.Properties)
  10.      */
  11.     @Override
  12.     public void configure(Properties props) throws HibernateException {
  13.         String user = props.getProperty(Environment.USER);
  14.         String password = props.getProperty(Environment.PASS);
  15.         props.setProperty(Environment.USER, SecUtil.decrypt(user));
  16.         props.setProperty(Environment.PASS, SecUtil.decrypt(password));
  17.         super.configure(props);
  18.     }
  19. }

 

再写一个类,使用AES负责字符串的加密与解密

  1. /**
  2.  * AES加密工具
  3.  * 
  4.  * @author Bany
  5.  * 
  6.  * @version 创建时间:2008-8-5 上午10:58:16
  7.  * 
  8.  */
  9. public class SecUtil {
  10.     private static byte[] keybytes = { 0x31, 0x32, …… };
  11.     public static void main(String[] args) throws Exception {
  12.         String e1 = encrypt("newpassword");
  13.         System.out.println(e1);
  14.         String e2 = decrypt(e1);
  15.         System.out.println(e2);
  16.     }
  17.     /**
  18.      * 加密
  19.      * @param value
  20.      * @return
  21.      */
  22.     public static String encrypt(String value) {
  23.         
  24.         String s=null;
  25.         int mode = Cipher.ENCRYPT_MODE;
  26.         try {
  27.             Cipher cipher = initCipher(mode);
  28.             byte[] outBytes = cipher.doFinal(value.getBytes());
  29.             s = String.valueOf(Hex.encodeHex(outBytes));
  30.         } catch (Exception e) {
  31.             e.printStackTrace();
  32.         }
  33.         return s;
  34.     }
  35.     /**
  36.      * 解密
  37.      * @param value
  38.      * @return
  39.      */
  40.     public static String decrypt(String value) {
  41.         String s = null;
  42.         int mode = Cipher.DECRYPT_MODE;
  43.         try {
  44.             Cipher cipher = initCipher(mode);
  45.             byte[] outBytes = cipher.doFinal(Hex.decodeHex(value.toCharArray()));
  46.             s = new String(outBytes);
  47.         } catch (Exception e) {
  48.             e.printStackTrace();
  49.         }
  50.         return s;
  51.     }
  52.     
  53.     private static Cipher initCipher(int mode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
  54.         Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  55.         Key key = new SecretKeySpec(keybytes, "AES");
  56.         cipher.init(mode, key);
  57.         return cipher;
  58.     }
  59. }

调用SecUtil.encrypt的方法,把用户密码加密生成密文,然后根据密文修改hibernate.cfg.xml文件

 

  1. <property name="connection.username">c59cd98</property>
  2. <property name="connection.password">68e32593ea5943a6a</property>
  3. <property name="connection.provider_class">bany.CustomDriverManagerConnectionProvider</property>

第一二行是加密后的密文,第三行是使用自定义的连接器

 

 

PS:如果使用第三方的连接器,CustomDriverManagerConnectionProvider则需要继承于相应的连接器,如C3P0ConnectionProvider

 

 

  hibernate.cfg.xml中,用户和密码是明文存放的,存放某些安全问题,想了一个办法加密些用户和密码的信息。

 

首先创建一个连接供应器,配置文件里的参数解释都是此类负责,所以,只要在此类中进行密文解密即可。

  1. public class CustomDriverManagerConnectionProvider extends
  2.         Provider {
  3.     public CustomDriverManagerConnectionProvider() {
  4.         super();
  5.     }
  6.     /*
  7.      * (non-Javadoc)
  8.      * 
  9.      * @see org.hibernate.connection.DriverManagerConnectionProvider#configure(java.util.Properties)
  10.      */
  11.     @Override
  12.     public void configure(Properties props) throws HibernateException {
  13.         String user = props.getProperty(Environment.USER);
  14.         String password = props.getProperty(Environment.PASS);
  15.         props.setProperty(Environment.USER, SecUtil.decrypt(user));
  16.         props.setProperty(Environment.PASS, SecUtil.decrypt(password));
  17.         super.configure(props);
  18.     }
  19. }

 

再写一个类,使用AES负责字符串的加密与解密

  1. /**
  2.  * AES加密工具
  3.  * 
  4.  * @author Bany
  5.  * 
  6.  * @version 创建时间:2008-8-5 上午10:58:16
  7.  * 
  8.  */
  9. public class SecUtil {
  10.     private static byte[] keybytes = { 0x31, 0x32, …… };
  11.     public static void main(String[] args) throws Exception {
  12.         String e1 = encrypt("newpassword");
  13.         System.out.println(e1);
  14.         String e2 = decrypt(e1);
  15.         System.out.println(e2);
  16.     }
  17.     /**
  18.      * 加密
  19.      * @param value
  20.      * @return
  21.      */
  22.     public static String encrypt(String value) {
  23.         
  24.         String s=null;
  25.         int mode = Cipher.ENCRYPT_MODE;
  26.         try {
  27.             Cipher cipher = initCipher(mode);
  28.             byte[] outBytes = cipher.doFinal(value.getBytes());
  29.             s = String.valueOf(Hex.encodeHex(outBytes));
  30.         } catch (Exception e) {
  31.             e.printStackTrace();
  32.         }
  33.         return s;
  34.     }
  35.     /**
  36.      * 解密
  37.      * @param value
  38.      * @return
  39.      */
  40.     public static String decrypt(String value) {
  41.         String s = null;
  42.         int mode = Cipher.DECRYPT_MODE;
  43.         try {
  44.             Cipher cipher = initCipher(mode);
  45.             byte[] outBytes = cipher.doFinal(Hex.decodeHex(value.toCharArray()));
  46.             s = new String(outBytes);
  47.         } catch (Exception e) {
  48.             e.printStackTrace();
  49.         }
  50.         return s;
  51.     }
  52.     
  53.     private static Cipher initCipher(int mode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
  54.         Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  55.         Key key = new SecretKeySpec(keybytes, "AES");
  56.         cipher.init(mode, key);
  57.         return cipher;
  58.     }
  59. }

调用SecUtil.encrypt的方法,把用户密码加密生成密文,然后根据密文修改hibernate.cfg.xml文件

 

  1. <property name="connection.username">c59cd98</property>
  2. <property name="connection.password">68e32593ea5943a6a</property>
  3. <property name="connection.provider_class">bany.CustomDriverManagerConnectionProvider</property>

第一二行是加密后的密文,第三行是使用自定义的连接器

 

 

PS:如果使用第三方的连接器,CustomDriverManagerConnectionProvider则需要继承于相应的连接器,如C3P0ConnectionProvider

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多