2016-08-24 6 views
1

httpダイジェスト認証レストサービスのクライアントを実装する際に問題があります。私は適切にサービスを呼び出すことができていますCURLを使用した:私はそのトレースを見ることができるentity.jsonは、エンティティオブジェクトのJSON構造が含まれていジャージ2.xでHttpダイジェスト認証が失敗し、カールで動作する

curl -v --digest -u "username:password" -H "Content-Type: application/json" --data @entity.json http://<server>:<port>/path 

は、wiresharkのを使用して、サーバー

に送信すると、HTTP POSTが送信され、許可されたサーバ応答401が受信される。 md5パスワードのnounceなどの正しい組み合わせを使用する新しい投稿が送信され、200 OKがjsonレスポンスエンティティオブジェクトで受信されます。すべて順調。

私はこの使用してジャージー2.xのを実装しようとしてい

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

import javax.ws.rs.client.*; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response; 

public class RestApi 
{ 
    private static final Logger log = LoggerFactory.getLogger(RestApi.class) 

    private Client client; 

    private String server; 
    private String username; 
    private String password; 

    public RestApi(String server, String username, String password) { 
    this.server = server; 
    this.username = username; 
    this.password = password; 
    } 

    public Client getClient() { 
    if(this.client == null) { 
     HttpAuthenticationFeature feature = HttpAuthenticationFeature.digest(username, password); 
     this.client = ClientBuilder.newClient(); 
     this.client.register(feature) 
    } 
    return this.client; 
    } 

    public <T> T post(
    final String path, 
    final Object bodyValue, 
    final Class<T> c) throws RestPostException { 
    log.debug("POST path='{}', server='{}', bodyValue='{}'", path, server, bodyValue); 

    WebTarget webTarget = getClient().target(server).path(path); 
    Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON_TYPE); 

    Response response = invocationBuilder.post(Entity.entity(bodyValue, MediaType.APPLICATION_JSON_TYPE)); 

    if (response.getStatus() != 200) { 
     String errorString = response.readEntity(String.class); 
     throw new RestPostException(
     "POST failed to '" + server + "/" + path + "' body value: '" + bodyValue + "' : HTTP error code : " + response.getStatus() + 
      ". Error string:" + errorString); 
    } 
    T retval = response.readEntity(c); 
    return retval; 
    } 
} 

POSTメソッドは、このような別のクラスから呼び出されます。vを送信する私のJSONオブジェクトである

String returnedValue = getRestApi().post(
    path, 
    v, 
    String.class); 

要求に含まれます。

wiresharkでこれをトレースすると、POSTが送信されていることがわかります(POSTは、私がcurlで送信したものと正確に同じです)、401の不正な応答があります。新しい投稿が送信され、200 OKを取得できません。代わりに私は新しい401 Unauthorizedを取得します。

誰もこの経験がありますか? Javaコードを使用する方法に欠けている設定や設定などが表示されますか?

答えて

0

"HttpAuthenticationFeature.digest"で動作させることができませんでした。

しかし、それは標準のapache httpクライアントの仕方で動作します。 以下は、CredentialsProviderをdropwizardのJerseyClientBuilderに登録する例です。

JerseyClientConfiguration configuration = config.httpClient; 
    HttpHost target = HttpHost.create(config.apiUrl); 
    CredentialsProvider credsProvider = new BasicCredentialsProvider(); 
    credsProvider.setCredentials(new AuthScope(target), new UsernamePasswordCredentials(config.user, config.password)); 

    final Client httpClient = new JerseyClientBuilder(environment) 
      .using(configuration) 
      .using(credsProvider) 
      .build("my-client"); 
関連する問題