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

【JAVA】- 配置加解密之Jasypt的使用

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

【JAVA】- 配置加解密之Jasypt的使用

背景

对于配置中心的账号、密码进行加密,项目汇总用到的是 jasypt 的解决方案。

Maven 依赖

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

JAVA 参数配置

在启动的bootstrap.yml 增加 jasypt 解密的秘钥

jasypt:
  encryptor:
    password: sang #根密码
调用JAVA API 生成密文
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = AdminApplication.class)
public class AdminApplicationTest {
	@Autowired
	private StringEncryptor stringEncryptor;

	@Test
	public void testEnvironmentProperties() {
		System.out.println(stringEncryptor.encrypt("sang"));
	}

}

或者直接使用JAVA 方法调用 (不依赖 spring 容器)

  
    @Test
    public void testEnvironmentProperties() {
        System.setProperty(JASYPT_ENCRYPTOR_PASSWORD, "sang");
        StringEncryptor stringEncryptor = new DefaultLazyEncryptor(new StandardEnvironment());

        //加密方法
        System.out.println(stringEncryptor.encrypt("123456"));
        //解密方法
        System.out.println(stringEncryptor.decrypt("saRv7ZnXs"));
    }
配置文件中使用密文
spring:
  datasource:
    password: ENC(密文)
解析代码示例
public static void main(String[] args) {
   StringEncryptor stringEncryptor = new DefaultLazyEncryptor(new StandardEnvironment());
   System.setProperty("jasypt.encryptor.password", "key");
   System.out.println(stringEncryptor.decrypt("---"));

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/572305.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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