系列导航

springBoot项目打jar包

1、springboot工程新建(单模块)

2、springboot创建多模块工程

3、springboot连接数据库

4、SpringBoot连接数据库引入druid

5、SpringBoot连接数据库引入mybatis

6、SpringBoot-mybatis分页实现pagehelper

7、SpringBoot-mybatis-plus引入

8、SpringBoot 事务

9、SpringBoot-mybatis-druid多源数据多源数据

10、SpringBoot-mybatis-plus-druid多源数据

11、SpringBoot-mybatis-plus-druid多源数据事务

12、SpringBoot-mybatis-plus-ehcache

13、SpringBoot-配置文件里密码加密

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-配置文件里密码加密的更多相关文章

  1. springboot对数据库密码加密

    第一步:maven引jar包 <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifa ...

  2. SpringBoot项目配置文件中密码的加密

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/15565862.html 版权声明:本文为博主原创文章,转载请附上博文链接! 公众号:追梦1819 ...

  3. Spring Boot 配置文件密码加密两种方案

    Spring Boot 配置文件密码加密两种方案 jasypt 加解密 jasypt 是一个简单易用的加解密Java库,可以快速集成到 Spring 项目中.可以快速集成到 Spring Boot 项 ...

  4. 配置 Druid 数据源及密码加密-SpringBoot 2.7 实战基础

    在SpringBoot中配置 Druid 数据源及密码加密的方法 前文集成 MyBatis Plus,实现了一组增删改查接口.在启动服务时,从控制台中可以看出 Spring Boot 默认使用 Hik ...

  5. 解决spring-boot配置文件使用加密方式保存敏感数据启动报错No decryption for FailsafeTextEncryptor. Did you configure the keystore correctly

    spring-boot配置文件使用加密方式保存敏感数据 application.yml spring: datasource: username: dbuser password: '{cipher} ...

  6. Jasypt加密SpringBoot配置文件

    如果 SpringBoot 的 properties 文件中含有用户名密码等敏感信息,为了安全起见需要对明文密码加密.Jasypt 是用来加密的 jar 包. 1.引入 Jasypt 在 pom.xm ...

  7. 【SpringBoot】SpringBoot集成jasypt数据库密码加密

    一.为什么要使用jasypt库? 目前springboot单体应用项目中,甚至没有使用外部配置中心的多服务的微服务架构的项目,开发/测试/生产环境中的密码往往是明文配置在yml或properties文 ...

  8. Springboot 配置文件、隐私数据脱敏的最佳实践(原理+源码)

    大家好!我是小富- 这几天公司在排查内部数据账号泄漏,原因是发现某些实习生小可爱居然连带着账号.密码将源码私传到GitHub上,导致核心数据外漏,孩子还是没挨过社会毒打,这种事的后果可大可小. 说起这 ...

  9. tomcat安全配置之证书密码加密存储

    最近项目组要完成一个新Web Servicer接口的开发,其中有项要求是支持外部客户程序以https方式访问这些SOAP接口.项目组当前基于tomcat6.0.29开发,axis版本为1.4.拿到这个 ...

  10. mysql-8.0 安装教程(自定义配置文件,密码方式已修改)

    下载zip安装包: MySQL8.0 For Windows zip包下载地址:https://dev.mysql.com/downloads/file/?id=476233,进入页面后可以不登录.后 ...

随机推荐

  1. [P7880][Ynoi2006] rldcot

    [Ynoi2006] rldcot 题目描述 给定一棵 \(n\) 个节点的树,树根为 \(1\),每个点有一个编号,每条边有一个边权. 定义 \(dep(x)\) 表示一个点到根简单路径上边权的和, ...

  2. linux环境下脚本部署项目出现nohup: redirecting stderr to stdout问题

    解决办法: 把后面的 "&" 改成 "2>&1 &" 最终改为: nohup java -jar ${JAR_NAME} --lo ...

  3. Mybatis-Flex核心功能之@Table

    1.能干啥? @Table 主要是用于给 Entity 实体类添加标识,用于描述 实体类 和 数据库表 的关系,以及对实体类进行的一些 功能辅助. 例如: 数据库有一张tb_member的会员表 这时 ...

  4. LeetCode1806:还原排列的最少操作步数(置换群 or 模拟)

    题意:题目的意思是,给定一个初始状态perm,然后对perm的每个元素按照上述的规则进行变换操作.问:perm经过多少次这种操作能够变回初始的perm. 解题思路:第一种方法就是模拟,一直变换,直到变 ...

  5. 使用代码生成工具快速开发应用-结合后端Web API提供接口和前端页面快速生成,实现通用的业务编码规则管理

    在前面随笔<在Winform应用中增加通用的业务编码规则生成>,我介绍了基于Winform和WPF的一个通用的业务编码规则的管理功能,本篇随笔介绍基于后端Web API接口,实现快速的Vu ...

  6. Harbor安装和镜像推送

    安装前提: yum -y install docker-compose-plugin 安装: tar -zxvf harbor-offline-installer-v2.6.1.tgz cd harb ...

  7. Oracle数据字典(各种视图、表)

    数据字典是存放整个数据库实例重要信息的一组表,这些数据字典大部分都是SYS用户所有. 数据字典的构成 Oracle数据字典名称由前缀和后缀组成,使用下画线"_"连接.其代表的含义如 ...

  8. hszxoj 矿场搭建 [tarjan]

    hszxoj 矿场搭建 题目描述 原题来自:HNOI 2012 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决 ...

  9. python tkinter 使用(三)

    python tkinter 使用(三) 本篇文章主要讲下tkinter下的filedialog的使用. 1: askopenfilename 首先使用tkinter中fiedialog来实现一个简单 ...

  10. JavaFx之播放MP4(二十七)

    JavaFx之播放MP4(二十七) JavaFX 视频和音频支持,由 JavaFX 媒体类 Media.MediaPlayer.MediaView 和 AudioClip 提供. import jav ...