栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

jasypt对springboot加密使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jasypt对springboot加密使用

jasypt对springboot加密使用

1.版本说明及引用

因为考虑到我的项目springboot-boot-starter是2.3.3RELEASE版本,所以关于jasypt使用的是jasypt-spring-boot-starter
2.1.2版本。


    com.github.ulisesbocchio
    jasypt-spring-boot-starter
    2.1.2

springboot


    org.springframework.boot
    spring-boot-starter-parent
    2.3.3RELEASE


2:配置yml
yml中添加如下加盐处理,properties自行转化

其中这个password只是为了对你的要加密解密的字符串加盐,非真正要加密的串。

jasypt:
  encryptor:
    password: 71144850f4fb4cc55fc0ee6935badddf

3:controller测试:

import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
@RestController
public class TestController extends CommonController {
 
    @Autowired
    private StringEncryptor encryptor;
 
    
    @GetMapping("/jasypt")
    public void testJasypt() {
        String password = "123456";
        String encryptPwd = encryptor.encrypt(password);
        System.out.println("encryption: " + encryptPwd);
        System.out.println("decryption: " + encryptor.decrypt(encryptPwd));
    }
 
}

结果

Test Unit4测试:

import com.ericsson.bit.sqa.BITOauth2Application;
import org.jasypt.encryption.StringEncryptor;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = BITOauth2Application.class)
public class JasyptUtilTest {
 
    @Autowired
    private StringEncryptor encryptor;
 
    @Test
    public void jasypt(){
        String name = encryptor.encrypt("hello");
        System.out.println("en: " + name);
        System.out.println("de: " + encryptor.decrypt(name));
    }
 
}

结果

4:yml配置解密

对要加密的敏感信息进行jasypt加密处理后,需要在配置文件用ENC(加密串)进行配置文件加密。

这样即可正常启动程序而达到配置文件加密的目的。

5:注意:如果没在配置中配置加盐会出现如下错误

[2021-01-22 13:55:33][ERROR][ANONYMOUS] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: either 'jasypt.encryptor.password' or one of ['jasypt.encryptor.private-key-string', 'jasypt.encryptor.private-key-location'] must be provided for Password-based or Asymmetric encryption] with root cause
java.lang.IllegalStateException: either 'jasypt.encryptor.password' or one of ['jasypt.encryptor.private-key-string', 'jasypt.encryptor.private-key-location'] must be provided for Password-based or Asymmetric encryption
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/285468.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号