码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Spring Boot 配置文件加密


    方式一:Spring Cloud Config

    一、建立config server

    1. build.gradle 文件中添加:
    plugins {
    	id 'java'
    	id 'org.springframework.boot' version '2.7.0'
    	id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    }
    
    ext {
    	set('springCloudVersion', "2021.0.8")
    }
    
    dependencies {
    	implementation 'org.springframework.cloud:spring-cloud-config-server'
    	// 不加该依赖无法调用 /encrypt 和 /decrypt 接口:
    	implementation 'org.springframework.cloud:spring-cloud-starter-config'
    }
    
    dependencyManagement {
    	imports {
    		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    2. application.yaml 文件中添加:
    • 存放配置文件的git仓库。可以是git remote repo,也可以是local repo(file://…)
    spring:
      cloud:
        config:
          server:
            git:
              uri: https://gitlab.some.domain/somerepo.git
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 加解密的密钥
    encrypt:
      key: key_instance
    
    • 1
    • 2
    3. Application 启动类添加 @EnableConfigServer 注解:

    (不加该注解也可实现加解密功能)

    @EnableConfigServer
    @SpringBootApplication
    public class DemoApplication {
    	...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    二、获得加密后的密文

    获得加密后的密文:

    $ curl http://localhost:8080/encrypt -d id111
    7b04915ff83caae96116140fe180be4b9a217fbdaf4c3d38263c2479819d5202
    
    • 1
    • 2

    获得解密后的明文:

    $ curl http://localhost:8080/decrypt -d 7b04915ff83caae96116140fe180be4b9a217fbdaf4c3d38263c2479819d5202
    id111
    
    • 1
    • 2

    三、将加密后的密文添加进配置文件

    application.yaml 文件中添加:

    • 加密后的密文
      id: '{cipher}7b04915ff83caae96116140fe180be4b9a217fbdaf4c3d38263c2479819d5202'
      
      • 1

    启动后项目中的id属性会自动解密为“id111"。

    Spring Cloud Config 配置的加密解密
    分布式配置中心Spring Cloud Config初窥
    Spring Cloud Config

    SpringCloud与SpringBoot的版本适配

    建立 config client:

    Spring Cloud Config(统一配置中心服务端和客户端)

    方式二:jasypt

    【开发经验】springboot配置文件加密详解
    SpringBoot 配置文件这样加密,才足够安全!
    jasypt对springboot配置文件加密解密及在测试加密解密中遇到的坑
    云服务器运行报错Value for key ‘PROMPT_COMMAND‘ could not be loaded using ‘com.ulisesbocchio.jasyptspringboot
    JASYPT with Spring Boot problem encrypt strong password and using it in the app

  • 相关阅读:
    经纬恒润智能感知后视镜亮相北京车展
    虚拟机的网络模式
    长难句5.15
    招生CRM系统|基于Springboot实现培训机构招生CRM管理系统
    SpringBoot源码 | printBanner方法解析
    云计算与大数据第8章 大数据采集习题及答案
    PostgreSQL 和 MySQL 在用途、好处、特性和特点上的异同
    飞机大作战(c语言)
    同源策略,跨域,请求,网络安全详细知识
    通过动态IP解决网络数据采集问题
  • 原文地址:https://blog.csdn.net/weixin_44992607/article/details/132667435
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号