springboot项目创建笔记32 之《配置文件密码加密》

1、pom文件添加依赖

<!-- 配置文件密码加密工具 -->
<dependency>
		<groupId>com.github.ulisesbocchio</groupId>
		<artifactId>jasypt-spring-boot-starter</artifactId>
		<version>3.0.4</version>
</dependency>

2、application.yml文件添加

#加密所需的salt(盐)
jasypt:
    encryptor:
        password: AD42F6697B035B7580E4FEF93BE20BAD
        #加密算法设置3.0.0以后
        algorithm: PBEWithMD5AndDES
        iv-generator-classname: org.jasypt.iv.NoIvGenerator

3、添加测试类JasyptTest.java

package myboot;

import org.jasypt.util.text.BasicTextEncryptor;

public class JasyptTest {
	public static void main(String[] args) {
		BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
		// 加密所需的salt(盐)
		textEncryptor.setPassword("AD42F6697B035B7580E4FEF93BE20BAD");

		String str1 = "654321"; // 数据库
		String str2 = "123456"; // 邮箱

		System.out.println(textEncryptor.encrypt(str1));
		System.out.println(textEncryptor.encrypt(str2));
	}
}

执行结果:

TSNRe9Ou+hxupLlQ3me15Q==
EBfyncD8izHWW8+8dFKPmw==

每次执行加密结果不同,但是都可以解密出明文

4、将配置文件中password后的值改成ENC(xxxxxxxxxx)
例如:

#定义数据源
#参考 https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE
spring:
    datasource:
            druid:
                driver-class-name: com.mysql.cj.jdbc.Driver
                type: com.alibaba.druid.pool.DruidDataSource
                url: jdbc:mysql://localhost:3306/webapp2?serverTimezone=UTC
                username: user2
                #654321
                password: ENC(TSNRe9Ou+hxupLlQ3me15Q==)
                #配置监控统计拦截的filters
                filters: stat,wall,slf4j
                max-active: 20
                initial-size: 1
                max-wait: 60000
                min-idle: 1
                time-between-eviction-runs-millis: 60000
                min-evictable-idle-time-millis: 300000
                test-while-idle: true
                test-on-borrow: false
                test-on-return: false
                pool-prepared-statements: true
                max-open-prepared-statements: 20
                async-init: true
                filter: 
                    slf4j: 
                        enabled: true
                        statement-create-after-log-enabled: false
                        statement-close-after-log-enabled: false
                        result-set-open-after-log-enabled: false
                        result-set-close-after-log-enabled: false
#邮件发送
    mail:
      username: xxx@xxx.com
      #123456
      password: ENC(EBfyncD8izHWW8+8dFKPmw==)
      host: smtp.exmail.qq.com
      port: 465
      properties:
        mail:
          transport:
            protocol: smtp
          smtp:
            socketFactory:
              class: javax.net.ssl.SSLSocketFactory
            port: ${spring.mail.port}
            auth: true
            starttls:
              enable: true
              required: true

参考资料:
https://blog.csdn.net/qq_37143673/article/details/107563064/

注:最新代码上传至https://github.com/csj50/myboot
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值