简体   繁体   English

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

[英]How to pass jasypt encrypted password to database through DataSourceBuilder

Need to store encrypted password in .properties file and then need to decrypt in configuration class and need to pass to database using jasypt 需要将加密的密码存储在.properties文件中,然后需要在配置类中解密,并且需要使用jasypt传递给数据库

Trying to Encrypt and decrypt the password using jasypt in springboot application 在springboot应用程序中尝试使用jasypt加密和解密密码
Reference link-1 link-2 参考 链接1 链接2

Added dependency in POM.XML POM.XML添加了依赖性

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

Added encrypted password in .properties file .properties文件中添加了加密密码

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

In Springboot Application class 在Springboot Application类中

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

In configuration class 在配置类中

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

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

But getting the error due to wrong password, without adding encryption code application is working fine 但是由于密码错误而导致错误,而无需添加加密代码应用程序也可以正常工作

I managed to make it work like so: 我设法使它像这样工作:

  1. the encryptorBean! cryptoorBean! The encrypted parts are going to be decrypted through this bean. 加密的部分将通过此bean解密。 It is wired in the application.properties file with the jasypt.encryptor.bean placeholder, this is where you provide your magic word (password) for the decryption to work 它与jasypt.encryptor.bean占位符连接在application.properties文件中,在这里您可以提供魔术字(密码)以使解密工作

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

Then, how do I access the encrypted properties? 然后,如何访问加密的属性? The "magic" is done throughout the following excerpt 在以下摘录中完成了“魔术”

The Database Configurator 数据库配置器

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();
    }

}

Behind the scenes Environment has registered the classpath.properties also as an encrypted property source, hence when you ask for a property if its surrounded with ENC() it calls the jasypt.encryptor.bean to try to decode the property. 在后台, Environment已将classpath.properties也注册为加密的属性源,因此,当您请求某个属性是否被ENC()包围时,它会调用jasypt.encryptor.bean尝试对该属性进行解码。

Hope this helps! 希望这可以帮助!

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM