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

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

使用过SpringBoot配置文件的朋友都知道,资源文件中的内容通常情况下是明文显示,安全性就比较低一些。打开application.properties或application.yml,比如mysql登陆密码,redis登陆密码以及第三方的密钥等等一览无余,这里介绍一个加解密组件,提高一些属性配置的安全性。

jasypt[http://www.jasypt.org/],官方给出的释意是:

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/ulisesbocc...,下面介绍下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 {
    
    @Autowired
    StringEncryptor encryptor;
    
    @Test
    public void getPass() {
        String result = encryptor.encrypt("root");
        System.out.println(result); 
        Assert.assertTrue(result.length() > 0);
    }
}

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

  • 旧有配置
#mysql database config
spring.datasource.url=jdbc:mysql://localhost:3306/micro_account?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull
#use jasypt to encrypt username/password
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
  • 升级后配置
#mysql database config
spring.datasource.url=jdbc:mysql://localhost:3306/micro_account?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull
#use jasypt to encrypt username/password
spring.datasource.username=ENC(BnBr3/idF0PH9nd20A9BXw==)
spring.datasource.password=ENC(BnBr3/idF0PH9nd20A9BXw==)
spring.datasource.driverClassName=com.mysql.jdbc.Driver

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

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

扩展阅读:

基于SpringCloud的Microservices架构实战案例-序篇

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

Spring Boot + Elasticsearch 实现索引的日常维护

微服务体系下如何快速构建一个服务

介绍几款常用的在线API管理工具

如何从传统软件开发顺利过渡到互联网技术开发

学习新技术时你应当掌握的『最少必要知识』

做了七年软件开发后反而更迷茫

软技能:代码之外的生存指南

程序员,保护你的好奇心和求知欲

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值