我一直在寻找从aws秘密管理器获取数据库配置属性的方法,但结果仅显示为春季启动。我是新的spring mvc,请建议我需要实现哪些步骤。
下面的示例展示了如何从aws秘密管理器获取secret值。参考
添加以下依赖项:
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>secretsmanager</artifactId>
<version>2.14.7</version>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.17.46</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
在配置数据源时,您可以在spring mvc项目中使用以下组件:
config.properties
newSecretName= <Set the secret name>
secretValue= <Set the secret value>
modSecretValue= <Set an updated secret value>
GetSecretValue.java
package com.example.secrets;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse;
import software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException;
public class GetSecretValue {
public static void main(String[] args) {
final String USAGE = "n" + "Usage:n" + " <secretName> nn" + "Where:n"
+ " secretName - the name of the secret (for example, tutorials/MyFirstSecret). n";
if (args.length != 1) {
System.out.println(USAGE);
System.exit(1);
}
String secretName = args[0];
Region region = Region.US_EAST_1;
SecretsManagerClient secretsClient = SecretsManagerClient.builder().region(region).build();
getValue(secretsClient, secretName);
secretsClient.close();
}
public static void getValue(SecretsManagerClient secretsClient, String secretName) {
try {
GetSecretValueRequest valueRequest = GetSecretValueRequest.builder().secretId(secretName).build();
GetSecretValueResponse valueResponse = secretsClient.getSecretValue(valueRequest);
String secret = valueResponse.secretString();
System.out.println(secret);
} catch (SecretsManagerException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
}
示例中使用的Hibernate配置是基于Hibernate java的配置。
@Configuration
@EnableTransactionManagement
public class AppContext {
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] {
"net.knf.demo.entity"
});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
//**Here you can set the secrets from aws secret manager while configuring datasource.**
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(<DriveClassName>));
dataSource.setUrl(<URL>);
dataSource.setUsername(<UserName>);
dataSource.setPassword(<password>);
return dataSource;
}
private Properties hibernateProperties() {
.........
return properties;
}
@Bean
public HibernateTransactionManager getTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
}