承担 Spring 云 AWS 自动配置的角色
Assume Role with Spring Cloud AWS Autoconfiguration
为了向 SNS 发布消息,我需要承担正确的角色,因此在使用 AWS SDK 时,我创建了一个 AmazonSNS bean,如下所示:
@Bean
public AmazonSNS amazonSnsClient(
@Value("${cloud.aws.credentials.accessKey}") String accessKey,
@Value("${cloud.aws.credentials.secretKey}") String secretKey,
@Value("${cloud.aws.role.publisherArn}") String publisherRoleArn
) {
AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
.withRegion(EU_CENTRAL_1)
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
STSAssumeRoleSessionCredentialsProvider credentialsProvider = new STSAssumeRoleSessionCredentialsProvider
.Builder(publisherRoleArn, SESSION_NAME)
.withStsClient(stsClient)
.build();
return AmazonSNSClientBuilder.standard()
.withRegion(EU_CENTRAL_1)
.withCredentials(credentialsProvider)
.build();
}
我正在尝试对 Spring 云 AWS 消息传递和自动配置做同样的事情,但到目前为止我没有找到关于该主题的任何信息。我的 application.yml 看起来像这样
cloud:
aws:
credentials:
accessKey: ***
secretKey: ***
instanceProfile: true
region:
static: eu-central-1
它是否受 Spring 支持,我只是没能找到它,还是我应该坚持使用 AWS SDK?
看起来没有开箱即用的解决方案。最简单的解决方法是创建您自己的 AWSCredentialsProvider bean
@Configuration
public class AwsCredentials {
private static final String SESSION_NAME = "TestConsumer";
@Bean
@Primary
public AWSCredentialsProvider credentialsProvider(
@Value("${cloud.aws.credentials.accessKey}") String accessKey,
@Value("${cloud.aws.credentials.secretKey}") String secretKey,
@Value("${cloud.aws.role}") String role ) {
AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
.withRegion(EU_CENTRAL_1)
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
return new STSAssumeRoleSessionCredentialsProvider
.Builder(role, SESSION_NAME)
.withStsClient(stsClient)
.build();
}
}
为了向 SNS 发布消息,我需要承担正确的角色,因此在使用 AWS SDK 时,我创建了一个 AmazonSNS bean,如下所示:
@Bean
public AmazonSNS amazonSnsClient(
@Value("${cloud.aws.credentials.accessKey}") String accessKey,
@Value("${cloud.aws.credentials.secretKey}") String secretKey,
@Value("${cloud.aws.role.publisherArn}") String publisherRoleArn
) {
AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
.withRegion(EU_CENTRAL_1)
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
STSAssumeRoleSessionCredentialsProvider credentialsProvider = new STSAssumeRoleSessionCredentialsProvider
.Builder(publisherRoleArn, SESSION_NAME)
.withStsClient(stsClient)
.build();
return AmazonSNSClientBuilder.standard()
.withRegion(EU_CENTRAL_1)
.withCredentials(credentialsProvider)
.build();
}
我正在尝试对 Spring 云 AWS 消息传递和自动配置做同样的事情,但到目前为止我没有找到关于该主题的任何信息。我的 application.yml 看起来像这样
cloud:
aws:
credentials:
accessKey: ***
secretKey: ***
instanceProfile: true
region:
static: eu-central-1
它是否受 Spring 支持,我只是没能找到它,还是我应该坚持使用 AWS SDK?
看起来没有开箱即用的解决方案。最简单的解决方法是创建您自己的 AWSCredentialsProvider bean
@Configuration
public class AwsCredentials {
private static final String SESSION_NAME = "TestConsumer";
@Bean
@Primary
public AWSCredentialsProvider credentialsProvider(
@Value("${cloud.aws.credentials.accessKey}") String accessKey,
@Value("${cloud.aws.credentials.secretKey}") String secretKey,
@Value("${cloud.aws.role}") String role ) {
AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
.withRegion(EU_CENTRAL_1)
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
return new STSAssumeRoleSessionCredentialsProvider
.Builder(role, SESSION_NAME)
.withStsClient(stsClient)
.build();
}
}