2016-08-24 11 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 

은 와이어 샤크를 사용하여 서버

에 보낼를 HTTP를 POST가 보내지고 승인 된 서버 응답 401이 수신됩니다. md5 password nounce 등의 올바른 조합을 사용하는 새 게시물이 전송되고 200 OK가 json 응답 엔터티 개체와 함께 수신됩니다. 모든 것이 좋습니다. 사후 방법은 다음과 같이 다른 클래스에서 호출

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; 
    } 
} 

: V 보낼 내 JSON 객체가

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

입니다

나는이 사용 저지 2.X를 구현하기 위해 노력했다 요청에.

wireshark로 이것을 추적하면 POST가 전송되었다는 것을 알 수 있습니다 (POST는 추적 결과에 따라 말풍선과 정확히 일치합니다). 401 인증되지 않은 응답입니다. 새 게시물이 전송되고 200 OK를 얻지 못합니다. 대신에 새로운 401 Unauthorized를 얻습니다.

아무도이 경험이 있습니까? Java 코드를 사용하는 방식과 관련하여 누락 된 설정, 구성 등이 있습니까?

답변

0

"HttpAuthenticationFeature.digest"와 함께 작동시키지 못했습니다.

하지만 표준 아파치 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");