使用过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依赖


  1. <!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->

  2. <dependency>

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

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

  5.    <version>1.14</version>

  6. </dependency>

2、配置加密参数

  • 在application.yml 中增加配置


  1. jasypt:

  2.  encryptor:

  3.    #这里可以理解成是加解密的时候使用的密钥

  4.    password: your password

  • 在application.properties中增加配置


  1. jasypt.encryptor.password=your password

此处密码的生成可以通过两种方式生成,写main函数生成和直接采用jar命令方式。本处采用main函数的方式,此代码位于account-serv的test包中。


  1. package com.simplemall.account.test;

  2. import org.jasypt.encryption.StringEncryptor;

  3. import org.junit.Assert;

  4. import org.junit.Test;

  5. import org.junit.runner.RunWith;

  6. import org.springframework.beans.factory.annotation.Autowired;

  7. import org.springframework.boot.test.context.SpringBootTest;

  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

  9. import org.springframework.test.context.web.WebAppConfiguration;

  10. import com.simplemall.account.AccountServApplication;

  11. @RunWith(SpringJUnit4ClassRunner.class)

  12. @WebAppConfiguration

  13. @SpringBootTest(classes = AccountServApplication.class)

  14. public class Jasyptest {

  15.    @Autowired

  16.    StringEncryptor encryptor;

  17.    @Test

  18.    public void getPass() {

  19.        String result = encryptor.encrypt("root");

  20.        System.out.println(result);

  21.        Assert.assertTrue(result.length() > 0);

  22.    }

  23. }

3、升级application.properties/yml中相应的配置项

  • 旧有配置


  1. #mysql database config

  2. spring.datasource.url=jdbc:mysql://localhost:3306/micro_account?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull

  3. #use jasypt to encrypt username/password

  4. spring.datasource.username=root

  5. spring.datasource.password=root

  6. spring.datasource.driverClassName=com.mysql.jdbc.Driver

  • 升级后配置


  1. #mysql database config

  2. spring.datasource.url=jdbc:mysql://localhost:3306/micro_account?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull

  3. #use jasypt to encrypt username/password

  4. spring.datasource.username=ENC(BnBr3/idF0PH9nd20A9BXw==)

  5. spring.datasource.password=ENC(BnBr3/idF0PH9nd20A9BXw==)

  6. spring.datasource.driverClassName=com.mysql.jdbc.Driver

至此,配置完成,效果就如你在simplemall源码中看到的那样,针对配置文件中相关属性做了一次安全升级。

源码:https://github.com/backkoms/simplemall

扩展阅读:

基于SpringCloud的Microservices架构实战案例-配置文件属性内容加解密的更多相关文章

  1. 基于SpringCloud的Microservices架构实战案例-在线API管理

    simplemall项目前几篇回顾: 1基于SpringCloud的Microservices架构实战案例-序篇 2基于SpringCloud的Microservices架构实战案例-架构拆解 3基于 ...

  2. 基于SpringCloud的Microservices架构实战案例-架构拆解

    自第一篇< 基于SpringCloud的Microservices架构实战案例-序篇>发表出来后,差不多有半年时间了,一直也没有接着拆分完,有如读本书一样,也是需要契机的,还是要把未完成的 ...

  3. 基于SpringCloud的Microservices架构实战案例

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  4. 基于SpringCloud的微服务架构实战案例项目,以一个简单的购物流程为示例

    QuickStart 基于SpringCloud体系实现,简单购物流程实现,满足基本功能:注册.登录.商品列表展示.商品详情展示.订单创建.详情查看.订单支付.库存更新等等. 每个业务服务采用独立的M ...

  5. 基于SpringCloud的微服务架构实战案例项目

    QuickStart 基于SpringCloud体系实现,简单购物流程实现,满足基本功能:注册.登录.商品列表展示.商品详情展示.订单创建.详情查看.订单支付.库存更新等等. github源码地址:h ...

  6. Spring Cloud Config 配置中心 自动加解密功能 JCE方式

    1.首先安装JCE JDK8的下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.h ...

  7. Spring Cloud Config 配置中心 自动加解密功能 jasypt方式

    使用此种方式会存在一种问题:如果我配置了自动配置刷新,则刷新过后,加密过后的密文无法被解密.具体原因分析,看 SpringCloud 详解配置刷新的原理 使用  jasypt-spring-boot- ...

  8. 基于springCloud的分布式架构体系

    Spring Cloud作为一套微服务治理的框架,几乎考虑到了微服务治理的方方面面,之前也写过一些关于Spring Cloud文章,主要偏重各组件的使用,本次分享主要解答这两个问题:Spring Cl ...

  9. SharePoint 2013 开发——获取用户配置文件属性内容(User Profile)

    博客地址:http://blog.csdn.net/FoxDave 本篇我们应用SharePoint CSOM(.NET)来读取用户配置文件的信息,个人开始逐渐倾向于客户端模型,因为不用远程登录到 ...

随机推荐

  1. Plupload上传插件中文文档

    Plupload上传插件中文帮助文档 如有疑问,加群交流:646104701 下载地址:https://www.plupload.com/download/ 配置参数 实例化一个plupload对象时 ...

  2. mariadb 允许远程访问

    进入MariaDB服务器,将mysql.user的host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%. 1 MariaDB [(none)]> ...

  3. Directory.GetFiles()获取多个类型格式的文件

    第一种(用通配符) string[] fileNameX = Directory.GetFiles(@"D:\Sjdc", "*.xls?"); Array a ...

  4. 零元学Expression Blend 4 - Chapter 14 用实例了解布局容器系列-「Pathlistbox」II

    原文:零元学Expression Blend 4 - Chapter 14 用实例了解布局容器系列-「Pathlistbox」II 本章将延续上一章的范例,步骤解析. 本章将延续上一章的范例,步骤解析 ...

  5. 网站运行编译器错误CS1617: 选项“6”对 /langversion 无效;必须是 ISO-1、ISO-2、3、4、5 或 Default

    运行winform程序时提示, CS1617: 选项“6”对 /langversion 无效:必须是 ISO-1.ISO-2.3.4.5 或 Default 找到网站的web.config配置文件,找 ...

  6. .NET重思(一)sealed和interface

    博主这几天正好闲着,砸砸基础,毕竟自学,基础不牢靠很致命,要踏实啊~~~ 1.sealed关键字修饰一个类,也可以修饰实现方法或者属性.当sealed用于修饰一个类时,这个类不可以被继承,因此,这个类 ...

  7. 使用PyQt5编写一个简单的GUI程序(pyside 有 pyside-uic 把ui文件转成py文件,pyside-rcc 把qrc文件转成 py文件导入就行了)

    我做Python窗口界面编程时,经常使用PyQt进行设计.这里简单叙述一下使用PyQt5制作一个简单的图形界面的流程 PyQt的简介以及开发环境的搭建在此不多赘述. 1.       打开Qt Des ...

  8. Qt单元测试浅析

    Qt单元测试框架,使用于基于Qt的应用程序和库,先从一个简单的demo工程说起吧. 我们可以通过QtCreator来创建一个简单的Qt单元测试工程,夏天到了,这个demo工程的名字就叫Summer好了 ...

  9. web.congfig 禁用 ViewState Session

    <!--禁用 ViewState Session--> <pages enableViewState="false" enableSessionState=&qu ...

  10. 在C#中创建文件快捷方式

    创建快捷方式对于绝大多数 Windows 用户来说都是小菜一碟了,然而,这项工作却为程序员带来不少麻烦..NET 没有提供简便直接的创建快捷方式的方法,那么在 .NET 中我们如何为应用程序创建快捷方 ...