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,进入页面后可以不登录.后 ...
随机推荐
- 瀑布图有什么作用?除了excel如何快速制作?
瀑布图是一种特殊的数据可视化图表,具有以下作用: 1. 对比变化:瀑布图可以清晰地展示数据在不同因素作用下的变化情况.通过将数据分解成各个组成部分,并以阶梯状呈现,可以直观地对比每个因素对总体结果的影 ...
- 吉特日化MES实施--三种浪费
在实施吉特日化MES系统的过程中,遇到各种问题,包括自身问题以及甲方问题,导致项目滞后延期的主要问题分析,汇总三种浪费: (1) 信息传递的浪费: 这个在甲方产品设计以及生产过程中出现的问题,也是我 ...
- [ABC263G] Erasing Prime Pairs
Problem Statement There are integers with $N$ different values written on a blackboard. The $i$-th v ...
- [ABC246B] Get Closer
section> Problem Statement From the point $(0,0)$ in a two-dimensional plane, let us move the dis ...
- vue-test4 -----插槽
<template> <!-- <Main class="cccc"/> <component-a/> --> <slot-d ...
- 华企盾DSC客户端右键菜单不显示常见处理方法
1.检查控制台"客户端不显示右键菜单项" 2.未分发模块权限,若以分配可尝试去掉重新分配模块 3.检查杀毒软件是否杀掉了5097目录的文件(覆盖安装,以上两条没问题,这条比较常见) ...
- 一个WPF版的Layui前端UI库
前言 相信做.NET后端开发的很多小伙伴都用过Layui前端UI组件库,今天我们分享一个WPF版的Layui前端UI样式库:Layui-WPF. WPF介绍 WPF 是一个强大的桌面应用程序框架,用于 ...
- Confluence OGNL表达式注入命令执行漏洞(CVE-2022-26134)
Confluence OGNL表达式注入命令执行漏洞(CVE-2022-26134) 简介 Atlassian Confluence是企业广泛使用的wiki系统.2022年6月2日Atlassian官 ...
- Python——第一章:占位——pass
pass: 常用于代码占位 a = 10 if a > 100: pass 当设计代码时,有些条件或代码还没有想好要如何处理,先用pass做占位,后续可以回来继续写.如果不写pass则会报错,因 ...
- SQL优化案例(2):OR条件优化
接下来上一篇文章< SQL优化案例(1):隐式转换>的介绍,此处内容围绕OR的优化展开. 在MySQL中,同样的查询条件,如果变换OR在SQL语句中的位置,那么查询的结果也会有差异,在多个 ...