基于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依赖

  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



扩展阅读:


posted @ 2018-04-13 10:02  maventalker  阅读(216)  评论(0编辑  收藏  举报