【问题标题】:HTTP NTLM authentication in jenkins pipeline script詹金斯管道脚本中的 HTTP NTLM 身份验证
【发布时间】:2021-08-07 12:51:21
【问题描述】:

我正在尝试发布需要 NTLM 身份验证的请求。 curl 命令在我进行调用后工作正常,但相同的方法请求不适用于 jenkins 管道脚本。

卷曲命令:

curl -X POST -k -v -H \"Content-Type: application/json\" -H \"Content-Length: 0\" --ntlm -u domain/username:password http://blrapi/ExeWebAPI/testplans/run/username/89cd1093-6558-4321-b689-cb1

Jenkins 流水线代码

def getClient(){
    def server = ""
    def username = "username"
    def userpassword = "password"
    def domain = "domain"

    def client = new HttpClient()
    client.state.setCredentials(
       AuthScope.ANY,
        new NTCredentials(username, password, "", domain)
    )
    return client
}

def RunPlan( planId ){
    SknetPost("hhttp://blrapi/ExeWebAPI/testplans/run/username/89cd1093-6558-4321-b689-cb1","")
 }

def skynetExecute(httpMethod){
    def httpResponse = ""
    def sknetClient = getClient()

    try {
        int result = sknetClient.executeMethod(httpMethod)
        println "Return code: ${result}"
        httpResponse = httpMethod.getResponseBodyAsString()
    } 
    finally {
        httpMethod.releaseConnection()
    }
    return httpResponse
 }

void SknetPost(url, jsondata) {
    def post = new PostMethod( url )
    post.doAuthentication = true
    post.setRequestHeader("Content-type", "application/json")

    StringRequestEntity requestEntity = new StringRequestEntity( jsonData , "text/html", "UTF-8");
    post.setRequestEntity(requestEntity);
    httpResponse = sknetExecute(post)
    return httpResponse
 }
}

当我执行程序时,它会给出 401-未经授权的访问错误。使用相同的凭据 curl 命令它工作正常,但在詹金斯管道它失败了。

请帮我解决这个问题。

【问题讨论】:

    标签: jenkins ntlm http-status-code-401


    【解决方案1】:

    来自 Jenkins 管道的具有 NTLM 身份验证的 Web 请求可以使用 HTTP Request Plugin 来实现。
    在 jenkins 凭证存储中添加凭证(用户/密码)。
    然后,您可以在管道中使用 httpRequest

    script {
        def response = httpRequest httpMode: 'GET',
            url: "http://localhost:80",
            authentication: '3bb9use-your-id-from-store',
            useNtlm: true
        println("Status: "+response.status)
        println("Content: "+response.content)
    }
    

    问候。
    使用 jenkins 2.324,HTTP 请求插件 1.12 为我工作

    【讨论】:

      猜你喜欢
      • 2013-03-28
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多