21

基本認証でgroovyのhttp-builderを使用する場合、デフォルトの動作では、最初に認証されていない要求を送信し、最初に401を受信した後に資格情報を使用して要求を再送信します。ApacheのHttpclientは、最初のリクエストで直接資格情報を送信するためのプリエンプティブ認証を提供します。Groovyのhttp-builderでプリエンプティブ認証を使用するにはどうすればよいですか?コード例はありがたいです。

4

2 に答える 2

37

グルーヴィーなスタイルで解決することもできます

http = new RESTClient('http://awesomeUrl/')
http.headers['Authorization'] = 'Basic '+"myUsername:myPassword".getBytes('iso-8859-1').encodeBase64()
于 2013-03-07T20:16:59.263 に答える
34

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
于 2011-07-10T15:38:29.193 に答える