小工具知识网

当前位置: 首页 > 行业知识 > springboot 配置文件密码加密的3种方案

行业知识

springboot 配置文件密码加密的3种方案

2026-11-13 00:00:43 来源:互联网转载

配置文件密码加密的3种方案

一.配置文件添加jasypt

1.pom.xml添加jasypt引用

    com.github.ulisesbocchio    jasypt-spring-boot-starter

2.在springboot配置文件内加入 jasypt配置

jasypt:  encryptor:    algorithm: PBEWithMD5AndDES ##盐    password: dddda123 #密钥

二.使用配置类初始加载加解密

1.pom.xml添加jasypt依赖

    com.github.ulisesbocchio    jasypt-spring-boot-starter

2.编写配置类加载bean

package com.top.common.config;import org.jasypt.encryption.StringEncryptor;import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;*//** * @author  * @date 2023/6/719:41 * @Package  * @description 自动配置加密信息 *//*@Configurationpublic class EncryptorConfig {    @Bean("jasyptStringEncryptor")    public StringEncryptor jasyptStringEncryptor() {        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();        SimpleStringPBEConfig config = new SimpleStringPBEConfig();        config.setPassword("WyYJ5C8l9qWSjkMpP2ilsIRkvhvp9wH4");        // 注释部分为配置默认        config.setAlgorithm("PBEWithMD5AndDES");//        config.setKeyObtentionIterations("1000");        config.setPoolSize("1");//        config.setProviderName("SunJCE");//        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");//        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");//        config.setStringOutputType("base64");        encryptor.setConfig(config);        return encryptor;    }}

三、不在配置文件显示配置秘钥

使用Jasypt来加密和解密

1、添加Jasypt依赖

在pom.xml文件中添加Jasypt依赖:

    org.jasypt    jasypt-spring-boot-starter    3.0.3

2、获取加密后的字符串

在线网站:https://new.lxc1314.xyz/

3、在配置文件中使用加密后的密码

在application.properties或application.yml中使用加密后的密码,格式为ENC(加密后的密码)。例如:

spring.datasource.password=ENC(1WqJf1V5n1R4n5v2k3P4q5I7r2Q3u5Q1)

4、在安装的时候向环境变量中配置解密密码和盐值

export JASYPT_ENCRYPTOR_PASSWORD=mySecretPasswordexport JASYPT_ENCRYPTOR_IV_GENERATOR_CLASSNAME=org.jasypt.iv.RandomIvGenerator

5、java 命令启动时传入jasypt.encryptor.password 和 jasypt.encryptor.iv-generator-classname

java -jar myapp.jar -Djasypt.encryptor.password=$JASYPT_ENCRYPTOR_PASSWORD -Djasypt.encryptor.iv-generator-classname=org.jasypt.iv.RandomIvGenerator

密码加密的使用

1.在需要加密的配置文件使用ENC(加密后的密码)

2.获取加密后的密码方法

    private static final String ALGORITHM_INFO = "PBEWithMD5AndDES";    private static final String PASSWORD_INFO = "WyAh5C8l9qWSGHMpda1lsIRkvhvpyDH1";    @GetMapping("/encryptPwd")    public void encryptPwd() {        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();        //配置文件中配置如下的算法        standardPBEStringEncryptor.setAlgorithm(ALGORITHM_INFO);        //配置文件中配置的password        standardPBEStringEncryptor.setPassword(PASSWORD_INFO);        //要加密的文本        String name = standardPBEStringEncryptor.encrypt("abababa");        String password = standardPBEStringEncryptor.encrypt("e022f87539fd81f3");        String redisPassword = standardPBEStringEncryptor.encrypt("gbasedbt123");        String redisPassword2 = standardPBEStringEncryptor.encrypt("abababa");        String redisPassword3 = standardPBEStringEncryptor.encrypt("123456");        String redisPassword4 = standardPBEStringEncryptor.encrypt("6a3d373e077b41b5a1c53f993c4fdc80");        String oppeer= standardPBEStringEncryptor.encrypt("oppeer");        String ddlla= standardPBEStringEncryptor.encrypt("ddlla");        String nacos = standardPBEStringEncryptor.encrypt("nacos");        //将加密的文本写到配置文件中        System.out.println("【abababa】=" + name);        System.out.println("【e022f87539fd81f3】=" + password);        System.out.println("【gbasedbt123】=" + redisPassword);        System.out.println("【abababa】=" + redisPassword2);        System.out.println("【123456】=" + redisPassword3);        System.out.println("【6a3d373e077b41b5a1c53f993c4fdc80】=" + redisPassword4);        System.out.println("【oppeer】=" + oppeer);        System.out.println("【ddlla】=" + ddlla);        System.out.println("【nacos】=" + nacos);        //要解密的文本       *//* String name2 = standardPBEStringEncryptor.decrypt("3U+OHwCEJvWNCnjV6iVYvMxYYcJTxJpoQchB4/ttEsV5YdHI9jFW8S92KiTI6yLk");        String password2 = standardPBEStringEncryptor.decrypt("0PbODjzlXKy2a0Ijag3oEFqmvRpUl736GVkh55sY4SmTk5Nl1FzxpETEScFtL47W");//        String redisPassword2 = standardPBEStringEncryptor.decrypt("ZII7UphhbVuJ8c3oxPUeyw==");        //解密后的文本        System.out.println("name2=" + name2);        System.out.println("password2=" + password2);*//*}
  • 两种配置文件密码加密方案,一种是在配置文件中添加jasypt引用并加入jasypt配置,另一种是使用配置类加载bean进行加解密。加密后的密码可以在配置文件中使用ENC(加密后的密码)获取。其中使用的加密算法是PBEWithMD5AndDES,密钥为WyAh5C8l9qWSGHMpda1lsIRkvhvpyDH1。可以通过访问/encryptPwd来获取加密后的密码。

上一篇:qq怎么管理定位服务器?

下一篇:美国网站服务器解析,速度与稳定性兼备的秘密解析