java - 如何通过DataSourceBuilder将jasypt加密密码传递给数据库

标签 java spring-boot encryption configuration jasypt

需要将加密密码存储在.properties文件中,然后需要在配置类中解密并需要使用jasypt传递到数据库

尝试在 springboot 应用程序中使用 jasypt 加密和解密密码
引用 link-1 link-2

POM.XML中添加了依赖项

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

.properties文件中添加加密密码

mail.encrypted.property=ENC(Fy/hjJHHbIYYwijL5YwXAj8Ho2YTwzhi)

在Springboot应用程序类中

@SpringBootApplication
@EnableEncryptableProperties
@PropertySource(name="EncryptedProperties", value = "classpath:encrypted.properties")
public class MyApplication {
    ...
}

在配置类中

@Value("${mail.encrypted.property}")
private String password;

@ConfigurationProperties(prefix = "mail")
public Datasource ConfigProperties {
    return DataSourceBuilder
        .create()
        .password(password)
        .build();
}

但是由于密码错误而出现错误,没有添加加密代码应用程序工作正常

最佳答案

我设法让它像这样工作:

  1. 加密器Bean!加密部分将通过该 bean 进行解密。它与 application.properties 文件中的 jasypt.encryptor.bean 占位符相连,您可以在此处提供用于解密的魔术词(密码)

application.properties

jasypt.encryptor.bean=encryptorBean

info.jdbcUrl=jdbc:jtds:sqlserver://xxx.xxx.xxx.xxx:yyy/DEUR
info.username=ENC(1es1kdTptS7HNG5nC8UzT1pmfeYFkww)
info.password=ENC(z6selbQvJrjpxErfsERU6BDtFeweUZX)
info.driverClassName=net.sourceforge.jtds.jdbc.Driver
info.connectionTestQuery=select 1

那么,如何访问加密属性呢? “魔法”是在以下摘录中完成的

数据库配置器

package com.telcel.info;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.jasypt.encryption.pbe.PBEStringCleanablePasswordEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;

@Configuration
@EnableEncryptableProperties
@PropertySource("classpath:application.properties")
public class DatabaseConfig {

    @Autowired
    private Environment env;

    @Bean(name = "encryptorBean")
    public PBEStringCleanablePasswordEncryptor basicTextEncryptor() {
        StandardPBEStringEncryptor encryptor;
        encryptor = new StandardPBEStringEncryptor();
        encryptor.setAlgorithm("PBEWithMD5AndDES");
        encryptor.setPassword("hocus pocus");
        return encryptor;
    }

    @Bean(name = "infoDS")
    @ConfigurationProperties(prefix = "info")
    public DataSource infoDatasource() {
        String username = env.getProperty("info.username");
        String password = env.getProperty("info.password");

        return DataSourceBuilder.create()
                .username(username)
                .password(password)
                .build();
    }

}

在幕后环境也将classpath.properties注册为加密的属性源,因此当您请求一个属性时,如果它被 ENC() 包围,它会调用jasypt.encryptor.bean 尝试解码该属性。

希望这有帮助!

干杯

关于java - 如何通过DataSourceBuilder将jasypt加密密码传递给数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52152010/

相关文章:

java - 使用 gradle 构建 jar

java - 如何使用 JpaRepository 和嵌套对象列表进行搜索?

php - RSA 安卓加密/RSA PHP 解密

java - 将字段限制为类的实例并同时实现接口(interface)

java - 模拟 RCP 应用程序的 jar 文件。哪些因素可能会导致其出错?

java - 如何使用切换按钮构建字符串列表

java - Thymeleaf 无法解析多模块 Spring Boot 项目中的模板

java - 创建名为 : Injection of autowired dependencies failed, 的 bean 时出错,无法解析占位符

java - JDK RSACore.priCrypt 如何工作以及 getBlindingRandomPair 是什么意思?

java - Android AES(带有 keystore )使用相同的纯文本生成不同的密文