gpt4 book ai didi

api - 如何为 Jenkins 申请面包屑发行人

转载 作者:行者123 更新时间:2023-12-03 09:52:24 28 4
gpt4 key购买 nike

我想使用Jenkins Remote API,并且正在寻找安全的解决方案。我遇到了Prevent Cross Site Request Forgery exploits,想使用它,但是我读到某个地方,您必须提出一个面包屑请求。

如何获得面包屑请求以使API正常工作?

我找到了这个https://github.com/entagen/jenkins-build-per-branch/pull/20,但是我仍然不知道如何解决它。

我的Jenkins版本是1.50.x。

Authenticated remote API request responds with 403 when using POST request

最佳答案

我也没有在文档中找到这个。此代码已针对较旧的Jenkins(1.466)进行了测试,但仍然可以使用。

要发出面包屑,请使用crumbIssuer

// left out: you need to authenticate with user & password -> sample below
HttpGet httpGet = new HttpGet(jenkinsUrl + "crumbIssuer/api/json");
String crumbResponse = toString(httpclient, httpGet);
CrumbJson crumbJson = new Gson().fromJson(crumbResponse, CrumbJson.class);

这会给你这样的回应
{"crumb":"fb171d526b9cc9e25afe80b356e12cb7","crumbRequestField":".crumb"}

其中包含您需要的两条信息
  • 需要传递面包屑的字段名称
  • 面包屑本身

  • 如果您现在想从 Jenkins (Jenkins)那里获取东西,请将面包屑添加为标题。在下面的示例中,我获取了最新的构建结果。

    HttpPost httpost = new HttpPost(jenkinsUrl + "rssLatest");
    httpost.addHeader(crumbJson.crumbRequestField, crumbJson.crumb);

    这里是整体示例代码。我正在使用 gson 2.2.4解析响应,其余部分则使用 Apache's httpclient 4.2.3解析。

    import org.apache.http.auth.*;
    import org.apache.http.client.*;
    import org.apache.http.client.methods.*;
    import org.apache.http.impl.client.*;

    import com.google.gson.Gson;

    public class JenkinsMonitor {

    public static void main(String[] args) throws Exception {

    String protocol = "http";
    String host = "your-jenkins-host.com";
    int port = 8080;
    String usernName = "username";
    String password = "passwort";

    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getCredentialsProvider().setCredentials(
    new AuthScope(host, port),
    new UsernamePasswordCredentials(usernName, password));

    String jenkinsUrl = protocol + "://" + host + ":" + port + "/jenkins/";

    try {
    // get the crumb from Jenkins
    // do this only once per HTTP session
    // keep the crumb for every coming request
    System.out.println("... issue crumb");
    HttpGet httpGet = new HttpGet(jenkinsUrl + "crumbIssuer/api/json");
    String crumbResponse= toString(httpclient, httpGet);
    CrumbJson crumbJson = new Gson()
    .fromJson(crumbResponse, CrumbJson.class);

    // add the issued crumb to each request header
    // the header field name is also contained in the json response
    System.out.println("... issue rss of latest builds");
    HttpPost httpost = new HttpPost(jenkinsUrl + "rssLatest");
    httpost.addHeader(crumbJson.crumbRequestField, crumbJson.crumb);
    toString(httpclient, httpost);

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

    }

    // helper construct to deserialize crumb json into
    public static class CrumbJson {
    public String crumb;
    public String crumbRequestField;
    }

    private static String toString(DefaultHttpClient client,
    HttpRequestBase request) throws Exception {
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = client.execute(request, responseHandler);
    System.out.println(responseBody + "\n");
    return responseBody;
    }

    }

    关于api - 如何为 Jenkins 申请面包屑发行人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16738441/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com