簡體   English   中英

在WSO2 IS中使用SCIM獲取列表用戶

[英]Get List Users with SCIM in WSO2 IS

我正在研究WSO2IS,發現我可以使用SCIM從WSO2創建/更新/刪除/獲取用戶,我嘗試使用以下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$10.run(HttpURLConnection.java:1890)
at sun.net.www.protocol.http.HttpURLConnection$10.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,需要從我這一邊進行修復。 我使用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."}]}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM