spring boot 中使用jasypt 对配置文件里信息进行加密 - Go语言中文社区

spring boot 中使用jasypt 对配置文件里信息进行加密


博客参考:
springboot集成jasypt实现配置文件敏感信息加密,详细!

Spring Boot使用jasypt处理加密问题

 

​​​​​1、pom.xml 引入jar包,我这里使用的spring boot  1.5.10.RELEASE

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

2、application.properties 添加配置信息

     重点配置:jasypt.encryptor.password的值  用来加解密使用的

3、两种方式给明文进行加密

  1. jasypt提供了封装类StringEncryptor,可以通过代码来加解密  但是上面的说的jasypt.encryptor.password的值要一样
    import org.jasypt.encryption.StringEncryptor;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import static sun.plugin.javascript.navig.JSType.Embed;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest
    public class JasyptTest {
    
        @Autowired
        StringEncryptor encryptor;
        //加密
        @Test
        public void getPass(){
            String name = encryptor.encrypt("hello");
            String password = encryptor.encrypt("world");
            System.out.println(name); //BEJsOY+ny6/Bo+B8Rv2f9Q==
            System.out.println(password); //+w8Jn3Iv1R7nsFa6fO24Dw==
        }
        //解密
        @Test
        public void passDecrypt(){
            String username = encryptor.decrypt("BEJsOY+ny6/Bo+B8Rv2f9Q==");
            String password = encryptor.decrypt("+w8Jn3Iv1R7nsFa6fO24Dw==");
            System.out.println(username+"--"+password);
        }
    
    }

    2、通过命令行运行 jasypt-1.9.2.jar 包命令来加密解密;

            java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI

            input=需要被加密的明文内容

           password=密钥(盐值) 

            algorithm=加密方式 


       例如,我们要对明文用户名“hello”进行加密,密码为helloworld,加密方式为PBEWithMD5AndDES

  • 找到本地maven仓库  ,这个路径 ..orgjasyptjasypt1.9.2 下  打开命令行 运行如下加密命令
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="hello" password=helloworld algorithm=PBEWithMD5AndDES

运行结果如下: 

D:usermavenrespositoryorgjasyptjasypt1.9.2>java -cp jasypt-1.9.2.jar
 org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="hello" 
password=helloworld algorithm=PBEWithMD5AndDES

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

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.172-b11



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

algorithm: PBEWithMD5AndDES
input: hello
password: helloworld



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

wizvEdztMylxBFptawIo/w==
  • 使用刚才加密出来的结果进行解密,执行如下解密命
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="wizvEdztMylxBFptawIo/w==" password=helloworld algorithm=PBEWithMD5AndDES

运行结果如下: 

D:usermavenrespositoryorgjasyptjasypt1.9.2>java -cp jasypt-1.
9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="wizvEdztMyl
xBFptawIo/w==" password=helloworld algorithm=PBEWithMD5AndDES

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

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.172-b11



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

algorithm: PBEWithMD5AndDES
input: wizvEdztMylxBFptawIo/w==
password: helloworld



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

hello

4、将配置文件中原来的明文账号密码改为加密后的密文 

注意:因为默认情况下jasypt-spring-boot解析密码的规则是前缀是ENC
配置文件中jasypt.encryptor.password的值必须和命令行中的password属性值保持一致,否则springboot在启动时候自动解析密文的时候会报错,无法解析密文

5、测试

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class JasyptController {

     @Value("${cmd.username}")
    private String cmdUsername;

     @Value("${cmd.password}")
    private String cmdPassword;

    @Value("${code.username}")
    private String codeUsername;

    @Value("${code.password}")
    private String codePassord;

    @RequestMapping("/jasypt")
    public String testJasypt() {
        return "我是命令行加密的:" +cmdUsername+"-->"+cmdPassword +"<br>"+"我是代码生成的:"+codeUsername+"-->"+ codePassord;
    }
}

运行结果如下:

 

备注: 使用代码和命令行 密钥(盐值)也是一样的对同一字符串进行加密,为什么生成的密文不一样的;猜测可能是加密方式不一样,后续 查看源码看看代码加密使用什么加密方式

密钥放在启动脚本里 java -jar test.jar jasypt.encryptor.password=text & 或者放在环境变量里 

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/dreaming_open/article/details/82728744
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 22:29:22
  • 阅读 ( 751 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