13、SpringBoot-配置文件里密码加密
系列导航
6、SpringBoot-mybatis分页实现pagehelper
9、SpringBoot-mybatis-druid多源数据多源数据
10、SpringBoot-mybatis-plus-druid多源数据
11、SpringBoot-mybatis-plus-druid多源数据事务
12、SpringBoot-mybatis-plus-ehcache
14、SpringBoot-easyexcel导出excle
完结
springboot连接数据库,数据库的用户名、密码默认多是明文放在配置文件里,如何提高安全性不要明文写在配置文件里,不问就解决这个问题。
1、数据库中创建表
CREATE TABLE TEST_BLOCK_T
(
BLOCK_ID VARCHAR2(10 BYTE) PRIMARY KEY, --编码
BLOCK_NAME VARCHAR2(200 BYTE) --资源名称
);
Insert into TEST_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('1', '哈哈哈');
COMMIT;
2、pom.xml依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <!--密码加密-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
3、工程结构

4、源码
package com.example.demo.controller; import org.jasypt.encryption.StringEncryptor;
import org.jasypt.util.text.BasicTextEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/hello")
public class HelloController { @Autowired
JdbcTemplate jdbcTemplate; @Autowired
StringEncryptor stringEncryptor; //访问数据库的数据
@GetMapping("/list")
@ResponseBody
public String index() { String sql = "SELECT BLOCK_NAME FROM TEST_BLOCK_T WHERE BLOCK_ID = ?"; // 通过jdbcTemplate查询数据库
String mobile = (String) jdbcTemplate.queryForObject(sql, new Object[]{1}, String.class); return "Hello " + mobile;
} //对配置文件中的用户名密码加密,加密盐也是用配置文件里的
@GetMapping("/passwd")
@ResponseBody
public String passwd() { //加密密码
String name = stringEncryptor.encrypt("zy");
String pwd = stringEncryptor.encrypt("1");
System.out.println("name:"+name);
System.out.println("pwd:"+pwd);
return "success!"; } //解密配置文件中的用户名和密码,加密盐也是用配置文件里的
@GetMapping("/unpasswd")
@ResponseBody
public String unpasswd() {
//解密密码
String name = stringEncryptor.decrypt("YCfAQQPOSw5Jp/uzmA8LkQ==");
String pwd = stringEncryptor.decrypt("hOwW0zxYpHaEH/lkpHyJaA==");
System.out.println("name:"+name);
System.out.println("pwd:"+pwd);
return "success!"; } //对配置文件中的用户名密码加密,加密盐使用代码里的
@GetMapping("/passwd1")
@ResponseBody
public String passwd1() { BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt(盐),自定义
textEncryptor.setPassword("hello");
//要加密的数据(数据库的用户名或密码)
String username = textEncryptor.encrypt("zy");
String password = textEncryptor.encrypt("1");
System.out.println("username:"+username);
System.out.println("password:"+password); return "success!"; } }
package com.example.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }
5、配置文件
# 应用名称
spring.application.name=demo
# 应用服务 WEB 访问端口
server.port=8080 # 数据库设置
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@192.168.0.100:1521:orcl
#spring.datasource.username=zy
#spring.datasource.password=1
jasypt.encryptor.password=hello
spring.datasource.username=ENC(YCfAQQPOSw5Jp/uzmA8LkQ==)
spring.datasource.password=ENC(hOwW0zxYpHaEH/lkpHyJaA==)
6、测试
(1)用户名密码明文连接数据库
配置文件

访问查询数据库的接口 http://localhost:8080/hello/list
返回值:Hello 哈哈哈
(2)用户名密码明文连接数据库
<1>配置文件中打开jasypt.encryptor.password的参数,让系统知道加密盐的值。
完整配置如下:

<2>调用接口 http://localhost:8080/hello/passwd 获取加密后的用户名和密码
输出的结果是:
name:YCfAQQPOSw5Jp/uzmA8LkQ==
pwd:hOwW0zxYpHaEH/lkpHyJaA==
修改配置文件如下,其中EMC()就是告诉系统里面的内容是加密过的需要解密

<3>重启项目 调用接口 http://localhost:8080/hello/list
正常返回数据结果:Hello 哈哈哈
到此给配置文件中的敏感信息加密就完成了。这样做有个缺点就是加密盐是在配置文件里的,对方如果非要解密是可以做到的,例如:
<4>密文解密
将加密后的密文放到如下代码中

调用接口http://localhost:8080/hello/unpasswd
结果:
name:zy
pwd:1
到此成功解密出了之前加密的密文。所以之前的加密只能拦住不懂这个加密的人,真正懂得人是拦不住的依然可以解密出来。那还有没有更安全一些的做法?有
<5>启动时指定加密盐
先将程序打成jar包 如何打包参考https://www.cnblogs.com/yclh/p/15947054.html
打包后将配置文件中的如下这段去掉,配置文件中就没有加密盐的信息了
jasypt.encryptor.password=hello
启动的jar包的时候使用如下语句,其中“hello”就是加密盐,SpringBoot_PassWord.jar就是打好的jar包
java -jar -Djasypt.encryptor.password=hello SpringBoot_PassWord.jar

