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

标签 authentication groovy httpbuilder

当使用具有基本身份验证的groovy的http-builder时,默认行为是首先发送未经身份验证的请求,并在首先接收到401之后使用凭证重新发送该请求。
Apache的Httpclient提供preemptive authentication来在第一个请求上直接发送凭据。
如何在Groovy的http-builder中使用抢占式身份验证?任何代码示例,不胜感激。

最佳答案

基于JIRA issue,您可以执行以下操作:

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

关于authentication - 在抢占模式下使用groovy http-builder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6588256/

相关文章:

python - 使用 angularjs 和 flask 进行用户身份验证的正确方法

grails - 从grails Controller 调用scripts文件夹下的脚本

grails - 从HTTPResponse闭合Grails HTTPBuilder返回值到外部方法

php - Groovy httpbuilder ResponseParseException无效的JSON字符串

PHP/MySQLi/登录脚本

django - Mongoengine 用户认证

c# - 安全代码生成/比较和原子钟

java - 从 spock 模拟执行回调

groovy - 依赖于扩展对象的任务的 Gradle 插件最佳实践

unit-testing - Groovy HTTPBuilder模拟响应