dbcp 密码加密处理

为了能达到使用 spring dbcp配置时,也有类似密码加密的功能,运行期进行密码decode,最后进行数据链接

实现方式很简单,分析jboss的对应SecureIdentityLoginModule的实现,无非就是走了Blowfish加密算法,自己拷贝实现一份。

 

 

Java代码   收藏代码
  1. private static String encode(String secret) throws NoSuchPaddingException, NoSuchAlgorithmException,  
  2.                                                InvalidKeyException, BadPaddingException, IllegalBlockSizeException {  
  3.         byte[] kbytes = "jaas is the way".getBytes();  
  4.         SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");  
  5.   
  6.         Cipher cipher = Cipher.getInstance("Blowfish");  
  7.         cipher.init(Cipher.ENCRYPT_MODE, key);  
  8.         byte[] encoding = cipher.doFinal(secret.getBytes());  
  9.         BigInteger n = new BigInteger(encoding);  
  10.         return n.toString(16);  
  11.     }  
  12.   
  13.     private static char[] decode(String secret) throws NoSuchPaddingException, NoSuchAlgorithmException,  
  14.                                                InvalidKeyException, BadPaddingException, IllegalBlockSizeException {  
  15.         byte[] kbytes = "jaas is the way".getBytes();  
  16.         SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");  
  17.   
  18.         BigInteger n = new BigInteger(secret, 16);  
  19.         byte[] encoding = n.toByteArray();  
  20.   
  21.         Cipher cipher = Cipher.getInstance("Blowfish");  
  22.         cipher.init(Cipher.DECRYPT_MODE, key);  
  23.         byte[] decode = cipher.doFinal(encoding);  
  24.         return new String(decode).toCharArray();  
  25.     }  
 

最后的配置替换为:

 

 

Xml代码   收藏代码
  1. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
  2. ......  
  3.         <property name="password"><!-- 注意多了一层转化,将密码串调用decode解密为最初的数据库密码 -->  
  4.             <bean class="com.xxxxx.EncryptDBPasswordFactory">  
  5.                 <property name="password" value="${xxxx.password.encrypted}" />  
  6.             </bean>  
  7.         </property>  
  8. ........  
  9. </bean>  

 

--------------------------------------------


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值