我正在尝试通过HTTPS发送包含HTTP头中基本身份验证的REST请求,问题似乎是身份验证没有被插入到头部中。
HttpAuthenticationFeature feature = HttpAuthenticationFeature
.basicBuilder().build();
Client client = ClientBuilder.newBuilder().sslContext(getSSLContext())
.hostnameVerifier(getHostNameVerifier()).build();
client.register(feature);
client.register(new LoggingFilter());
try
{
String entity = client
.target(url)
.request(MediaType.APPLICATION_XML)
.property(
HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_USERNAME,
"username")
.property(
HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_PASSWORD,
"password").get(String.class);
System.out.println(entity);
} catch (WebApplicationException e)
{
ByteArrayInputStream in = (ByteArrayInputStream) e.getResponse()
.getEntity();
int n = in.available();
byte[] bytes = new byte[n];
in.read(bytes, 0, n);
String entity = new String(bytes, StandardCharsets.UTF_8);
System.out.println(entity);
}
日志记录内容:
Jun 16, 2015 2:06:53 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Sending client request on thread JavaFX Application Thread
1 > GET https://url
1 > Accept: application/xml
Jun 16, 2015 2:06:53 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 2 * Client response received on thread JavaFX Application Thread
2 < 403
2 < Connection: Keep-Alive
2 < Content-Length: 240
2 < Content-Type: text/html; charset=iso-8859-1
2 < Date: Tue, 16 Jun 2015 12:06:53 GMT
2 < Keep-Alive: timeout=15, max=100
结果代码只是403 Forbidden。
如果我删除client.register(feature);
这一行,那么2 < WWW-authenticate: basic realm="/"
这一行会被添加到日志的末尾,并且结果代码将变为401 Authorization Requried而不是403。
在FireFox中使用HTTP请求器时,REST请求正常工作。
我想我可能在某个地方遗漏了什么?
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
行。 - vonyx