Spring Cloud Config 配置中心 自动加解密功能

使用  jasypt-spring-boot-starter  进行加解密功能。

整个流程说明:

配置一个 spring cloud config server ,将要使用的配置文件存放到github上,然后从这个配置源拿配置。

我们使用 jasypt 进行自动加解密,将需要加密的数据,通过jasypt进行加密,然后将该内容放入 github。如下图:

使用 ENC() 将加密后的原文包裹,这样spring cloud config client 客户端拿到这个串之后,会自动解密,拿到原文。

下面看一下整体步骤:

1、首先创建spring cloud config server 服务端程序

  此处我就不写步骤了,更普通的服务端没有任何区别,唯一的不同就是,在github上面存储的配置文件中的信息,是经过加密的。如上图。

2.、创建客户端(使用spring boot 1.5.10, jasypt 1.16)

项目按照普通结构创建即可,额外需要加入  jasypt-spring-boot-starter 包。

pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.thunisoft</groupId>
	<artifactId>thunisoft-microservice-testconfig</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>thunisoft-microservice-testconfig</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Edgware.SR2</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>1.16</version>
        </dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

  

3、添加配置:在application.yml / application.properties中添加如下配置:

jasypt:
  encryptor:
    password: foo
    algorithm: PBEWithMD5AndDES

  algorithm :配置要使用的加密算法,默认值是 PBEWithMD5AndDES

  password:相当于是 加密中的 “盐(salt)”

4、之后就可以按照正常取配置文件的流程进行了。

package com.thunisoft.thunisoftmicroservicetestconfig.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DisplayConfigController {

    @Value("${profile}")
    private String profile;

	@GetMapping("/")
	public String showConfig() {
        return this.profile;
	}
}

  

原文地址:https://www.cnblogs.com/hfultrastrong/p/8556145.html

时间: 2024-08-26 18:45:14

Spring Cloud Config 配置中心 自动加解密功能的相关文章

微服务SpringCloud之Spring Cloud Config配置中心SVN

在回来的路上看到一个个的都抱着花,吃了一路的狗粮,原本想着去旁边的工业园里跑跑步呢,想想还是算了,人家过七夕,俺们过巴西.上一博客学习了Spring Cloud Config使用git作为配置中心,本篇学习下使用svn作为配置中心. 一.Server 端 1.准备配置文件 这里在本地电脑安装了下svn server,并在https://cuiyw/svn/config-repo/config目录下提交了上一博客的3个配置文件. 2.创建Spring Cloud Config SVN  Serve

Spring Cloud Config 配置中心

1.构建config-server 创建一个pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://

Spring Cloud学习--配置中心(Config)

Spring Cloud学习--配置中心(Config) 一 Spring Cloud Config简介 二 编写 Config Server 三 编写Config Client 四 使用refresh端点手动刷新配置 五 Spring Config Server与Eurelka配合使用 六 Config Server的高可用 一. Spring Cloud Config简介 微服务要实现集中管理微服务配置.不同环境不同配置.运行期间也可动态调整.配置修改后可以自动更新的需求,Spring Cl

spring cloud 的配置中心config

为啥要配置中心? 微服务架构中,每个项目都有一个yml配置,管理起来麻烦.要使用spring cloud config来统一管理. 它支持配置服务放在配置服务的内存中(即本地),也支持放在远程码云(码云中国的github)仓库中. 在spring cloud config 组件中,分两个角色,一是config server(配置中心),二是config client(客户端配置). 1.1.1. 架构 操作 一:操作码云 1.在码云中建立仓库 下一步 最好选择私有,据说访问要快些 下一步,在新建

用Zookeeper作为Spring cloud的配置中心(转)

本文转自https://blog.csdn.net/CSDN_Stephen/article/details/78856323 Spring Cloud 配置中心的主流实现方式 Spring cloud configSpring cloud zookeeper config以下是这两者的简介 Srping Cloud Config Spring cloud config就是和git(svn)集成来实现配置中心.Spring cloud config分服务端.客户端和git(svn)三部分,服务端

spring cloud config配置中心源码分析之注解@EnableConfigServer

spring cloud config的主函数是ConfigServerApplication,其定义如下: @Configuration @EnableAutoConfiguration @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { new SpringApplicationBuilder(ConfigServerApplication.cl

Spring Cloud Config 配置属性覆盖优先级。

/** * Flag to indicate that the external properties should override system properties. * Default true. */ private boolean overrideSystemProperties = true; /** * Flag to indicate that {@link #isSystemPropertiesOverride() * systemPropertiesOverride} ca

spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config

我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分. 其中服务端也称为分布式配置中心,他是独立的微服务应用,用来连接配置仓库并为客户端提供获取接口(这些接口返回配置信息.加密.解密信息等): 客户端是微服务架构中的各个微服务应用或基础设施,它们通过制定的配置中心来管理

Spring Cloud Config分布式配置中心的使用和遇到的坑

分布式配置中心 为什么要有用分布式配置中心这玩意儿?现在这微服务大军已经覆盖了各种大小型企业,每个服务的粒度相对较小,因此系统中会出现大量的服务,每个服务都要有自己都一些配置信息,或者相同的配置信息,可能不同环境每个服务也有单独的一套配置,这种情况配置文件数量比较庞大,维护起来相当费劲,举个栗子: 在开发的过程中,一般数据库是开发环境数据库,所有服务DB的IP配置为:92.168.0.1,突然老大说,开发环境换了,DB的IP要修改,这下可不好受了,所有模块挨个修改DB的配置,就问你难受不难受?