当前位置: 首页> 汽车> 车展 > 江门模板建站定制_企业宣传片拍摄公司_seo搜索引擎优化关键词_seo网站推广企业

江门模板建站定制_企业宣传片拍摄公司_seo搜索引擎优化关键词_seo网站推广企业

时间:2025/8/10 7:10:37来源:https://blog.csdn.net/m0_52078336/article/details/142417182 浏览次数: 0次
江门模板建站定制_企业宣传片拍摄公司_seo搜索引擎优化关键词_seo网站推广企业

1 在项目中加密

1.1 导包

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

1.2 加密

配置文件

jasypt:encryptor:password: study-demoalgorithm: PBEWithMD5AndDESiv-generator-classname: org.jasypt.iv.NoIvGenerator

获取配置文件信息

package com.sky.properties;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** 应用配置信息** @author wangs* @date 2024/9/21 14:18*/
@Data
@Component
@ConfigurationProperties(value = "jasypt.encryptor")
public class AppProperties {/*** 加密盐值*/private String password;
}

加密代码

package com.sky;import com.sky.properties.AppProperties;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;@SpringBootTest(classes = SkyApplication.class)
@RunWith(SpringRunner.class)
public class EncryptorTest {/*** MySQL连接信息*/private final static String HOST_NAME = "主机IP";private final static String USERNAME = "数据库用户名";private final static String PASSWORD = "数据库密码";@Resourceprivate AppProperties appProperties;/*** 加密MySQL数据库*/@Testpublic void mysqlEncryptorTest() {StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();// 加密盐值encryptor.setPassword(appProperties.getPassword());String host = encryptor.encrypt(HOST_NAME);String username = encryptor.encrypt(USERNAME);String password = encryptor.encrypt(PASSWORD);System.out.println("host密文:"+host);System.out.println("username密文:"+ username);System.out.println("password密文:"+password);}
}

1.3 修改配置文件

将 1.2 中生成的密文替换配置文件中的明文信息

  datasource:driver-class-name: com.mysql.cj.jdbc.Driverhost: ENC(5+NRySB3LIsgOI2IEFrLaAt96lB0kvyK)port: 3306database: xxx_xxx_xxxusername: ENC(27L2N1KZZnJWGfYjIXRLcA==)password: ENC(f9Dc9EY6TQqmPMqARsO5/g==)

1.4 问题解决

在使用过程中,遇到了一个问题

failed to bind properties under spring.datasource.druid.password' to java.lang.String

出现这个问题的时候,在配置文件中,我并没有加上最后面的两行,加上后解决问题

jasypt:encryptor:password: study-demoalgorithm: PBEWithMD5AndDESiv-generator-classname: org.jasypt.iv.NoIvGenerator

2 参考博客

数据库加密:数据库连接加密(SpringBoot+jasypt加密)

问题报错:springboot - 解决jasypt failed to bind properties - 30岁程序员的挣扎之路 - SegmentFault 思否

关键字:江门模板建站定制_企业宣传片拍摄公司_seo搜索引擎优化关键词_seo网站推广企业

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: