### 2.添加测试类进行加密
先在application.yml文件中设置密码,前后缀,密码加密方式
#加密
jasypt:
encryptor:
# 盐值
password: 123
# 指定加密方式 可以去官网查看支持的类型
algorithm: PBEWithMD5AndDES
property:
# 标识为加密属性的前缀
prefix: ENC(
# 标识为加密属性的后缀
suffix: )
@SpringBootTest(classes = RuoYiApplication.class) //指向自己的启动类
@ActiveProfiles(“druid-dev”) //没有配置文件可以不填
@RunWith(SpringRunner.class)
public class EncryptorTest {
//引入加密工具
@Resource
private StringEncryptor stringEncryptor;
@Test
public void test() {
//进行加密
String encrypt = stringEncryptor.encrypt("root");
System.out.println(encrypt);
//你的数据库的密码
String password = stringEncryptor.encrypt("123321");
System.out.println(password);
}
}
### 3.将加密结果替换到application.yml文件
替换数据源
pring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
# 主库数据源
master:
url: jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&allowMultiQueries=true
username: ENC(你的秘钥)
password: ENC(你的秘钥)
## 将秘钥配置到外部文件中
### 1.增加配置类
在项目启动的config包下增加配置类来读取配置
public class LocalSettingsEnvironmentPostProcessor implements EnvironmentPostProcessor{
/**
* 设置路径数组,保证线上和本地都能读取到该文件
*/
private static final String LOCATIONS [] = {“/home/ruoyi/jasypt.properties”,“D:\ruoyi\jasypt.properties”};
@Override
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
for(String fileLocation : LOCATIONS){
File file = new File(fileLocation);
if (file.exists()) {
MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
Properties properties = loadProperties(file);
propertySources.addFirst(new PropertiesPropertySource("Config", properties));
return ;
}
}
}
private Properties loadProperties(File f) {
FileSystemResource resource = new FileSystemResource(f);
try {
return PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Failed to load local settings from " + f.getAbsolutePath(), ex);
}
}
}
### 2.添加读取的配置文件
在resources下的META-INF文件夹中新增spring.factories文件,添加以下内容
org.springframework.boot.env.EnvironmentPostProcessor=你的实际路径.config.LocalSettingsEnvironmentPostProcessor
### 3.在服务器和本地添加指定的文件jasypt.properties
>
> jasypt.encryptor.password=盐值
>
>
>
## JDK1.8之前版本注意避坑
## 最后
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
**深知大多数网络安全工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
**因此收集整理了一份《2024年网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**
![img](https://img-blog.csdnimg.cn/img_convert/df1ae2b763f231298163b082471769fe.png)
![img](https://img-blog.csdnimg.cn/img_convert/ce7b41f67e4a658348b2db62dd82dd66.png)
![img](https://img-blog.csdnimg.cn/img_convert/5dd1db7becac7b9a9cefc1bb24425e4f.png)
![img](https://img-blog.csdnimg.cn/img_convert/b2632b437df10d399744fd9f1fc37651.png)
![img](https://img-blog.csdnimg.cn/img_convert/347a4bdac1719f20bd0599ff9a7c377e.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上网络安全知识点!真正的体系化!**
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618653875)
**由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**
*
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618653875)
**由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**