applicationContext.xml中数据源密码加密方法

一、新建加密类,根据项目情况使用任何方法都可以,这里使用AES加密。

package com.xxx.xxx.util;

/**
 * AES 高级加密算法,本项目中用于对数据库的验证信息进行加密
 *
 * @author Mcy
 * @create 2020/10/15
 * @since 1.0.0
 */
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AESUtil {
    //自己编一个key就行
    private static String key="xxx";

    /**
     * 加密
     * @param content
     * @param strKey
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(String content,String strKey ) throws Exception {
        SecretKeySpec skeySpec = getKey(strKey);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(content.getBytes());
        return  encrypted;
    }

    /**
     * 解密
     * @param strKey
     * @param content
     * @return
     * @throws Exception
     */
    public static String decrypt(byte[] content,String strKey ) throws Exception {
        SecretKeySpec skeySpec = getKey(strKey);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] original = cipher.doFinal(content);
        String originalString = new String(original);
        return originalString;
    }

    private static SecretKeySpec getKey(String strKey) throws Exception {
        byte[] arrBTmp = strKey.getBytes();
        byte[] arrB = new byte[16]; // 创建一个空的16位字节数组(默认值为0)
        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }

        SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");

        return skeySpec;
    }


    /**
     * base 64 encode
     * @param bytes 待编码的byte[]
     * @return 编码后的base 64 code
     */
    public static String base64Encode(byte[] bytes){
        return new BASE64Encoder().encode(bytes);
    }

    /**
     * base 64 decode
     * @param base64Code 待解码的base 64 code
     * @return 解码后的byte[]
     * @throws Exception
     */
    public static byte[] base64Decode(String base64Code) throws Exception{
        return base64Code.isEmpty() ? null : new BASE64Decoder().decodeBuffer(base64Code);
    }

    /**
     * AES加密为base 64 code
     * @param content 待加密的内容
     * encryptKey 加密密钥
     * @return 加密后的base 64 code
     * @throws Exception  //加密传String类型,返回String类型
     */
    public static String aesEncrypt(String content) throws Exception {
        return base64Encode(encrypt(content, key));

    }
    /**
     * 将base 64 code AES解密
     * @param encryptStr 待解密的base 64 code
     * decryptKey 解密密钥
     * @return 解密后的string   //解密传String类型,返回String类型
     * @throws Exception
     */
    public static String aesDecrypt(String encryptStr) throws Exception {
        return encryptStr.isEmpty() ? null : decrypt(base64Decode(encryptStr), key);
    }


    public static void main(String[] args) throws Exception {
        String encrypt = aesEncrypt("原密码");
        System.out.println(encrypt);
        String decrypt = aesDecrypt("加密后");
        System.out.println(decrypt);
    }
}

二、新建jdbc.properties,password字段用加密类加密,把加密后的字符串放到配置文件中。

三、新建JDBCUtil.java,该类用于applicationContext.xml中解密字符串。

package com.xxx.xxx.util;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * @author Mcy
 * @create 2020/10/15
 * @since 1.0.0
 */
public class JDBCUtil extends PropertyPlaceholderConfigurer {

    @Override
    protected String convertProperty(String propertyName, String propertyValue)
    {

        System.out.println("开始解密 : "+propertyValue);
        String decryptValue = "";
        try {
            decryptValue = AESUtil.aesDecrypt(propertyValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("解密结束 : "+decryptValue);
        return decryptValue;
    }
}

四、applicationContext.xml中把配置文件加进去

<bean id="jdbcproperties" class="com.xxx.xxx.util.JDBCUtil">
	<property name="locations">
		<list>
			<value>classpath:jdbc.properties</value>
		</list>
	</property>
</bean>

五、applicationContext.xml中数据源密码改为${jdbc.password}

<property name="password" value="${jdbc.password}" />

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值