gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:01:41 24 4
gpt4 key购买 nike

需要将加密密码存储在.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com