zoukankan      html  css  js  c++  java
  • SpringBoot jasypt 对配置文件项加密

    1、pom.xml引入jar依赖

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

    2、application.xml配置加/解密的密码

    jasypt.encryptor.password=EbfYkitulv73I2p0mXI50JMXoaxZTKJ7

    3、使用StringEncryptor生成加密项的密文

    import com.xfz.App;
    import org.jasypt.encryption.StringEncryptor;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class EncryptorTest {
        @Autowired
        StringEncryptor encryptor;
    
        @Value("${spring.datasource.url}")
        private String url;
        @Value("${spring.datasource.username}")
        private String username;
        @Value("${spring.datasource.password}")
        private String password;
    
        @Test
        public void getPass() {
        System.out.println("------------------------------------加密------------------------------------");
        String escUrl = encryptor.encrypt(url);
        String escUsername = encryptor.encrypt(username);
        String escPassword = encryptor.encrypt(password);
        System.out.println(escUrl);
        System.out.println(escUsername);
        System.out.println(escPassword);
        System.out.println("-------------------------------------解密-----------------------------------");
        System.out.println(encryptor.decrypt(escUrl));
        System.out.println(encryptor.decrypt(escUsername));
        System.out.println(encryptor.decrypt(escPassword));

        Assert.assertTrue(escUrl.length() > 0);
        Assert.assertTrue(escUsername.length() > 0);
        Assert.assertTrue(escPassword.length() > 0);
     } }

    4、替换application.xml加密项为密文

    spring.datasource.url=ENC(密文)
    spring.datasource.username=ENC(密文)
    spring.datasource.password=ENC(密文)
  • 相关阅读:
    利用 Memory Dump Diagnostic for Java (MDD4J) 分析内存管理问题
    Google开源软负载seesaw
    Commit can not be set while enrolled in a transaction
    del_cursor 批量删除游标
    java api 批量数据库操作
    eclipse invalid zip archive lib
    less,more,view一个文件时中文可以正常显示,可是VI却显示乱码呢?
    10-13 Zuul面试点之Cookie和特殊头信息处理
    10-12 Zuul面试点之Hystrix降级处理
    10-11 Zuul面试点之Hystrix整合
  • 原文地址:https://www.cnblogs.com/df-xfz/p/9856397.html
Copyright © 2011-2022 走看看