锋盈数科-知识库 Logo
首页
软件开发
计算机基础
Hello Halo
新手必读
关于本知识库
登录 →
锋盈数科-知识库 Logo
首页 软件开发 计算机基础 Hello Halo 新手必读 关于本知识库
登录
  1. 首页
  2. 软件开发
  3. springboot 配置文件密码加密的3种方案

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

0
  • 软件开发
  • 发布于 2024-07-29
  • 13 次阅读
黄健
黄健

原文链接:https://blog.csdn.net/SevenBean/article/details/137857983

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

一.配置文件添加jasypt

1.pom.xml添加jasypt引用

<dependency>

    <groupId>com.github.ulisesbocchio</groupId>

    <artifactId>jasypt-spring-boot-starter</artifactId>

</dependency>

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

jasypt:

  encryptor:

    algorithm: PBEWithMD5AndDES ##盐

    password: dddda123 #密钥

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

1.pom.xml添加jasypt依赖

<dependency>

    <groupId>com.github.ulisesbocchio</groupId>

    <artifactId>jasypt-spring-boot-starter</artifactId>

</dependency>

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 自动配置加密信息

 //

@Configuration

public 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依赖:

<dependency>

    <groupId>org.jasypt</groupId>

    <artifactId>jasypt-spring-boot-starter</artifactId>

    <version>3.0.3</version>

</dependency>

2、获取加密后的字符串

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

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

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

spring.datasource.password=ENC(1WqJf1V5n1R4n5v2k3P4q5I7r2Q3u5Q1)

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

export JASYPT_ENCRYPTOR_PASSWORD=mySecretPassword

export 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来获取加密后的密码。

标签: #软件开发 1171 #JAVA 991 #新手必读 21
相关文章

万字:支付“核心系统”详解 2024-11-02 15:33

专栏作者:隐墨星辰 \| 主编:陈天宇宙 这篇文章也尝试化繁为简,探寻支付系统的本质,讲清楚在线支付系统最核心的一些概念和设计理念。 虽然支付行业已经过了风头最劲的时光,但跨境支付仍然在蓬勃发展,每年依然有很多新人进入这个行业,这篇文章尝试为这些刚入行的新人提供一点帮助。 文章只介绍一些支付行业十几

资深支付架构师视角:实战从问题定义到代码落地的完整套路 2024-11-02 15:33

前言 今天从一个实际案例入手,介绍站在架构师的角度,如何识别并定义问题,提炼需求,技术方案选型,再到详细设计,最后利用AI的能力协助写出核心的代码,验证与调优。 解决问题存在一定的模式,也可以称之为框架,总结出自己的思考和解题框架,以后再碰到同类型的问题就可以如庖丁解牛一样容易。 很多年前,我写代码

Spring 实现 3 种异步接口 2024-10-18 09:07

大家好,我是苏三~ 如何处理比较耗时的接口? 这题我熟,直接上异步接口,使用 Callable、WebAsyncTask 和 DeferredResult、CompletableFuture等均可实现。 但这些方法有局限性,处理结果仅返回单个值。在某些场景下,如果需要接口异步处理的同时,还持续不断地

重学SpringBoot3-集成Redis(五)之布隆过滤器 2024-10-08 11:24

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(五)之布隆过滤器 1. 什么是布隆过滤器? * 基本概念 适用场景 2. 使用 Redis 实现布隆过滤器 * 项目依赖 Redis 配置

设计模式第16讲——迭代器模式(Iterator) 2024-10-08 11:24

一、什么是迭代器模式 迭代器模式是一种行为型设计模式,它提供了一种统一的方式来访问集合对象中的元素,而不是暴露集合内部的表示方式。简单地说,就是将遍历集合的责任封装到一个单独的对象中,我们可以按照特定的方式访问集合中的元素。 二、角色组成 抽象迭代器(Iterator):定义了遍历聚合对象所需的方法

vue2路由和vue3路由区别及原理 2024-10-08 11:24

一、Vue2 与 Vue3 路由的区别 1. 创建路由实例方式的不同 Vue 2 中,通过 Vue.use() 注册路由插件,并通过 new VueRouter() 来创建路由实例。 import Vue from 'vue';import VueRouter from 'vue-router';i

目录

IT 外包服务商

  • 意见投递
  • zyf6619

软件开发应用

主菜单

  • 首页
  • 软件开发
  • 计算机基础
  • Hello Halo
  • 新手必读
  • 关于本知识库
Copyright © 2024 your company All Rights Reserved. Powered by Halo.