iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >关于properties配置文件的加密方式
  • 881
分享到

关于properties配置文件的加密方式

Python 官方文档:入门教程 => 点击学习

摘要

目录需要4个步骤编写加密解密工具类获取用户名和密码的秘文编写PropertyPlaceholderConfigurer的子类配置Bean要完成properties属性文件某些属性值的

要完成properties属性文件某些属性值的加密,和读取属性文件时进行解密

需要4个步骤

  • 编写加密解密工具类
  • 手动通过加密解密工具类获得加密后的属性值密文,并把密文填写在properties文件中
  • 编写PropertyPlaceholderConfigurer的子类,重写convertProperty()方法
  • spring-dao.xml配置文件中配置PropertyPlaceholderConfigurer类

接下来我们将拿配置数据库的properties文件进行举例(一般我们需要对用户名和密码进行加密)

编写加密解密工具类

在编写工具类前我们需要导入包含Base64这个类的依赖

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.14</version>
</dependency>

之所以要使用Base64对加密后的byte数组进行编码,可以参考Base64编码及其作用

编写使用DES加密算法的加密解密工具类

package com.lxc.o2o.util;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import org.apache.commons.codec.binary.Base64;


public class DESUtil {

    // 秘钥对象
    private static Key key;
    // 设置密钥key
    private static String KEY_STR = "myKey";
    // 使用的编码
    private static String CHARSETNAME = "UTF-8";
    // 设置使用DES算法(我们这里主要使用java的DES算法)
    private static String ALGoRITHM = "DES";

    // 初始化秘钥对象key
    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);
        }
    }

    
    public static String getEncryptString(String str) {

        try {
            // 按UTF8编码
            byte[] bytes = str.getBytes(CHARSETNAME);
            // 获取加密对象
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            // 初始化密码信息,Cipher.ENCRYPT_MODE为加密类型
            cipher.init(Cipher.ENCRYPT_MODE, key);
            // 加密
            byte[] doFinal = cipher.doFinal(bytes);
            // 基于BASE64编码,接收byte[]并转换成String
            // byte[]to encode好的String并返回,编码成字符串返回
            return Base64.encodeBase64String(doFinal);
        } catch (Exception e) {
            // TODO: handle exception
            throw new RuntimeException(e);
        }
    }

    
    public static String getDecryptString(String str) {

        try {
            // 基于BASE64编码,接收byte[]并转换成String
            // 将字符串decode成byte[],解码操作
            byte[] bytes = Base64.decodeBase64(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) throws UnsupportedEncodingException {
        System.out.println(getEncryptString("root"));
        System.out.println(getEncryptString("123456"));

    }

}

获取用户名和密码的秘文

通过上面编写的DESUtil获取用户名和密码的密文,再把密文填写进jdbc.properties文件

jdbc.driver=com.Mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/myo2o?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
jdbc.username=WnplV/ietfQ=
jdbc.passWord=QAHlVoUc49w=

编写PropertyPlaceholderConfigurer的子类

package com.lxc.o2o.util;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    // 需要加密的字段数组(这里整个jdbc属性文件,我们只对username和password加密了)
    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;
        }
    }
    
    private boolean isEncryptProp(String propertyName) {
        // 若等于需要加密的field,则进行加密
        for (String encryptpropertyName : encryptPropNames) {
            if (encryptpropertyName.equals(propertyName))
                return true;
        }
        return false;
    }
}

配置Bean

在spring-dao.xml配置文件中配置我们自己实现的EncryptPropertyPlaceholderConfigurer类

<!--连接数据库时,会自动读取对应的配置文件,并进行解密操作-->
<bean class="com.lxc.o2o.util.EncryptPropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
            <!-- 如果要读取其他加密的配置文件,继续配置在这个list中 -->
        </list>
    </property>
    <property name="fileEncoding" value="UTF-8"></property>
</bean>

运行时创建了上面的bean后,可以直接通过${属性名}获取解密后的属性值

<!-- 2.数据库连接池  -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!--配置连接池属性 -->
    <property name="driverClass" value="${jdbc.driver}"></property>
    <property name="jdbcUrl" value="${jdbc.url}"></property>
    <property name="user" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>    
</bean>

到这里我们就完成了加密和解密properties文件的所有操作 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 关于properties配置文件的加密方式

本文链接: https://www.lsjlt.com/news/170166.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作