Spring 管理下的web项目,对Properties文件重要参数的加密解密处理

本文介绍了在Spring管理的web项目中,如何对Properties文件中的重要参数如jdbc、redis的用户名和密码进行加密解密处理。通过自定义 DecryptPropertyPlaceholderConfigurer 类在读取配置前进行解密,确保敏感信息的安全。同时提到,通常不加密整个文件以方便其他非敏感参数的修改。文中还提到了PropertiesConstant常量类和SpringConfig.xml的关键配置,并警告在配置加载时要避免两种加载方式的冲突。
摘要由CSDN通过智能技术生成

javaWeb项目中往往我们会把jdbc/redis等的属性配置放在properties文件中,但是又不想让非开发人员看到某些重要配置信息,所以需要对这样类似与用户名、密码等的字段进行加密,但是Spring管理项目启动时候只去读取,所以必须继承PropertyPlaceholderConfigurer 类来实现读取前的解密过程

加密类

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

public class DesEncryptUtil
{
   
    public static void main(String[] args) throws Exception
    {
        /** 生成KEY */
        // createKey("../key");

        Key key = getKey("../key");

        /** 加密 */
        byte[] result = doEncrypt(key, "");
//      System.out.println(result);
//      System.out.println(result.length);
        String buf = "";
        for (int i = 0; i < result.length; i++)
        {
            buf += result[i] + ",";
        }
        System.out.println(buf);

        /** 解密 */
//      System.out.println(doDecrypt(key, buf));
    }

    @SuppressWarnings("restriction")
    public static void createKey(String keyFilePath) throws Exception
    {
        Security.insertProviderAt(new com.sun.crypto.provider.SunJCE(), 1);
        KeyGenerator generator = KeyGenerator.getInstance("DES");
        generator.init(new 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值