如何使用Jersey作为Rest客户端连接Ambari进行授权

时间:2014-06-06 04:16:51

标签: java rest jersey

我尝试使用Jersey建立一个休息客户端来连接Ambari服务器。但是,无法使用过滤器完成授权。

除了过滤器还有其他授权方式吗?

我尝试的案例如下:

ClientConfig 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);

1 个答案:

答案 0 :(得分:0)

您正在尝试使用凭据发布表单 - 这不是您想要的。对于标准基本身份验证(与curl -u username:password相同),您应该

webResource.addFilter(new HTTPBasicAuthFilter(username, password));