我尝试使用Jersey使rest客户端连接Ambari服务器。但是,不能使用Filter完成授权。
除了Filter之外,还有其他授权方式吗?
我尝试的情况如下:
ClientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(
JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
client.addFilter(new ClientFilter() {
private ArrayList<Object> cookies;
@Override
public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
if (cookies != null) {
request.getHeaders().put("Cookie", cookies);
}
ClientResponse response = getNext().handle(request);
// copy cookies
if (response.getCookies() != null) {
if (cookies == null) {
cookies = new ArrayList<Object>();
}
// A simple addAll just for illustration (should probably check for duplicates and expired cookies)
cookies.addAll(response.getCookies());
}
return response;
}
});
String username = "admin";
String password = "admin";
WebResource webResource = client.resource("http://master.node.ibm.com:8080/api/v1/clusters");
Form form = new Form();
form.putSingle("Username", username);
form.putSingle("Password", password);
webResource.type("application/json").post(form);
ClientResponse response = webResource.accept("application/json").type("application/json").get(ClientResponse.class);
您正在尝试发布具有凭据的表单-这不是您想要的。对于标准的基本身份验证(与curl -u username:password
相同),您应该
webResource.addFilter(new HTTPBasicAuthFilter(username, password));