jasypt 加密解密+自定义加密算法

1.引用jar包

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

2.在application.yml文件中加入配置信息
#数据库配置文件加密
jasypt:
encryptor:
## 实现jasypt加密解密的类
bean: desencrypt
对数据库密码加密,使用 ENC() 包裹起来,然后里面放置密文

 

 

###################################################
# 数据库配置 #
###################################################
# 数据库驱动
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
# 数据库链接
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
# 数据库用户名
spring.datasource.username=NR
# 数据库密码
spring.datasource.password=ENC(TLI=)
# 数据源连接池中最大连接数
spring.datasource.hikari.maximum-pool-size=50

 

 


3.加密解密类
import gov.cnao.security.service.EncryptDecryptService;
import gov.cnao.security.service.impl.EncryptDecryptServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jasypt.encryption.StringEncryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component("desencrypt")
public class NrStringEncryptor implements StringEncryptor {
private static Logger logger = LoggerFactory.getLogger(NrStringEncryptor.class);

private EncryptDecryptService encryptDecryptService = new EncryptDecryptServiceImpl();

@Override
public String encrypt(String message) {
if (StringUtils.isNotBlank(message)){
try {
logger.info("加密前密码:"+message);
message = encryptDecryptService.encrypt(message);
logger.info("加密后密码:"+message);
logger.info("配置信息加密成功!");
} catch (Exception e) {
logger.error("配置信息加密失败!");
}
}
return message;
}

@Override
public String decrypt(String encryptedMessage) {
if (StringUtils.isNotBlank(encryptedMessage)){
try {
logger.info("解密前密码:"+encryptedMessage);
encryptedMessage = encryptDecryptService.decrypt(encryptedMessage);
logger.info("解密后密码:"+encryptedMessage);
logger.info("配置信息解密成功!");
} catch (Exception e) {
logger.error("配置信息解密失败!");
}
}
return encryptedMessage;
}
}

4. 自定义加密算法

posted @ 2021-09-01 18:20  JLCUI  阅读(1967)  评论(0编辑  收藏  举报