SSM数据库账号密码加密

使用SSM框架开发WEB项目时,数据库的账号密码一般会写在dbconfig.properties里,为了做到保护版权等效果,要对数据库账号密码进行加密,一共有分为三步。

一、创建DESUtil类

提供自定义密钥,加密解密的方法。

package com.hzdy.DCAD.common.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;

/**
 * Created by Wongy on 2017/10/30.
 */
public class DESUtil {
    private static Key key;
    //自己的密钥
    private static String KEY_STR = "mykey";

    static {
        try {
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(KEY_STR.getBytes());
            generator.init(secureRandom);
            key = generator.generateKey();
            generator = null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对字符串进行加密,返回BASE64的加密字符串
     *
     * @param str
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getEncryptString(String str) {
        BASE64Encoder base64Encoder = new BASE64Encoder();
        try {
            byte[] strBytes = str.getBytes("UTF-8");
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encryptStrBytes = cipher.doFinal(strBytes);
            return base64Encoder.encode(encryptStrBytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 对BASE64加密字符串进行解密
     *
     */
    public static String getDecryptString(String str) {
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try {
            byte[] strBytes = base64Decoder.decodeBuffer(str);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] encryptStrBytes = cipher.doFinal(strBytes);
            return new String(encryptStrBytes, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }


    public static void main(String[] args) {
        String name = "root";
        String password = "hzdy2016";
        String encryname = getEncryptString(name);
        String encrypassword = getEncryptString(password);
        System.out.println("encryname : " + encryname);
        System.out.println("encrypassword : " + encrypassword);

        System.out.println("name : " + getDecryptString(encryname));
        System.out.println("password : " + getDecryptString(encrypassword));
    }
}

二、 创建EncryptPropertyPlaceholderConfigurer类

建立与配置文件的关联。

package com.hzdy.DCAD.common.util;

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

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    //属性需与配置文件的KEY保持一直
    private String[] encryptPropNames = {"jdbc.username", "jdbc.password"};

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

        //如果在加密属性名单中发现该属性  
        if (isEncryptProp(propertyName)) {
            String decryptValue = DESUtil.getDecryptString(propertyValue);
            System.out.println(decryptValue);
            return decryptValue;
        } else {
            return propertyValue;
        }

    }

    private boolean isEncryptProp(String propertyName) {
        for (String encryptName : encryptPropNames) {
            if (encryptName.equals(propertyName)) {
                return true;
            }
        }
        return false;
    }
} 

修改配置文件

将spring-context中的
<context:property-placeholder location="classpath:.properties" />
修改为
<bean class="com.hzdy.DCAD.common.util.EncryptPropertyPlaceholderConfigurer"p:locations="classpath:*.properties"/>
//注意只能存在一个读取配置文件的bean,否则系统只会读取最前面的

这个时候就能实现,在配置文件中写密文,系统读取时自动变成明文的效果。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM框架,可以通过使用加密算法来对数据库密码进行加密,以增加系统的安全性。下面是一种常见的方法来配置数据库密码加密: 1. 创建数据库密码加密工具类: 创建一个工具类,用于实现数据库密码加密解密功能。可以使用Java的加密算法,如AES或DES,来加密密码。在这个工具类,提供加密解密方法供后续使用。 2. 配置数据库密码加密的拦截器: 在SSM框架,可以使用拦截器来实现对数据库密码加密解密。创建一个拦截器,在拦截器数据库配置文件密码进行解密,并将解密后的密码写入到数据源。同时,在获取数据库连接时,将获取到的密码进行加密再传给数据库。 3. 配置拦截器到Spring配置文件: 在Spring配置文件,配置上述创建的拦截器,使其生效。可以通过配置拦截器的顺序来确保在获取数据库连接之前进行密码解密加密操作。 4. 配置数据库连接池和数据源: 在Spring配置文件,配置数据库连接池和数据源相关的信息。其密码字段使用加密后的形式进行配置。这样,在获取数据库连接时,会自动进行解密操作。 通过以上步骤,就可以实现在SSM框架数据库密码进行加密解密的功能。这样可以有效保护数据库密码的安全性。记得在实际应用,要选择合适的加密算法和密钥管理策略,以确保密码的安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值