【问题标题】:Jira REST API with proxy (Java)带有代理的 Jira REST API (Java)
【发布时间】:2023-01-03 00:01:18
【问题描述】:

我想在需要通过代理访问所需 Jira 实例的应用程序中使用 Jira REST Client API for Java。不幸的是,在使用该库中的给定工厂时,我没有找到设置它的方法:

JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
String authentication = Base64.getEncoder().encodeToString("username:password".toBytes());
return factory.createWithAuthenticationHandler(URI.create(JIRA_URL), new BasicAuthenticationHandler(authentication));

我们如何使用 Jira API 并设置代理?

【问题讨论】:

    标签: java proxy jira


    【解决方案1】:

    我在互联网上找到的唯一解决方案是使用系统参数对其进行设置(参见下面的解决方案 1)。不幸的是,这不符合我的要求,因为在我工作的公司中,有多个代理,并且根据要调用的服务,它必须使用另一个代理配置。在那种情况下,我无法在不破坏对需要另一个代理的其他服务的所有调用的情况下设置系统属性。

    尽管如此,我还是找到了一种通过重新实现一些类来设置它的方法(参见解决方案 2)。

    重要限制:代理服务器不得要求凭据。

    1. 上下文

    也许作为之前的上下文,我创建了一个包含代理配置的类:

    @Data
    @AllArgsConstructor
    public class ProxyConfiguration {
    
        public static final Pattern PROXY_PATTERN = Pattern.compile("(https?):\/\/(.*):(\d+)");
    
        private String scheme;
        private String host;
        private Integer port;
    
        public static ProxyConfiguration fromPath(String path) {
            Matcher matcher = PROXY_PATTERN.matcher(path);
            if (matcher.find()) {
                return new ProxyConfiguration(matcher.group(1), matcher.group(2), toInt(matcher.group(3)));
            }
            return null;
        }
    
        public String getPath() {
            return scheme + "://" + host + ":" + port;
        }
    
    }
    
    1. 为代理设置系统属性

      在应用程序启动时或在使用 Jira REST API 之前使用您的代理配置调用以下方法:

      public static void configureProxy(ProxyConfiguration proxy) {
          if (proxy != null) {
              System.getProperties().setProperty("http.proxyHost", proxy.getHost());
              System.getProperties().setProperty("http.proxyPort", proxy.getPort().toString());
              System.getProperties().setProperty("https.proxyHost", proxy.getHost());
              System.getProperties().setProperty("https.proxyPort", proxy.getPort().toString());
          }
      }
      
      1. 重新实现AsynchronousHttpClientFactory

      不幸的是,由于此类有许多私有内部类和方法,您将不得不复制粘贴并更改以下代码以提供所需的代理配置:

      public DisposableHttpClient createClient(URI serverUri, ProxyConfiguration proxy, AuthenticationHandler authenticationHandler) {
          HttpClientOptions options = new HttpClientOptions();
      
          if (proxy != null) {
              options.setProxyOptions(new ProxyOptions.ProxyOptionsBuilder()
                  .withProxy(HTTP, new Host(proxy.getHost(), proxy.getPort()))
                  .withProxy(HTTPS, new Host(proxy.getHost(), proxy.getPort()))
                  .build());
          }
      
          DefaultHttpClientFactory<?> defaultHttpClientFactory = ...
      }
      

      然后你可以使用它(在下面的例子中,我对AsynchronousHttpClientFactory的重新实现被称为AtlassianHttpClientFactory):

      URI url = URI.create(JIRA_URL);
      String authentication = Base64.getEncoder().encodeToString("username:password".toBytes());
      DisposableHttpClient client = new AtlassianHttpClientFactory().createClient(url, proxy, new BasicAuthenticationHandler(authentication));
      return new AsynchronousJiraRestClient(url, client);
      

      请注意,在所有这些问题之后,我还决定编写一个支持身份验证、代理、多个 HTTP 客户端并与 CompletableFuture 异步工作的Jira client library

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      相关资源
      最近更新 更多