Spring Cloud AWS – Assume Role & Proxy configuration for STS & SQS

<br />
@Configuration<br />
public class AppConfig {<br />
    private static final String SESSION_NAME = &quot;sample&quot;;</p>
<p>    @Value(&quot;${proxy.host:#{null}}&quot;)<br />
    private String proxyHost;</p>
<p>    @Value(&quot;${proxy.port:0}&quot;)<br />
    private int proxyPort;</p>
<p>    @Value(&quot;${cloud.aws.region.static}&quot;)<br />
    private String region;</p>
<p>    @Bean<br />
    @Primary<br />
    public AWSCredentialsProvider awsCredentialsProvider(<br />
            @Value(&quot;${cloud.aws.credentials.accessKey}&quot;) String accessKey,<br />
            @Value(&quot;${cloud.aws.credentials.secretKey}&quot;) String secretKey,<br />
            @Value(&quot;${cloud.aws.role}&quot;) String role) {</p>
<p>        AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()<br />
                .withRegion(region)<br />
                .withClientConfiguration(clientConfiguration())<br />
                .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))<br />
                .build();</p>
<p>        return new STSAssumeRoleSessionCredentialsProvider<br />
                .Builder(role, SESSION_NAME)<br />
                .withStsClient(stsClient)<br />
                .build();<br />
    }</p>
<p>    @Bean<br />
    public QueueMessageHandlerFactory queueMessageHandlerFactory() {<br />
        QueueMessageHandlerFactory factory = new QueueMessageHandlerFactory();<br />
        MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter();</p>
<p>        messageConverter.setStrictContentTypeMatch(false);<br />
        factory.setArgumentResolvers(Collections.singletonList(new PayloadArgumentResolver(messageConverter)));<br />
        return factory;<br />
    }</p>
<p>    @Bean(name = &quot;amazonSQS&quot;)<br />
    public AmazonSQSAsync amazonSQSAsyncClient(AWSCredentialsProvider awsCredentialsProvider) {<br />
        return AmazonSQSAsyncClientBuilder.standard()<br />
                .withRegion(region)<br />
                .withCredentials(awsCredentialsProvider)<br />
                .withClientConfiguration(clientConfiguration())<br />
                .build();<br />
    }</p>
<p>    @Bean<br />
    ClientConfiguration clientConfiguration() {<br />
        ClientConfiguration clientConfiguration = new ClientConfiguration();</p>
<p>        if (!StringUtils.isEmpty(proxyHost)) {<br />
            clientConfiguration.setProxyHost(proxyHost);<br />
            clientConfiguration.setProxyPort(proxyPort);<br />
        }</p>
<p>        return clientConfiguration;<br />
    }<br />
}<br />

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *