You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在使用jira-rest-java-client-core库时配置HTTPS代理?

解决Jira REST Java Client代理配置407错误的方案

问题原因

AsynchronousJiraRestClientFactory默认使用Apache HttpClient,它不会自动读取系统属性中的代理配置,尤其是代理身份验证部分。407错误本质是代理服务器要求客户端提供身份凭证,仅设置http.proxyHost/https.proxyPort这类系统属性无法满足认证要求。

解决方案:自定义HttpClient并配置代理(含认证)

通过显式构建带代理配置的HttpClient,再传入JiraRestClient的创建方法,即可解决问题。

完整代码示例

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.DefaultProxyRoutePlanner;
import org.apache.http.HttpHost;
import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.JiraRestClientFactory;
import com.atlassian.jira.rest.client.auth.BasicHttpAuthenticationHandler;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import java.net.URI;

public class JiraProxyConfig {
    public static void main(String[] args) {
        // 代理配置参数
        String PROXY_HOST = "your-proxy-host";
        int PROXY_PORT = 8080;
        String PROXY_USER = "proxy-username"; // 代理无需认证可省略
        String PROXY_PASSWORD = "proxy-password"; // 代理无需认证可省略
        
        // Jira配置参数
        String JIRA_URL = "xxxx-my-jira-url";
        String JIRA_USER = "userName";
        String JIRA_PASS = "password";

        // 1. 配置代理认证(解决407错误的核心步骤)
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
            new AuthScope(PROXY_HOST, PROXY_PORT),
            new UsernamePasswordCredentials(PROXY_USER, PROXY_PASSWORD)
        );

        // 2. 构建带代理的自定义HttpClient
        CloseableHttpClient httpClient = HttpClientBuilder.create()
            .setProxy(new HttpHost(PROXY_HOST, PROXY_PORT))
            .setDefaultCredentialsProvider(credsProvider)
            // 可选:配置本地/内部地址绕过代理
            .setRoutePlanner(new DefaultProxyRoutePlanner(new HttpHost(PROXY_HOST, PROXY_PORT)) {
                @Override
                protected HttpHost determineProxy(HttpHost target, org.apache.http.HttpRequest request, org.apache.http.protocol.HttpContext context) {
                    if (target.getHostName().equals("localhost") || target.getHostName().endsWith("your-internal-domain.com")) {
                        return null; // 该地址不走代理
                    }
                    return super.determineProxy(target, request, context);
                }
            })
            .build();

        // 3. 使用自定义HttpClient创建JiraRestClient
        JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
        URI jiraServerUri = URI.create(JIRA_URL);
        JiraRestClient restClient = factory.create(
            jiraServerUri,
            new BasicHttpAuthenticationHandler(JIRA_USER, JIRA_PASS),
            httpClient
        );

        // 后续使用restClient操作Jira,记得最后关闭资源:restClient.close();
    }
}

关键说明

  • 若代理无需身份验证,可删除CredentialsProvider相关代码,仅保留setProxy配置。
  • 该方案同时支持HTTP和HTTPS协议的Jira服务,HttpClient会自动处理HTTPS的代理隧道逻辑。
  • 必须使用factory.create()的重载方法传入自定义HttpClient,createWithBasicHttpAuthentication方法使用默认HttpClient,无法读取自定义代理配置。

内容的提问来源于stack exchange,提问作者Poor Monkey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 23:05:09