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

标签 java authentication wso2-identity-server

我正在研究 WSO2IS,发现我可以使用 SCIM 从 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$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."}]}

关于java - 在 WSO2 IS 中获取使用 SCIM 的用户列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39482004/

相关文章:

java - 奇怪的 String.getByte(编码)

java - 使用 CustomListAdapter 时出现 NullPointerException

session - 如何在 Angular JS 应用程序中处理身份验证

即使具有有效 token ,仪表板中也没有应用程序的 WSO2 Identity Server

java - 添加和注册服务提供者并在 WSO2 中使用 Java 添加新的角色和权限

java - Spring 中的负载平衡 LDAP 连接

java - 使用 apt-get 在 Google Compute Engine 实例上安装 Java 8

google-chrome - 如何使用 FIDO U2F 允许用户通过我的网站进行身份验证?

grails - 登录后重定向?

wso2-api-manager - WSO2 APIM 3.2.0 管理门户无法访问