2013-10-18 3 views
12

사용자를 인증하지 않습니다 다음 코드 (더 인증 실패가 발생하지 않지만, 호출에 의한 권한의 부족으로 실패) :HTTPBuilder 기본 인증이 작동하지 않는 이유는 무엇입니까?

def remote = new HTTPBuilder("http://example.com") 
remote.auth.basic('username', 'password') 
remote.request(POST) { req -> 
    uri.path = "/do-something" 
    uri.query = ['with': "data"] 

    response.success = { resp, json -> 
     json ?: [:] 
    } 
} 

그러나 다음 잘 작동 :

def remote = new HTTPBuilder("http://example.com") 
remote.request(POST) { req -> 
    uri.path = "/do-something" 
    uri.query = ['with': "data"] 
    headers.'Authorization' = 
       "Basic ${"username:password".bytes.encodeBase64().toString()}" 

    response.success = { resp, json -> 
     json ?: [:] 
    } 
} 

왜 ISN을 첫 번째 작품은?

+2

방법이 실패하는 데 도움이

protected final runGetRequest(String endpointPassedIn, RESTClient Client){ URIBuilder myEndpoint = new URIBuilder(new URI(Client.uri.toString() + endpointPassedIn)) HttpResponseDecorator unprocessedResponse = Client.get(uri: myEndpoint) as HttpResponseDecorator def Response = unprocessedResponse.getData() as LazyMap return Response } 

희망 다른 클래스에 캡슐화? 서버는 HTTP 401 상태 코드를 반환하여 기본 인증을 트리거해야합니다. 그러면 HttpBuilder는 Authorization 헤더와 함께 두 번째 요청을 보냅니다. – ataylor

+0

그냥 작동하지 않습니다. 요청 자체는 사용자에게 작업 수행 권한이 없다는 메시지와 함께 반환됩니다. 사용자 이름과 암호를 완전히 무효로 변경할 수 있으며 같은 일이 발생합니다. –

+2

그러면 잠재적으로 문제가 해결됩니다. http://stackoverflow.com/questions/6588256/using-groovy-using-groovy-http-builder-in-preemptive-mode –

답변

0

귀하의 서버가 HTTPBuilder-compilant가 아닌 것 같습니다. HTTPBuilder가이 상태를 포착하고 인증 요청을 다시 보내려면 401 코드 (REST 서버에 대한 표준 동작이지만 다른 서버에서는 표준이 아닌 동작)를 반환해야합니다. Here에 관해서는 작성되었습니다.

1

내 머리 꼭대기에서 생각할 수있는 두 가지.

.setHeaders 메서드에는지도가 필요합니다. 시도해 보셨습니까
'Authorization' : "Basic ${"username:password".bytes.encodeBase64().toString()}"?

그렇지 않은 경우 조금 더 많은 작업과 코드가 있지만 URIBuilder도 사용할 수 있습니다. 일반적으로 나는이