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

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

作者: 不知不怪 | 来源:发表于2020-02-12 00:16 被阅读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,每次拿到的密文都不一样,本人算法是外行,只想会用就行!

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/gizffhtx.html