zoukankan      html  css  js  c++  java
  • 数据源的加密解密

    先来看一下数据库配置文件:

    dbCustomer.driverClass=org.gjt.mm.mysql.Driver
    dbCustomer.jdbcUrl=jdbc:mysql://192.168.1.81:3306/p2p_customer?useUnicode=true&characterEncoding=UTF8
    dbCustomer.user=PCVoqoFQn5w=     加密后的用户名
    dbCustomer.password=eh1IPqyJjLs=  加密后的密码
    #
    dbCustomer.initialPoolSize=10
    dbCustomer.maxIdleTime=60
    dbCustomer.maxPoolSize=50
    dbCustomer.minPoolSize=10
    #
    dbCustomer.acquireIncrement=3
    dbCustomer.acquireRetryDelay=1000
    dbCustomer.acquireRetryAttempts=30
    dbCustomer.breakAfterAcquireFailure=false

    applicationContext.xml 中的C3P0中的配置如下:

     <!-- 数据库连接池管理 -->
        <bean id="c3p0DataSourceCustomer" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="${dbCustomer.driverClass}"/>
            <property name="jdbcUrl" value="${dbCustomer.jdbcUrl}"/>
            <!-- <property name="user" value="${dbCustomer.user}"/>
            <property name="password" value="${dbCustomer.password}"/> -->
            <property name="properties" ref="dataSourcePropertiesCustomer"/> 
    
            <property name="initialPoolSize" value="${dbCustomer.initialPoolSize}"/>
            <property name="maxIdleTime" value="${dbCustomer.maxIdleTime}"/>
            <property name="maxPoolSize" value="${dbCustomer.maxPoolSize}"/>
            <property name="minPoolSize" value="${dbCustomer.minPoolSize}"/>
            <property name="acquireIncrement" value="${dbCustomer.acquireIncrement}"/>
            <property name="acquireRetryDelay" value="${dbCustomer.acquireRetryDelay}"/>
            <property name="acquireRetryAttempts" value="${dbCustomer.acquireRetryAttempts}"/>
            <property name="breakAfterAcquireFailure" value="${dbCustomer.breakAfterAcquireFailure}"/>
        </bean>
    <bean id="dataSourcePropertiesCustomer" class="com.hzfh.service.EncryptedDataSourceFactory"> <property name="properties"> <props> <prop key="user">${dbCustomer.user}</prop> <prop key="password">${dbCustomer.password}</prop> </props> </property> </bean>

    项目启动加载时,会自动找到 com.hzfh.service.EncryptedDataSourceFactory 类,并且 用户名:user、密码:password 传入到该类中进行解密

     1 package com.hzfh.service.EncryptedDataSourceFactory;
     2 import java.io.UnsupportedEncodingException;
     3 import java.util.Properties;
     4 
     5 import org.springframework.beans.factory.FactoryBean;
     6 
     7 import com.hzframework.encrypt.DESEncoder;
     8 import com.hzframework.encrypt.Encoder;
     9   
    10 public class EncryptedDataSourceFactory implements FactoryBean {  
    11   
    12     private Properties properties;  
    13       
    14     public Object getObject() throws Exception {  
    15         return getProperties();  
    16     }  
    17   
    18     public Class getObjectType() {  
    19         return java.util.Properties.class;  
    20     }  
    21   
    22     public boolean isSingleton() {  
    23         return true;  
    24     }  
    25   
    26     public Properties getProperties() {  
    27         return properties;  
    28     }  
    29   
    30     public void setProperties(Properties inProperties) {  
    31         this.properties = inProperties;  
    32         String originalUsername = properties.getProperty("user");  
    33         String originalPassword = properties.getProperty("password");  
    34         if (originalUsername != null){  
    35             String newUsername = decryptDESUsername(originalUsername);  
    36             properties.put("user", newUsername);  
    37         }  
    38         if (originalPassword != null){  
    39             String newPassword = decryptDESPassword(originalPassword);  
    40             properties.put("password", newPassword);  
    41         }  
    42     }  
    43       
    44     private String decryptDESUsername(String originalUsername){  
    45         return decryptDES(originalUsername);  
    46     }  
    47       
    48     private String decryptDESPassword(String originalPassword){  
    49         return decryptDES(originalPassword);  
    50     }  
    51     /**
    52      * 解密
    53      * @param data 原始数据
    54      * @return 加密后的数据
    55      */
    56     public  String decryptDES(String data) {
    57         try {
    58             String key = "GWWEEuUvhV4=";
    59             byte[] bytes = Encoder.decryptBASE64(data);
    60             return new String(DESEncoder.decrypt(bytes, key));
    61         } catch (Exception e) {
    62             e.printStackTrace();
    63         }
    64         return null;
    65     }
    66     /**
    67      * 加密
    68      * @param data 原始数据
    69      * @return 加密后的数据
    70      */
    71     public  String encryptDES(String data) {
    72         try {
    73             String key ="GWWEEuUvhV4=";
    74             byte[] bytes = toByteArray(data);
    75             return Encoder.encryptBASE64(DESEncoder.encrypt(bytes, key));
    76         } catch (Exception e) {
    77             e.printStackTrace();
    78         }
    79         return null;
    80     }
    81     private  byte[] toByteArray(String str) throws UnsupportedEncodingException {
    82         return str.getBytes("UTF-8");
    83     }
    84   
    85 }  

    上述com.hzfh.service.EncryptedDataSourceFactory类需要继承FactoryBean ,同时里面的加密、解密算法就要根据自己项目中的加密解密去写了,可以参考上一篇文章

    对用户名、密码加密时,我用到了单元测试 直接生产加密后的字符串

     1     @Test
     2     public void getEncrypt(){
     3             try {
     4                 String key ="GW0EYuUvhV4=";
     5                 byte[] bytes = toByteArray("123456");
     6                 System.out.println(Encoder.encryptBASE64(DESEncoder.encrypt(bytes, key)));
     7             } catch (Exception e) {
     8                 e.printStackTrace();
     9             }
    10     }
    11     private  byte[] toByteArray(String str) throws UnsupportedEncodingException {
    12         return str.getBytes("UTF-8");
    13     }

    输出:eh1IPqyJjLs=

    这样数据源就可以进行密文显示了,同时不影响数据库的连接。

  • 相关阅读:
    SubString函数总结
    button按钮居中
    2019 面试题
    linux(centos)搭建SVN服务器
    svn 设置钩子将代码同步到web目录下面
    sql 语句总结
    php 多维数组转换
    php 两个数组是否相同,并且输出全面的数据,相同的加一个字段标示
    PHP错误类型及屏蔽方法
    设置div中文字超出时自动换行
  • 原文地址:https://www.cnblogs.com/benefitworld/p/5843081.html
Copyright © 2011-2022 走看看