spring web项目 数据库用户名密码加密解密

spring web项目 数据库用户名密码加密解密

在使用springMVC开发web项目中,数据库的用户名,密码一般都是配置在.properties文件中

然后在通过.xml配置文件引入.properties的变量,例如

在config.properties文件中,配置如下变量,变量值配置在pom.xml的profile标签下,在此就不再赘述

[html]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. jdbc.driverClassName=com.mysql.jdbc.Driver  
  2. jdbc.url=jdbc\:mysql\://${p.jdbc.url}/${p.jdbc.dbname}?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull&rewriteBatchedStatements\=true  
  3. jdbc.username=${p.jdbc.username}  
  4. jdbc.password=${p.jdbc.password}  


在applicationContext.xml中,通过如下标签引入这些变量值

    1. <context:property-placeholder location="classpath:/config.properties" /> 


这样对于是明文的帐号,密码,是没有问题的。但是如果我在配置文件中的帐号密码是加密后的,那么如何进行使用配置呢?


解决办法:

1.首先确定加密解密算法,这种情况下,我们肯定选择是对称性加密解密算法,首选DES算法,在这里就拿他举例

2.完成加密解密算法,这个代码很简单,就不赘述,密钥自己决定,保密即可

[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. public class DESUtils  
  2. {  
  3.     private static Key key;  
  4.     private static String KEY_STR="mykey";  
  5.       
  6.     static{  
  7.         try  
  8.         {  
  9.             KeyGenerator generator = KeyGenerator.getInstance("DES");  
  10.             SecureRandom secureRandom=SecureRandom.getInstance("SHA1PRNG");  
  11.             secureRandom.setSeed(KEY_STR.getBytes());  
  12.             generator.init(secureRandom);  
  13.             key = generator.generateKey();  
  14.             generator=null;  
  15.         }  
  16.         catch (Exception e)  
  17.         {  
  18.             throw new RuntimeException(e);  
  19.         }  
  20.     }  
  21.       
  22.     /** 
  23.      * 对字符串进行加密,返回BASE64的加密字符串 
  24.      * <功能详细描述> 
  25.      * @param str 
  26.      * @return 
  27.      * @see [类、类#方法、类#成员] 
  28.      */  
  29.     public static String getEncryptString(String str){  
  30.         BASE64Encoder base64Encoder = new BASE64Encoder();  
  31.         System.out.println(key);  
  32.         try  
  33.         {  
  34.             byte[] strBytes = str.getBytes("UTF-8");  
  35.             Cipher cipher = Cipher.getInstance("DES");  
  36.             cipher.init(Cipher.ENCRYPT_MODE, key);  
  37.             byte[] encryptStrBytes = cipher.doFinal(strBytes);  
  38.             return base64Encoder.encode(encryptStrBytes);  
  39.         }  
  40.         catch (Exception e)  
  41.         {  
  42.             throw new RuntimeException(e);  
  43.         }  
  44.           
  45.     }  
  46.       
  47.     /** 
  48.      * 对BASE64加密字符串进行解密 
  49.      * <功能详细描述> 
  50.      * @param str 
  51.      * @return 
  52.      * @see [类、类#方法、类#成员] 
  53.      */  
  54.     public static String getDecryptString(String str){  
  55.         BASE64Decoder base64Decoder = new BASE64Decoder();  
  56.         try  
  57.         {  
  58.             byte[] strBytes = base64Decoder.decodeBuffer(str);  
  59.             Cipher cipher = Cipher.getInstance("DES");  
  60.             cipher.init(Cipher.DECRYPT_MODE, key);  
  61.             byte[] encryptStrBytes = cipher.doFinal(strBytes);  
  62.             return new String(encryptStrBytes,"UTF-8");  
  63.         }  
  64.         catch (Exception e)  
  65.         {  
  66.             throw new RuntimeException(e);  
  67.         }  
  68.           
  69.     }  
  70.       
  71.       
  72.     public static void main(String[] args)  
  73.     {  
  74.         String name ="root";  
  75.         String password="1234";  
  76.         String encryname = getEncryptString(name);  
  77.         String encrypassword = getEncryptString(password);  
  78.         System.out.println(encryname);  
  79.         System.out.println(encrypassword);  
  80.           
  81.         System.out.println(getDecryptString(encryname));  
  82.         System.out.println(getDecryptString(encrypassword));  
  83.     }  
  84. }  


3.springMVC中需要实现解密的接口

需要覆盖convertProperty方法,encryptPropNames存储的是需要解密的属性

[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer  
  2. {  
  3.     private String[] encryptPropNames = {"jdbc.username""jdbc.password"};  
  4.       
  5.     @Override  
  6.     protected String convertProperty(String propertyName, String propertyValue)  
  7.     {  
  8.           
  9.         //如果在加密属性名单中发现该属性  
  10.         if (isEncryptProp(propertyName))  
  11.         {  
  12.             String decryptValue = DESUtils.getDecryptString(propertyValue);  
  13.             System.out.println(decryptValue);  
  14.             return decryptValue;  
  15.         }else {  
  16.             return propertyValue;  
  17.         }  
  18.           
  19.     }  
  20.       
  21.     private boolean isEncryptProp(String propertyName)  
  22.     {  
  23.         for (String encryptName : encryptPropNames)  
  24.         {  
  25.             if (encryptName.equals(propertyName))  
  26.             {  
  27.                 return true;  
  28.             }  
  29.         }  
  30.         return false;  
  31.     }  
  32. }  


4.配置这个解密类
[html]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. 用  
  2. <bean class="com.cyou.web.common.EncryptPropertyPlaceholderConfigurer" p:location="classpath:/config.properties"></bean>  
  3. 代替  
  4. <context:property-placeholder location="classpath:/config.properties" /> 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值