在 WSO2 IS 中使用 SCIM 获取用户列表

Get List Users with SCIM in WSO2 IS

我正在研究 WSO2IS,发现我可以将 SCIM 用于 create/update/delete/get 来自 WSO2 IS 的用户 我尝试使用以下 cURL 命令

curl -v -k --user admin:admin https://localhost:9443/wso2/scim/Users

并且有一个 JsonObject,其中包括命令行中的用户列表。

但是当我用 java 代码实现它时。我无法获得任何信息并遇到如下问题:

public static JSONObject getListUser() throws Exception {
    JSONObject json =null;
    try {

        String url = "https://localhost:9443/wso2/scim/Users/";
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
        TrustModifier.relaxHostChecking(con);
        byte[] bytesEncoded = Base64.getEncoder().encode((admin + ":" + admin).getBytes());
        String authorization = new String(bytesEncoded );

        con.setRequestMethod("POST");


con.setRequestProperty("Authorization", "Basic " + authorization);
//          con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty( "Accept", "*/*" );

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(url);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());

        JSONParser parser = new JSONParser();
        Object o = parser.parse(response.toString());
        json = (JSONObject)o;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return json;
}

异常情况如下:

java.io.IOException: Server returned HTTP response code: 400 for URL: https://localhost:9443/wso2/scim/Users/
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at sun.net.www.protocol.http.HttpURLConnection.run(HttpURLConnection.java:1890)
at sun.net.www.protocol.http.HttpURLConnection.run(HttpURLConnection.java:1885)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1884)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1457)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)

在这种情况下请帮助我。非常感谢!

成功了。它确实使用了一些已弃用的 API's ,需要从我这边修复。我使用 Apache HTTP Utils 来完成这个。这适用于 WSO2 IS

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;

public class HTTPTestClient {

    public static void main(String[] args) throws Exception {
        String url = "https://localhost:9443/wso2/scim/Users";
        BasicCookieStore cookieStore = new BasicCookieStore();

        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin", "admin");

        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
        CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLSocketFactory(sslsf)
                .setDefaultCookieStore(cookieStore).build();

        HttpGet get = new HttpGet(url);
        get.addHeader("Accept", "application/json");
        get.addHeader("charset", "UTF-8");
        get.addHeader("Content-Type", "application/json");

        Header header = new BasicScheme().authenticate(credentials, get);
        get.addHeader(header);

        HttpResponse response = httpclient.execute(get);
        System.out.println(response.getStatusLine());
        System.out.println("Response Code : " + response);

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        System.out.println(result);

    }
}

回复

HTTP/1.1 404 Not Found
Response Code : HttpResponseProxy{HTTP/1.1 404 Not Found [Cache-Control: private, Expires: Thu, 01 Jan 1970 05:30:00 IST, Date: Wed, 14 Sep 2016 04:48:38 GMT, Content-Type: application/json, Content-Length: 78, Server: WSO2 Carbon Server] ResponseEntityProxy{[Content-Type: application/json,Content-Length: 78,Chunked: false]}}
{"Errors":[{"code":"404","description":"Users not found in the user store."}]}