• 商铺项目(使用DES加密配置信息)


    package com.ouyan.o2o.util;
    
    import java.security.Key;
    import java.security.SecureRandom;
    
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    /**
     * DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法。
     * 
     * @author 
     *
     */
    public class DESUtil {
    
        private static Key key;
        // 设置密钥key
        private static String KEY_STR = "myKey";
        private static String CHARSETNAME = "UTF-8";
        private static String ALGORITHM = "DES";
    
        static {
            try {
                // 生成DES算法对象
                KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
                // 运用SHA1安全策略
                SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
                // 设置上密钥种子
                secureRandom.setSeed(KEY_STR.getBytes());
                // 初始化基于SHA1的算法对象
                generator.init(secureRandom);
                // 生成密钥对象
                key = generator.generateKey();
                generator = null;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * 获取加密后的信息
         * 
         * @param str
         * @return
         */
        public static String getEncryptString(String str) {
            // 基于BASE64编码,接收byte[]并转换成String
            BASE64Encoder base64encoder = new BASE64Encoder();
            try {
                // 按UTF8编码
                byte[] bytes = str.getBytes(CHARSETNAME);
                // 获取加密对象
                Cipher cipher = Cipher.getInstance(ALGORITHM);
                // 初始化密码信息
                cipher.init(Cipher.ENCRYPT_MODE, key);
                // 加密
                byte[] doFinal = cipher.doFinal(bytes);
                // byte[]to encode好的String并返回
                return base64encoder.encode(doFinal);
            } catch (Exception e) {
                // TODO: handle exception
                throw new RuntimeException(e);
            }
        }
    
        /**
         * 获取解密之后的信息
         * 
         * @param str
         * @return
         */
        public static String getDecryptString(String str) {
            // 基于BASE64编码,接收byte[]并转换成String
            BASE64Decoder base64decoder = new BASE64Decoder();
            try {
                // 将字符串decode成byte[]
                byte[] bytes = base64decoder.decodeBuffer(str);
                // 获取解密对象
                Cipher cipher = Cipher.getInstance(ALGORITHM);
                // 初始化解密信息
                cipher.init(Cipher.DECRYPT_MODE, key);
                // 解密
                byte[] doFinal = cipher.doFinal(bytes);
                // 返回解密之后的信息
                return new String(doFinal, CHARSETNAME);
            } catch (Exception e) {
                // TODO: handle exception
                throw new RuntimeException(e);
            }
        }
    
        public static void main(String[] args) {
            System.out.println(getEncryptString("work"));
            System.out.println(getEncryptString("230230"));
        }
    
    }

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.slave.url=jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8
    jdbc.master.url=jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8
    jdbc.username=zCKAAEaFQUI=
    jdbc.password=TdixoQRe6Y0=

    package com.ouyan.o2o.util;
    
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    
    public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
        // 需要加密的字段数组
        private String[] encryptPropNames = { "jdbc.username", "jdbc.password" };
    
        /**
         * 对关键的属性进行转换
         */
        @Override
        protected String convertProperty(String propertyName, String propertyValue) {
            if (isEncryptProp(propertyName)) {
                // 对已加密的字段进行解密工作
                String decryptValue = DESUtil.getDecryptString(propertyValue);
                return decryptValue;
            } else {
                return propertyValue;
            }
        }
    
        /**
         * 该属性是否已加密
         * 
         * @param propertyName
         * @return
         */
        private boolean isEncryptProp(String propertyName) {
            // 若等于需要加密的field,则进行加密
            for (String encryptpropertyName : encryptPropNames) {
                if (encryptpropertyName.equals(propertyName))
                    return true;
            }
            return false;
        }
    }

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        <!-- 配置整合mybatis过程 -->
            <!-- 1.配置数据库相关参数properties的属性:${url} -->
        <bean class="com.ouyan.o2o.util.EncryptPropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                </list>
            </property>
        </bean>
        <!-- 2.数据库连接池 -->
        <bean id="abstractDataSource" abstract="true" destroy-method="close"
            class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!-- c3p0连接池的私有属性 -->
            <property name="maxPoolSize" value="30" />
            <property name="minPoolSize" value="10" />
            <!-- 关闭连接后不自动commit -->
            <property name="autoCommitOnClose" value="false" />
            <!-- 获取连接超时时间 -->
            <property name="checkoutTimeout" value="10000" />
            <!-- 当获取连接失败重试次数 -->
            <property name="acquireRetryAttempts" value="2" />
        </bean>
        <bean id="master" parent="abstractDataSource">
            <!-- 配置连接池属性 -->
            <property name="driverClass" value="${jdbc.driver}" />
            <property name="jdbcUrl" value="${jdbc.master.url}" />
            <property name="user" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
        <bean id="slave" parent="abstractDataSource">
            <!-- 配置连接池属性 -->
            <property name="driverClass" value="${jdbc.driver}" />
            <property name="jdbcUrl" value="${jdbc.slave.url}" />
            <property name="user" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
        <!-- 3.配置SqlSessionFactory对象 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 注入数据库连接池 -->
            <property name="dataSource" ref="dataSource" />
            <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
            <property name="configLocation" value="classpath:mybatis-config.xml" />
            <!-- 扫描entity包 使用别名 -->
            <property name="typeAliasesPackage" value="com.ouyan.o2o.entity" />
            <!-- 扫描sql配置文件:mapper需要的xml文件 -->
            <property name="mapperLocations" value="classpath:mapper/*.xml" />
        </bean>
        <!-- 配置动态数据源,这儿targetDataSources就是路由数据源对应的名称 -->
        <bean id="dynamicDataSource" class="com.ouyan.o2o.dao.split.DynamicDataSource">
            <property name="targetDataSources">
                <map>
                    <entry value-ref="master" key="master"></entry>
                    <entry value-ref="slave" key="slave"></entry>
                </map>
            </property>
        </bean>
        <bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
            <property name="targetDataSource">
                <ref bean="dynamicDataSource"/>        
            </property>
        </bean>
        <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 注入sqlSessionFactory -->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
            <!-- 给出需要扫描Dao接口包 -->
            <property name="basePackage" value="com.ouyan.o2o.dao" />
        </bean>
    </beans>
  • 相关阅读:
    INTERVAL YEAR TO MONTH数据类型
    Oracle 中DATE类型的计算
    Oracle中特殊的变量类型
    Webview窗口设置遮罩层
    mui.init方法配置
    mui.fire()触发自定义事件
    管理员启动程序的命令
    收藏网址
    html标签
    Event对象和触发
  • 原文地址:https://www.cnblogs.com/XJJD/p/7706002.html
Copyright © 2020-2023  润新知