我想使用一个示例配置文件连接到rds aurora示例,该示例配置文件利用sts来承担角色,这样我就不需要在解决方案中硬编码我的密码。我收到一个错误,说明我的用户没有访问权限。但是,当我用用户名和密码硬编码连接字符串时,我可以使用相同的用户进行连接。我已经检查了db用户权限,它们是正确的。我还尝试使用角色中的所有权限。我添加了日志记录来检查我是否收到了令牌。任何想法或帮助都将不胜感激。我一直关注的亚马逊文档是:https://docs.aws.amazon.com/amazonrds/latest/userguide/usingwithrds.iamdbauth.connecting.java.html
我的代码:
public final class Database {
private static String jdbcUrl = "jdbc:mysql://" + DockerConstants.PersistenceStoreUrl + ":" + DockerConstants.PersistenceStorePort + "/dbname";
private Database() {
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(jdbcUrl, setMySqlConnectionProperties());
}
static String generateAuthToken(){
RdsIamAuthTokenGenerator generator = RdsIamAuthTokenGenerator.builder()
.credentials(InstanceProfileCredentialsProvider.getInstance())
.region(EC2MetadataUtils.getEC2InstanceRegion())
.build();
String authToken = generator.getAuthToken(
GetIamAuthTokenRequest.builder()
.hostname(DockerConstants.PersistenceStoreUrl)
.port(DockerConstants.PersistenceStorePort)
.userName(DockerConstants.PersistenceStoreUserName)
.build());
return authToken;
}
private static Properties setMySqlConnectionProperties() {
Properties mysqlConnectionProperties = new Properties();
mysqlConnectionProperties.setProperty("verifyServerCertificate","false");
mysqlConnectionProperties.setProperty("useSSL", "false");
mysqlConnectionProperties.setProperty("user", DockerConstants.PersistenceStoreUserName);
mysqlConnectionProperties.setProperty("password",generateAuthToken());
return mysqlConnectionProperties;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!