jasypt加密数据库连接

博客围绕Spring展开,包含依赖、加密解密工具类、判断逻辑、判断解密等内容,还给出了配置类和配置文件的数据源示例,聚焦于Spring在项目中的应用及相关功能实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

依赖

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

加密解密工具类

public class EncryptAndDecryptUtil {

	private static Log logger = LogFactory.getLog(EncryptAndDecryptUtil.class);

	static String algorithm = PropertiesBean.getInstance().getProperty("system.encryptor.algorithm");	
	static String salt = PropertiesBean.getInstance().getProperty("system.encryptor.salt");
	public static String StandardPBEStringEncryptor(String str){
		StandardPBEStringEncryptor standardPBEStringEncryptor =new StandardPBEStringEncryptor();
		standardPBEStringEncryptor.setAlgorithm(algorithm);
		standardPBEStringEncryptor.setPassword(salt);
		return standardPBEStringEncryptor.encrypt(str);

	}

	public static String StandardPBEStringDecryptor(String str){
		StandardPBEStringEncryptor standardPBEStringEncryptor =new StandardPBEStringEncryptor();
		standardPBEStringEncryptor.setAlgorithm(algorithm);
		standardPBEStringEncryptor.setPassword(salt);
		return standardPBEStringEncryptor.decrypt(str);		
	}
	public static void main(String[] args) {
	}
}

判断逻辑

public class PropertyDetector implements EncryptablePropertyDetector {

    public static final String ENCODED_PASSWORD_HINT = "{mediway}";

    // 如果属性的字符开头为"{dhcc}",返回true,表明该属性是加密过的
    @Override
    public boolean isEncrypted(String s) {
        if (null != s) {
            return s.startsWith(ENCODED_PASSWORD_HINT);
        }
        return false;
    }
    // 该方法告诉工具,如何将自定义前缀去除
    @Override
    public String unwrapEncryptedValue(String s) {
        return s.substring(ENCODED_PASSWORD_HINT.length());
    }
}

判断解密

public class PropertyResolver implements EncryptablePropertyResolver {
	private static Log logger = LogFactory.getLog(CatalogBlh.class);
    //自定义解密方法
    @Override
    public String resolvePropertyValue(String s) {
        if (null != s && s.startsWith(PropertyDetector.ENCODED_PASSWORD_HINT)) {
        	
            return EncryptAndDecryptUtil.StandardPBEStringDecryptor(s.substring(
            		PropertyDetector.ENCODED_PASSWORD_HINT.length()));
        }
        return s;
    }
}

配置类

@Configuration
@EnableEncryptableProperties
public class SysConfig {

	
    @Bean(name = "encryptablePropertyDetector")
    public EncryptablePropertyDetector encryptablePropertyDetector() {
        return new PropertyDetector();
    }

    @Bean(name = "encryptablePropertyResolver")
    public EncryptablePropertyResolver encryptablePropertyResolver() {
        return new PropertyResolver();
    }

}

配置文件的数据源示例

spring.datasource.url=jdbc:mysql://XXX:3306/cdbprcs?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username={mediway}AEEjrnViFiij6cUISoELFg==
spring.datasource.password={mediway}lsYrk8pixxe243W9qAcAOqCtp77zqmhM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值