需要将加密的密码存储在.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 Application类中

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


但是由于密码错误而导致错误,而无需添加加密代码应用程序也可以正常工作

最佳答案

我设法使它像这样工作:


cryptoorBean!加密的部分将通过此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();
    }

}


在后台,Environment已将classpath.properties也注册为加密的属性源,因此,当您要求某个属性是否被ENC()包围时,它会调用jasypt.encryptor.bean尝试对该属性进行解码。

希望这可以帮助!

干杯

10-01 04:57