在抢占模式下使用 groovy http-builder

发布于 2024-11-18 09:15:12 字数 296 浏览 3 评论 0原文

当使用带有基本身份验证的 groovy 的 http-builder 时,默认行为是首先发送未经身份验证的请求,并在首先收到 401 后重新发送带有凭据的请求。 Apache 的 Httpclient 提供抢占式身份验证,直接在第一次发送凭据要求。 如何在 Groovy 的 http-builder 中使用抢占式身份验证?任何代码示例都值得赞赏。

When using groovy's http-builder with basic authentication the default behavior is to send an unauthenticated request first and resend the request with credentials after receiving a 401 in the first place.
Apache's Httpclient offers preemptive authentication to send the credentials directly on the first request.
How can I use preemptive auth in Groovy's http-builder? Any code examples are appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还给你自由 2024-11-25 09:15:13

与 Jenkins 一起使用。

def accessToken = "ACCESS_TOKEN".bytes.encodeBase64().toString()
def req = new URL("https://raw.githubusercontent.com/xxxx/something/hosts").openConnection();
req.setRequestProperty("Authorization", "Basic " + accessToken)
def content = req.getInputStream().getText()

Used following with Jenkins.

def accessToken = "ACCESS_TOKEN".bytes.encodeBase64().toString()
def req = new URL("https://raw.githubusercontent.com/xxxx/something/hosts").openConnection();
req.setRequestProperty("Authorization", "Basic " + accessToken)
def content = req.getInputStream().getText()
从来不烧饼 2024-11-25 09:15:12

您还可以通过常规方式解决它

http = new RESTClient('http://awesomeUrl/')
http.headers['Authorization'] = 'Basic '+"myUsername:myPassword".getBytes('iso-8859-1').encodeBase64()

You can also solve it groovy style with

http = new RESTClient('http://awesomeUrl/')
http.headers['Authorization'] = 'Basic '+"myUsername:myPassword".getBytes('iso-8859-1').encodeBase64()
っ左 2024-11-25 09:15:12

基于 JIRA问题你可以这样做:

def http = new RESTClient('http://awesomeUrl/')

http.client.addRequestInterceptor(new HttpRequestInterceptor() {
    void process(HttpRequest httpRequest, HttpContext httpContext) {
        httpRequest.addHeader('Authorization', 'Basic ' + 'myUsername:myPassword'.bytes.encodeBase64().toString())
    }
})

def response = http.get(path: "aResource")

println response.data.text

Based on a JIRA issue you can do something like that :

def http = new RESTClient('http://awesomeUrl/')

http.client.addRequestInterceptor(new HttpRequestInterceptor() {
    void process(HttpRequest httpRequest, HttpContext httpContext) {
        httpRequest.addHeader('Authorization', 'Basic ' + 'myUsername:myPassword'.bytes.encodeBase64().toString())
    }
})

def response = http.get(path: "aResource")

println response.data.text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文