spring boot 配置数据库加密

本文介绍如何使用 Jasypt 在 Spring Boot 中加密数据库连接密码。通过 pom.xml 引入 jasypt-spring-boot-starter 依赖,并在 Windows 下生成加密密文。最后,在配置文件中设置加密后的密码。

1.pom.xml 文件中引

		<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
		<!-- 整合jasypt对数据库密码加密 -->
		<dependency>
			<groupId>com.github.ulisesbocchio</groupId>
			<artifactId>jasypt-spring-boot-starter</artifactId>
			<version>1.16</version>
		</dependency>

2.在windows下命令生成加密密文

java -cp G:\maven\repository\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="you4new!" password=saltnewfor algorithm=PBEWithMD5AndDES

参数说明:

input =数据库链接密码

password=加密字段,随意设置

algorithm= 算法

执行后有以下输出


----ENVIRONMENT-----------------

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.131-b11



----ARGUMENTS-------------------

algorithm: PBEWithMD5AndDES
input: you4new!
password: saltnewfor



----OUTPUT----------------------

UK61bS+W/BskHl0N0ViAQcrPAmZLZZwO

3.在spring boot 项目的配置文件中加入以下内容

#spring.datasource.url=jdbc:mysql://localhost:3306/Solrsearch
#spring.datasource.username=root
#jasypt.encryptor.password=${password}
#jasypt.encryptor.password=aidan123456
#spring.datasource.password=ENC(UK61bS+W/BskHl0N0ViAQcrPAmZLZZwO)
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver

https://www.ricston.com/blog/encrypting-properties-in-spring-boot-with-jasypt-spring-boot/

https://www.ricston.com/blog/encrypting-properties-in-spring-boot-with-jasypt-spring-boot/

http://www.cnblogs.com/zz0412/p/jasypt-001.html

https://github.com/ulisesbocchio/jasypt-spring-boot





                
### 如何在 Spring Boot配置数据库连接和设置 #### 配置 MySQL 数据库连接数 为了优化应用程序性能,合理配置数据库连接池参数至关重要。可以通过编辑 `application.properties` 或者 `application.yml` 文件来设定最大活跃连接数量和其他相关属性[^1]。 对于基于 Maven 的项目来说,在 pom.xml 添加 Druid 依赖项可以增强对数据库连接管理的支持: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency> ``` 接着定义具体的连接池参数如下所示(以 properties 格式为例): ```properties spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=admin # 设置初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置获取连接等待超时的时间 spring.datasource.maxWait=60000 ... ``` 上述配置指定了使用 Druid 作为默认的数据源实现,并设置了初始连接数目、最小空闲连接数以及最大活动连接数等重要选项[^4]。 #### 使用 Actuator 模块监控连接池状态 为了让开发者能够更好地了解当前系统的运行状况,建议启用 Spring Boot 提供的 Actuator 组件。这允许外部系统通过 HTTP API 获取有关应用内部组件的状态报告,比如查看 HikariCP 或其他类型的连接池指标信息。 只需简单引入 spring-boot-starter-actuator 即可激活此功能: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` 之后可以根据官方文档指导开放特定端点用于展示所需数据。 #### 加密存储敏感凭证 考虑到安全性因素,在生产环境中应当考虑采用适当的方法保护诸如用户名/密码之类的机密资料免受未授权访问威胁。一种常见做法就是利用 Jasypt 库来进行字符串级别的加密处理后再存入配置文件内[^3]。 安装 jasypt 春季引导程序插件后,按照指示修改 application.properties 来指定编码后的值即可完成基本的安全防护措施。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值