【发布时间】:2013-10-18 18:30:23
【问题描述】:
以下代码不对用户进行身份验证(不会发生身份验证失败,但由于权限不足导致调用失败):
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 ?: [:]
}
}
为什么第一个不起作用?
【问题讨论】:
-
它是如何失败的?服务器应返回 HTTP 401 状态代码以触发基本身份验证。然后 HttpBuilder 将发送带有 Authorization 标头的第二个请求。
-
这行不通。请求本身返回一条消息,说明用户无权执行该操作。我可以将用户名和密码更改为完全无效的东西,同样的事情也会发生。
-
这应该可以解决您的问题stackoverflow.com/questions/6588256/…
标签: groovy basic-authentication httpbuilder