【问题标题】:Passing JWT in request header not working in groovy script在请求标头中传递 JWT 在 groovy 脚本中不起作用
【发布时间】:2018-05-15 11:37:20
【问题描述】:

我正在尝试通过 groovy 脚本从 CMDB(BMC atrium) 访问 CI 数据,它分两步进行。

首先向登录api发送POST请求并获取授权令牌。

第二次在后续的 api 调用中使用此令牌。

我已经在 postman 中测试了这两个 API,它们都运行良好。

获取令牌的部分在 groovy 脚本中工作正常,但使用此令牌获取 CI 数据的部分返回错误 json:

"JsonResponse is[{"messageType":"ERROR","messageText":"控制记录中必须提供用户名","messageAppendedText":null,"messageNumber":149}]"

这是有趣的部分,我回到邮递员并在授权标头中放置了一个新生成的令牌,它给了我与上面提到的相同的错误 json。当我在授权标头中附加带有令牌值的“AR-JWT”时 - 它工作正常!

我尝试在标头中传递不同的值,例如 - AuthenticationInfoValue.authentication = username 、 Authorization-Type 、用户名和密码等,但似乎没有任何效果。

我已经查看了 BMC 官方网站的文档,但据我所知,我做的一切都是正确的。 如果有人能指出我做错了什么,那真的很有帮助。

这里是 groovy 脚本

    @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
    //post call to get the token
    def token
    def httpRequester = new 
    groovyx.net.http.HTTPBuilder("http://<localhost>:<port>/api/jwt/login");
    httpRequester.request(groovyx.net.http.Method.POST) { req ->
    requestContentType = groovyx.net.http.ContentType.URLENC
    body = [username:'user', password:'pass']

    response.success = { resp , reader ->
                    println "response success"                
                    httpResponse = resp
                    println "httpResponse : "+httpResponse
                    token = reader.getText()
                    println "token : "+token
                }

                response.failure = { resp , reader ->
                    println "response failure"                
                    httpResponse = resp
                    println "httpResponse : "+httpResponse
                    token = reader.getText()  
                    println "token : "+token                  
                 }
            }
    //Get call to fetch the CI Data
    httpRequester = new groovyx.net.http.HTTPBuilder("http://<localhost>:<port>/api/arsys/v1/entry/AST:ComputerSystem")
    def finalToken = "AR-JWT" + " " + token ;
    httpRequester.request(groovyx.net.http.Method.GET) { req ->
    println "Inside request"
    def headerMap = ['Content-Type': 'application/x-www-form-urlencoded' , 
    'Authorization': finalToken]
    httpRequester.setHeaders(headerMap)
    println "headers  :  "+httpRequester.getHeaders()
    requestContentType = groovyx.net.http.ContentType.URLENC
    response.success = { resp, json ->
                    println "response success"                
                    httpResponse = resp
                    jsonResponse = json
                }

                response.failure = { resp, json ->
                    println "response failure"                
                    httpResponse = resp
                    jsonResponse = json
                }
            }



            jsonResponse = new groovy.json.JsonBuilder(jsonResponse);
            println "JsonResponse is" +jsonResponse.toString();

【问题讨论】:

    标签: groovy jwt remedy cmdb bmc


    【解决方案1】:

    我调试了很多次才发现这个问题,结果很傻。

    在 GET API 调用中必须设置如下标头。

    headers.'Content-Type'='application/x-www-form-urlencoded'
    ​headers.'Authorization'=finalToken
    

    并注释掉或删除下面的行。

        //def headerMap = ['Content-Type': 'application/x-www-form-urlencoded' , 'Authorization': finalToken]
        //httpRequester.setHeaders(headerMap)
    

    【讨论】:

      猜你喜欢
      • 2018-06-26
      • 2018-10-16
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多