为了向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 Cloud AWS Messaging& amp;自动配置,但到目前为止,我没有找到有关该主题的任何信息。我的 application.yml 看起来像这样
cloud:
aws:
credentials:
accessKey: ***
secretKey: ***
instanceProfile: true
region:
static: eu-central-1
Spring是否支持我并没有设法找到它,或者我应该坚持使用AWS SDK?
答案 0 :(得分:-1)
看起来没有开箱即用的解决方案。最简单的解决方法是创建自己的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();
}
}