【问题标题】:AWS Java SDK behind a corporate proxy企业代理背后的 AWS Java 开发工具包
【发布时间】:2019-02-08 14:51:53
【问题描述】:

我想在本地测试我的 AWS 代码,所以我必须为 AWS 客户端设置代理。

在我的环境中通过变量HTTPS_PROXY 设置了一个代理主机(http://user@pass:my-corporate-proxy.com:8080)。

我没有找到如何设置整个代理的方法,所以我想出了以下代码:

AmazonSNS sns = AmazonSNSClientBuilder.standard()
    .withClientConfiguration(clientConfig(System.getenv("HTTPS_PROXY")))
    .withRegion(Regions.fromName(System.getenv("AWS_REGION")))
    .withCredentials(new DefaultAWSCredentialsProviderChain())
    .build();

ClientConfiguration clientConfig(String proxy) {
    ClientConfiguration configuration = new ClientConfiguration();
    if (proxy != null && !proxy.isEmpty()) {
        Matcher matcher = Pattern.compile("(\\w{3,5})://((\\w+):(\\w+)@)?(.+):(\\d{1,5})").matcher(proxy);
        if (!matcher.matches()) {
            throw new IllegalArgumentException("Proxy not valid: " + proxy);
        }
        configuration.setProxyHost(matcher.group(5));
        configuration.setProxyPort(Integer.parseInt(matcher.group(6)));
        configuration.setProxyUsername(matcher.group(3));
        configuration.setProxyPassword(matcher.group(4));
    }
    return configuration;
}

整个方法clientConfig 只是样板代码。

有什么优雅的方法可以做到这一点吗?

【问题讨论】:

  • 老实说,考虑到您的环境,这听起来和它会得到的一样好。我不确定在未设置代理的情况下发送一个空的ClientConfiguration 会做什么,因为我没有使用它,但我假设在这种情况下它会被忽略。
  • clientConfiguration.setProxy(String proxy) 这样的方法会好得多...

标签: amazon-web-services aws-java-sdk


【解决方案1】:

据我所知,在使用 AWS SDK V1 (1.11.840) 时,您是否在运行时设置了诸如 HTTP(S)_PROXYhttp(s)_proxy 之类的环境变量,或者诸如 http(s).proxyHostproxyPort、@ 之类的属性987654326@ 和 proxyPassword 传递给您的应用程序,您不必设置任何这些。它将automatically 读入新创建的ClientConfigiration

因此,如果需要,您只想设置ProxyAuthenticationMethod

ClientConfiguration clientConfig(ProxyAuthenticationMethod authMethod) {
    ClientConfiguration conf = new ClientConfiguration();
    List<ProxyAuthenticationMethod> proxyAuthentication = new ArrayList<>(1);
    proxyAuthentication.add(authMethod);
    conf.setProxyAuthenticationMethods(proxyAuthentication);
    return conf;
}

ProxyAuthenticationMethod 可以是ProxyAuthenticationMethod.BASICDIGESTKERBEROSNTLMSPNEGO

【讨论】:

    猜你喜欢
    • 2012-02-19
    • 2015-12-25
    • 2015-09-20
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多