暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

jasypt对Spring boot的数据源密码进行加密

NIU技术那点事 2019-04-04
973

针对@SpringBootApplication启动

---------------------------------------------------------------------

第一步:

pom.xml,加入jaspyt库的依赖

<dependency>

<groupId>com.github.ulisesbocchio</groupId>

<artifactId>jasypt-spring-boot-starter</artifactId>

     <version>2.0.0</version>

</dependency>

-----------------------------------------------------

第二步:

application.properties,设置加密的密码

#配置密码,eg: EbfYkitulv73I2p0mXI50JMXoaxZTKJ7

jasypt.encryptor.password=EbfYkitulv73I2p0mXI50JMXoaxZTKJ7

-----------------------------------------------------

第三步:

jaspyt得到加密的后的密文,采用二种方式:

每执行一次,所得到后的密文不一样!

1> 用单元测试用例得到:

import org.jasypt.encryption.StringEncryptor;

import org.junit.Test;

import org.junit.runner.RunWith;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.boot.test.context.SpringBootTest;

importorg.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)

@SpringBootTest

public class TestFir {

    @Autowired

    privateStringEncryptor stringEncryptor;

    @Test

    public voidencryptPwd() {

        Stringroot = stringEncryptor.encrypt("root");

       System.out.println("-----root="+root);

        Stringpass = stringEncryptor.encrypt("123456");

       System.out.println("-----pass="+pass);

        Stringtyklq = stringEncryptor.encrypt("tyklq");

       System.out.println("-----tyklq="+tyklq);

    }

}

2> 用命令行得到:

cd D:\app\apache-maven-3.0.3\lib\org\jasypt\jasypt\1.9.2

D:\app\apache-maven-3.0.3\lib\org\jasypt\jasypt\1.9.2>java -cpjasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLIinput="123456" password=EbfYkitulv73I2p0mXI50JMXoaxZTKJ7algorithm=PBEWithMD5AndDES



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


Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM25.60-b23




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


algorithm: PBEWithMD5AndDES

input: tyklq

password: EbfYkitulv73I2p0mXI50JMXoaxZTKJ7




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


TCfMK1zS4pueU24cHV3T8w==



说明:

java -cpjasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="需要加密的数据"password=加密的密码 algorithm=PBEWithMD5AndDES

其中:input为需要加密的信息;password为加密时采用的秘钥;algorithm为加密算法,默认算法为PBEWithMD5AndDES

-----------------------------------------------------

第四步:拿到密文,配置到application.properties中去

#明文root

spring.datasource.username = ENC(B23z/+2ZD8oE2WHf5Lqdzg==)

#明文123456

spring.datasource.password =ENC(44UlLZseJloBEMiKiSelIw==)

#明文tyklq

chat.info=ENC(TCfMK1zS4pueU24cHV3T8w==)

-----------------------------------------------------

第五步:可以得到tyklp

@RestController

@RequestMapping("/test")

publicclass TestController extends BaseController {

    private static final Log logger =LogFactory.getLog(TestController.class);

    @Value("${chat.info}")

    private String info;

    /*

     * 测试用 jasypt加密后,然后取值不是加密的数据

     * 日志输出:---info=tyklq

     */

    @RequestMapping("/getInfo")

    public String getUeseInfo() {

        logger.info("---info="+info);

        return info;

    }

}

 

完毕!



文章转载自NIU技术那点事,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论