优雅的给springboot 2.x配置文件中的密码加密

2020-02-12  本文已影响0人  不知不怪

1. pom.xml

<dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>3.0.2</version>
</dependency>

2.application.yml

a.b.c.d.password: ENC(OT0qGOpXrr1Iog1W+fjOiIDCJdBjHyhy)
jasypt:
   encryptor:
      password: spring-boot-demo
      algorithm: PBEWithMD5AndDES
      iv-generator-classname: org.jasypt.iv.NoIvGenerator

ENC(OT0qGOpXrr1Iog1W+fjOiIDCJdBjHyhy)为加密后的密文
jasypt.encryptor. password: spring-boot-demo 为盐

3.获取密文

package com.gzz;

import javax.annotation.PostConstruct;

import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SpringBootDemoEmailApplication {
    @Autowired
    private StringEncryptor encryptor;
    public static void main(String[] args) {
        
        SpringApplication.run(SpringBootDemoEmailApplication.class, args);
    }
    @PostConstruct
    public void run() {
        System.out.println(encryptor.encrypt("Just4Test!"));//加密
        System.out.println(encryptor.decrypt("UBdIPpXiK45yqMV8sJMx506MgpO8yUqy"));//解密
    }
}

4.说明

jasypt-3.0.2支持springboot 2.x
jasypt-2.1.1支持springboot 1.x
这个算法十分牛B,每次拿到的密文都不一样,本人算法是外行,只想会用就行!

上一篇下一篇

猜你喜欢

热点阅读