当前位置:网站首页>配置文件加密(Jasypt的简单使用)
配置文件加密(Jasypt的简单使用)
2022-06-28 14:08:00 【华为云】
title: 配置文件加密(Jasypt的简单使用)
categories: Jasypt
tags: Jasypt
需求引入
当我们需要对SpringBoot项目的配置文件中的信息进行加密时,如数据库密码等,此时我们可以使用Jasypt来实现。
解决方案
第一步,引入Jasypt依赖
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.0.0</version></dependency>第二步,编写工具类
JasyptUtils:
package com.keafmd.springdemo.utils;import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;/** * Keafmd * * @ClassName: JasyptUtils * @Description: * @author: 牛哄哄的柯南 * @date: 2022-04-25 11:18 */public class JasyptUtils { /** * Jasypt生成加密结果 * * @param password 配置文件中设定的加密密码 jasypt.encryptor.password * @param value 待加密值 * @return */ public static String encryptPwd(String password, String value) { PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor(); encryptOr.setConfig(cryptOr(password)); String result = encryptOr.encrypt(value); return result; } /** * 解密 * * @param password 配置文件中设定的加密密码 jasypt.encryptor.password * @param value 待解密密文 * @return */ public static String decyptPwd(String password, String value) { PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor(); encryptOr.setConfig(cryptOr(password)); String result = encryptOr.decrypt(value); return result; } /** * @param password salt * @return */ public static SimpleStringPBEConfig cryptOr(String password) { SimpleStringPBEConfig config = new SimpleStringPBEConfig(); config.setPassword(password); config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM); config.setKeyObtentionIterations("1000"); config.setPoolSize("1"); config.setProviderName(null); config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); config.setStringOutputType("base64"); return config; } public static void main(String[] args) { // 加密 // 盐值替换成自己熟悉的口令,此口令为解密密钥,需要妥善保管。 // 盐值也需要在第三步写入配置文件 System.out.println(encryptPwd("keafmd", "123456")); }}第三步,修改配置文件
- 把盐值写入配置文件
- 把明文密码替换为工具类生成的密文,并用ENC()包着
eg:
# 连接数据源spring: datasource: username: root password: ENC(aXcmM77CmYgj67mBxQgI2A==) url: jdbc:mysql://XXXX:XX/tt?useUnicode=true&characterEncoding=UTF-8&useSSL=false jasypt: encryptor: password: keafmd以上就是配置文件加密(Jasypt的简单使用)的全部内容
版权声明:
原创博主:牛哄哄的柯南
个人博客链接:https://www.keafmd.top/
看完如果对你有帮助,感谢点击下面的==一键三连==支持!
[哈哈][抱拳]

加油!
共同努力!
Keafmd
都看到这里了,下面的内容你懂得,让我们共同进步!
边栏推荐
- Adding virtual environments to the Jupiter notebook
- Simple understanding of ThreadLocal
- Multi dimensional monitoring: the data base of intelligent monitoring
- Open source invites you to participate in openinfra days China 2022. Topic collection is in progress ~
- 黑苹果安装教程OC引导「建议收藏」
- Deveco studio 3.0 editor configuration tips
- Thread life cycle and its methods
- Build a learning environment
- 开源社邀您参加OpenInfra Days China 2022,议题征集进行中~
- Reverse a stack with recursive functions and stack operations only
猜你喜欢

基于asp.net的文献检索系统

PC博物馆-熟悉又陌生的懵懂年代

China Radio and television 5g package is coming, lower than the three major operators, but not as low as expected

药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计

CVPR disputes again: IBM's Chinese draft papers were accused of copying idea, who won the second place in the competition

RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)

Research and Simulation of chaotic digital image encryption technology based on MATLAB

Kubernetes 深入理解kubernetes(一)

Four methods of thread termination

Action interprets value. The chairman of chenglian Youpin Han attended the Guangdong Yingde flood fighting donation public welfare event
随机推荐
go数组与切片,[]byte转string[通俗易懂]
Who is the main body of the waiting insurance record? Record in the local network security, right?
由两个栈组成的队列
@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
Connected to rainwater series problems
Inftnews | technology giants accelerate their march into Web3 and metauniverse
ArrayList源码解析
开源社邀您参加OpenInfra Days China 2022,议题征集进行中~
再谈exception——异常抛出时会发生什么?
Prediction of red wine quality by decision tree
Multi dimensional monitoring: the data base of intelligent monitoring
A bug liver a week I can't help mentioning issue
Regular matching numbers, English and English symbols
First knowledge of exception
PC博物馆-熟悉又陌生的懵懂年代
If a programmer goes to prison, will he be assigned to write code?
What is generics and how to use generics analysis
Reverse a stack with recursive functions and stack operations only
Research and Simulation of chaotic digital image encryption technology based on MATLAB
CVPR disputes again: IBM's Chinese draft papers were accused of copying idea, who won the second place in the competition