基于SpringCloud的Microservices架构实战案例-配置文件属性内容加解密
使用过SpringBoot配置文件的朋友都知道,资源文件中的内容通常情况下是明文显示,安全性就比较低一些。打开application.properties或application.yml,比如mysql登陆密码,redis登陆密码以及第三方的密钥等等一览无余,这里介绍一个加解密组件,提高一些属性配置的安全性。
jasypt,官方给出的释意是:
Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.
simplemall项目也采用此加密组件,结合Spring Boot使用。国外大神Ulises Bocchio写了一个Spring Boot下用的工具包,Github地址:https://github.com/ulisesbocchio/jasypt-spring-boot,下面介绍下jasypt在Spring Boot的用法。
1、引入maven依赖
<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter --><dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>1.14</version></dependency>
2、配置加密参数
在application.yml 中增加配置
jasypt:encryptor:#这里可以理解成是加解密的时候使用的密钥password: your password
在application.properties中增加配置
jasypt.encryptor.password=your password
此处密码的生成可以通过两种方式生成,写main函数生成和直接采用jar命令方式。本处采用main函数的方式,此代码位于account-serv的test包中。
package com.simplemall.account.test;import org.jasypt.encryption.StringEncryptor;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import com.simplemall.account.AccountServApplication;@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@SpringBootTest(classes = AccountServApplication.class)public class Jasyptest {@AutowiredStringEncryptor encryptor;@Testpublic void getPass() {String result = encryptor.encrypt("root");System.out.println(result);Assert.assertTrue(result.length() > 0);}}
3、升级application.properties/yml中相应的配置项
旧有配置
#mysql database configspring.datasource.url=jdbc:mysql://localhost:3306/micro_account?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull#use jasypt to encrypt username/passwordspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driverClassName=com.mysql.jdbc.Driver
升级后配置
#mysql database configspring.datasource.url=jdbc:mysql://localhost:3306/micro_account?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull#use jasypt to encrypt username/passwordspring.datasource.username=ENC(BnBr3/idF0PH9nd20A9BXw==)spring.datasource.password=ENC(BnBr3/idF0PH9nd20A9BXw==)spring.datasource.driverClassName=com.mysql.jdbc.Driver
至此,配置完成,效果就如你在simplemall源码中看到的那样,针对配置文件中相关属性做了一次安全升级。
源码:https://github.com/backkoms/simplemall
扩展阅读:
基于SpringCloud的Microservices架构实战案例-配置文件属性内容加解密的更多相关文章
- 基于SpringCloud的Microservices架构实战案例-在线API管理
simplemall项目前几篇回顾: 1基于SpringCloud的Microservices架构实战案例-序篇 2基于SpringCloud的Microservices架构实战案例-架构拆解 3基于 ...
- 基于SpringCloud的Microservices架构实战案例-架构拆解
自第一篇< 基于SpringCloud的Microservices架构实战案例-序篇>发表出来后,差不多有半年时间了,一直也没有接着拆分完,有如读本书一样,也是需要契机的,还是要把未完成的 ...
- 基于SpringCloud的Microservices架构实战案例
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- 基于SpringCloud的微服务架构实战案例项目,以一个简单的购物流程为示例
QuickStart 基于SpringCloud体系实现,简单购物流程实现,满足基本功能:注册.登录.商品列表展示.商品详情展示.订单创建.详情查看.订单支付.库存更新等等. 每个业务服务采用独立的M ...
- 基于SpringCloud的微服务架构实战案例项目
QuickStart 基于SpringCloud体系实现,简单购物流程实现,满足基本功能:注册.登录.商品列表展示.商品详情展示.订单创建.详情查看.订单支付.库存更新等等. github源码地址:h ...
- Spring Cloud Config 配置中心 自动加解密功能 JCE方式
1.首先安装JCE JDK8的下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.h ...
- Spring Cloud Config 配置中心 自动加解密功能 jasypt方式
使用此种方式会存在一种问题:如果我配置了自动配置刷新,则刷新过后,加密过后的密文无法被解密.具体原因分析,看 SpringCloud 详解配置刷新的原理 使用 jasypt-spring-boot- ...
- 基于springCloud的分布式架构体系
Spring Cloud作为一套微服务治理的框架,几乎考虑到了微服务治理的方方面面,之前也写过一些关于Spring Cloud文章,主要偏重各组件的使用,本次分享主要解答这两个问题:Spring Cl ...
- SharePoint 2013 开发——获取用户配置文件属性内容(User Profile)
博客地址:http://blog.csdn.net/FoxDave 本篇我们应用SharePoint CSOM(.NET)来读取用户配置文件的信息,个人开始逐渐倾向于客户端模型,因为不用远程登录到 ...
随机推荐
- laravel 增删修改
1.查 public function userTest(){ return $this->all(); } public function userTest(){ return $this-& ...
- Win8Metro(C#)数字图像处理--2.4图像颜色聚类
原文:Win8Metro(C#)数字图像处理--2.4图像颜色聚类 [函数名称] 图像颜色聚类函数ClusterProcess(WriteableBitmap src,int value) [算 ...
- android中优雅实现recyclerview
在慕课网上看了Abner_泥阿布老师不一样的RecyclerView优雅实现复杂列表布局这个视频课程,本人在Ubuntu16.04LTS下使用android studio2.2.3按照老师讲解写的代码 ...
- 零元学Expression Blend 4 - Chapter 40 Flash做的到的Blend也可以!轻松制作拥有动画的MenuBar!(上)
原文:零元学Expression Blend 4 - Chapter 40 Flash做的到的Blend也可以!轻松制作拥有动画的MenuBar!(上) 一直以来都有人拿Flash的动画问我Blend ...
- delphi常用正则表达式
function checkanystr(str: string; mytype: integer):Boolean;var myper: TPerlRegEx; areg: string;beg ...
- Change Default Route
route delete 0.0.0.0route add 0.0.0.0 mask 0.0.0.0 10.226.4.14
- Qt+VS编译器:默认库“library”与其他库的使用冲突;使用 /NODEFAULTLIB:library(我曾经碰到过,修改qmake.conf,但我修改的是VS的IDE配置)good
找到qt安装目录下的mkspecs文件夹,在里面找到你使用的对应版本编译器,打开qmake.conf.稍等: /MD:动态链接多线程库(msvcrt.lib).使用该选项时,需要用/NODEFAULT ...
- nginx(一) nginx详解
nginx是一个被广泛使用的集群架构组件,我们有必要对它有足够的了解.下面将先认识nginx:包括应用场景.nginx基本架构.功能特性.并发模型以及配置说明,最后我们再总结下,为什么选择nginx的 ...
- JavaScript规定了几种语言类型?
JavaScript中的每一个值都有它自己的类型,JavaScript规定了七种语言类型: 1.Undefined 2.Null 3.Boolean 4.String 5.Number 6.Symbo ...
- Spark之常用操作
-- 筛选 val rdd = sc.parallelize(List("ABC","BCD","DEF")) val filtered = ...