如何通过DataSourceBuilder将Jasypt加密密码传递给数据库

时间:2018-09-03 14:47:29

标签: 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 个答案:

答案 0 :(得分:0)

我设法使它像这样工作:

  1. encryptorBean!加密的部分将通过此bean解密。它与 jasypt.encryptor.bean 占位符连接在 application.properties 文件中,在这里您可以提供魔术词(密码)以使解密工作

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 尝试对属性进行解码。

希望这会有所帮助!

欢呼