Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpClient向Jenkins进行身份验证_Java_Jenkins_Apache Httpclient 4.x - Fatal编程技术网

Java HttpClient向Jenkins进行身份验证

Java HttpClient向Jenkins进行身份验证,java,jenkins,apache-httpclient-4.x,Java,Jenkins,Apache Httpclient 4.x,我有一台詹金斯1.515服务器。这被配置为将访问控制委托给servlet容器(独立的Tomcat 6)。我使用基于矩阵的安全性,并为用户“foo”的每个操作勾选了方框。 我正在尝试使用HttpClient(4.2.3)来查询构建状态。使用基本的HttpClient身份验证,到目前为止,我已经: DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCr

我有一台詹金斯1.515服务器。这被配置为将访问控制委托给servlet容器(独立的Tomcat 6)。我使用基于矩阵的安全性,并为用户“foo”的每个操作勾选了方框。
我正在尝试使用HttpClient(4.2.3)来查询构建状态。使用基本的HttpClient身份验证,到目前为止,我已经:

DefaultHttpClient httpclient = new DefaultHttpClient();

    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope("dev.mycompany.com", 80), 
            new UsernamePasswordCredentials("foo", "bar"));

    try {

        HttpPost httpost = new HttpPost("http://dev.mycompany.com/jenkins/rssLatest");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpost, responseHandler);
        System.out.println(responseBody);

    } finally {
        httpclient.getConnectionManager().shutdown();
    }

现在,我已经尝试了许多不同的“示例”,通过Google找到了不同的方法来使用HttpClient进行身份验证,但所有这些都会导致上述相同的错误或“内部服务器错误”
我需要确定使用HttpClient 4对这个Jenkins实例进行身份验证的确切过程。

在尝试了Java直接身份验证方法后,我发现wget可以使用“基本”授权,然后我使用HttpClient模拟相同的请求/响应头。这不是我能找到的推荐方法,但它对我很有效。例如:

HttpGet httpget = new HttpGet("http://dev.mycompany.com/jenkins/rssLatest?token=MYTOKEN");
            httpget.setHeader("Host", "dev.mycompany.com");
            httpget.setHeader("Connection", "keep-alive");
            httpget.setHeader("Authorization", "Basic USERNAMEandMYTOKENbase64ENCRYPTED=" );

这是因为Jenkins在您未登录时返回错误403(禁止),但HttpClient希望登录时返回错误401(未经授权)。因此,HttpClient从未注意到Jenkins询问密码

解决此问题的一种方法是使用此处描述的“抢占式身份验证”:

这将与您所做的相同:始终将“Authorization”头添加到请求中

代码示例的副本:

CloseableHttpClient httpclient = <...>

HttpHost targetHost = new HttpHost("localhost", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
        new AuthScope(targetHost.getHostName(), targetHost.getPort()),
        new UsernamePasswordCredentials("username", "password"));

// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);

// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);

HttpGet httpget = new HttpGet("/");
for (int i = 0; i < 3; i++) {
    CloseableHttpResponse response = httpclient.execute(
            targetHost, httpget, context);
    try {
        HttpEntity entity = response.getEntity();

    } finally {
        response.close();
    }
}
CloseableHttpClient httpclient=
HttpHost targetHost=新的HttpHost(“本地主机”,80,“http”);
CredentialsProvider credsProvider=新的BasicCredentialsProvider();
credsProvider.setCredentials(
新建AuthScope(targetHost.getHostName(),targetHost.getPort()),
新用户名密码凭据(“用户名”、“密码”);
//创建AuthCache实例
AuthCache AuthCache=new BasicAuthCache();
//生成基本方案对象并将其添加到本地身份验证缓存
碱性血红素碱性血红素=新碱性血红素();
authCache.put(targetHost,basicAuth);
//将AuthCache添加到执行上下文
HttpClientContext=HttpClientContext.create();
context.setCredentialsProvider(credProvider);
setAuthCache(authCache);
HttpGet HttpGet=新的HttpGet(“/”);
对于(int i=0;i<3;i++){
CloseableHttpResponse response=httpclient.execute(
targetHost、httpget、context);
试一试{
HttpEntity=response.getEntity();
}最后{
response.close();
}
}

在Jenkins配置中,是否启用了
防止跨站点请求伪造漏洞利用
选项?“防止跨站点请求伪造漏洞利用”框未选中
CloseableHttpClient httpclient = <...>

HttpHost targetHost = new HttpHost("localhost", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
        new AuthScope(targetHost.getHostName(), targetHost.getPort()),
        new UsernamePasswordCredentials("username", "password"));

// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);

// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);

HttpGet httpget = new HttpGet("/");
for (int i = 0; i < 3; i++) {
    CloseableHttpResponse response = httpclient.execute(
            targetHost, httpget, context);
    try {
        HttpEntity entity = response.getEntity();

    } finally {
        response.close();
    }
}