启动成功后访问接口:http://localhost:8080/hello/list
成功返回参数:Hello 哈哈哈
还有没有其他更安全的方式,网上看到也有单独搞个配置文件,设置环境变量等操作起来有点麻烦,个人感觉就没有绝对安全的,只要想破解的人懂你设置的那个套路就能破解,不管你把这个加密盐藏在哪里总归是有个地方存的。所以最好的加密就是你搞得东西别人不懂,懂得人欺负不懂得人。
13、SpringBoot-配置文件里密码加密的更多相关文章
- springboot对数据库密码加密
第一步:maven引jar包 <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifa ...
- SpringBoot项目配置文件中密码的加密
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/15565862.html 版权声明:本文为博主原创文章,转载请附上博文链接! 公众号:追梦1819 ...
- Spring Boot 配置文件密码加密两种方案
Spring Boot 配置文件密码加密两种方案 jasypt 加解密 jasypt 是一个简单易用的加解密Java库,可以快速集成到 Spring 项目中.可以快速集成到 Spring Boot 项 ...
- 配置 Druid 数据源及密码加密-SpringBoot 2.7 实战基础
在SpringBoot中配置 Druid 数据源及密码加密的方法 前文集成 MyBatis Plus,实现了一组增删改查接口.在启动服务时,从控制台中可以看出 Spring Boot 默认使用 Hik ...
- 解决spring-boot配置文件使用加密方式保存敏感数据启动报错No decryption for FailsafeTextEncryptor. Did you configure the keystore correctly
spring-boot配置文件使用加密方式保存敏感数据 application.yml spring: datasource: username: dbuser password: '{cipher} ...
- Jasypt加密SpringBoot配置文件
如果 SpringBoot 的 properties 文件中含有用户名密码等敏感信息,为了安全起见需要对明文密码加密.Jasypt 是用来加密的 jar 包. 1.引入 Jasypt 在 pom.xm ...
- 【SpringBoot】SpringBoot集成jasypt数据库密码加密
一.为什么要使用jasypt库? 目前springboot单体应用项目中,甚至没有使用外部配置中心的多服务的微服务架构的项目,开发/测试/生产环境中的密码往往是明文配置在yml或properties文 ...
- Springboot 配置文件、隐私数据脱敏的最佳实践(原理+源码)
大家好!我是小富- 这几天公司在排查内部数据账号泄漏,原因是发现某些实习生小可爱居然连带着账号.密码将源码私传到GitHub上,导致核心数据外漏,孩子还是没挨过社会毒打,这种事的后果可大可小. 说起这 ...
- tomcat安全配置之证书密码加密存储
最近项目组要完成一个新Web Servicer接口的开发,其中有项要求是支持外部客户程序以https方式访问这些SOAP接口.项目组当前基于tomcat6.0.29开发,axis版本为1.4.拿到这个 ...
- mysql-8.0 安装教程(自定义配置文件,密码方式已修改)
下载zip安装包: MySQL8.0 For Windows zip包下载地址:https://dev.mysql.com/downloads/file/?id=476233,进入页面后可以不登录.后 ...
随机推荐
- Vue源码学习(十七):实现computed计算属性
好家伙,本章我们尝试实现computed属性 0.完整代码已开源 https://github.com/Fattiger4399/analytic-vue.git 1.分析 1.1computed的常 ...
- 深入理解HarmonyOS UIAbility:生命周期、WindowStage与启动模式探析
本文分享自华为云社区<深入理解HarmonyOS UIAbility:生命周期.WindowStage与启动模式探析>,作者:柠檬味拥抱. UIAbility组件概述 UIAbility组 ...
- java中C3P0、Druid、HikariCP 、DBCP连接池的jar包下载与IDEA配置
## 一.什么是连接池连接池是应用程序与数据库之间的一个缓冲区,它存储了一定数量的空闲数据库连接,当应用程序需要连接数据库时,可以从连接池中获取一个可用连接,使用完毕后再将连接归还给连接池,从而避免了 ...
- 精通TypeScript:打造一个炫酷的天气预报插件
前言 随着数字化和信息化的发展,数据大屏使用越来越广泛,我们不仅需要展示数据,更需要以一种更加美观的方式展示数据.这就必然需要使用到各种图表组件,比如柱状图.饼图.折线图等等.但是有一些效果不太适 ...
- 倒计时4天!解锁《2023 .NET Conf China》 云原生分会场精彩议程
.NET Conf China 2023 定于 12 月16 日于北京举办为期一天的技术交流,届时会有.NET 领域专家与大家一同庆祝 .NET 8 的发布和回顾过去一年来 .NET 在中国的发展成果 ...
- [USACO2007NOVS] Cow Hurdles S
题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gan ...
- ProtocolBuffer详细教程
下面大佬写的特别详细,直接参考他的吧! 推荐参考大佬写的ProtocolBuffer详细教程
- C++ Qt开发:自定义Dialog对话框组件
Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍自定义Dial ...
- HTML之CSS Animation 属性常用动画
引入下面的样式表后 -webkit-animation: tada 1s ease 0.3s infinite both; -webkit-animation: name duration timin ...
- 快速上手 dbt 数据转换工具 -- dbt core 命令进阶篇
引 根据第一篇文章的约定,我想通过接下来的几篇文章带大家进一步了解 dbt 的用法,原计划这篇文章我会介绍 dbt 命令的进阶用法,进一步认识 dbt 的配置以及如何创建增量表等等零零散散十几个方面的 ...