如何在Web应用程序中集成cas restful?

时间:2016-05-24 01:52:25

标签: single-sign-on cas

我使用CAS build SSO,我想在我自己的Web应用程序中使用CAS RESTful API实现登录/注销。

和这样的api http://sso.cvs.cn:9990/cas-server-webapp/v1/tickets 我通过测试用例测试它,并且它成功了。这是代码:

public static void main(String... args) throws Exception {
    String username = "123";
    String password = "123";
    validateFromCAS(username, password);
}

public static boolean validateFromCAS(String username, String password) throws Exception {

    String url = "http://sso.cvs.cn:9990/cas-server-webapp/v1/tickets";
    try {
        HttpURLConnection hsu = (HttpURLConnection) openConn(url);
        String s = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
        s += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");

        System.out.println(s);
        OutputStreamWriter out = new OutputStreamWriter(hsu.getOutputStream());
        BufferedWriter bwr = new BufferedWriter(out);
        bwr.write(s);
        bwr.flush();
        bwr.close();
        out.close();

        String tgt = hsu.getHeaderField("location");
        System.out.println(hsu.getResponseCode());
        if (tgt != null && hsu.getResponseCode() == 201) {
            System.out.println(tgt);

            System.out.println("Tgt is : " + tgt.substring(tgt.lastIndexOf("/") + 1));
            tgt = tgt.substring(tgt.lastIndexOf("/") + 1);
            bwr.close();
            closeConn(hsu);

            String serviceURL = "http://sso.cvs.cn:7070/cas-simple-site-alpha/";
            String encodedServiceURL = URLEncoder.encode("service", "utf-8") + "=" + URLEncoder.encode(serviceURL, "utf-8");
            System.out.println("Service url is : " + encodedServiceURL);

            String myURL = url + "/" + tgt;
            System.out.println(myURL);
            hsu = (HttpURLConnection) openConn(myURL);
            out = new OutputStreamWriter(hsu.getOutputStream());
            bwr = new BufferedWriter(out);
            bwr.write(encodedServiceURL);
            bwr.flush();
            bwr.close();
            out.close();
            System.out.println("Response code is:  " + hsu.getResponseCode());
            BufferedReader isr = new BufferedReader(new InputStreamReader(hsu.getInputStream()));
            String line;
            System.out.println(hsu.getResponseCode());
            while ((line = isr.readLine()) != null) {
                System.out.println(line);
            }
            isr.close();
            hsu.disconnect();
            return true;
        } else {
            return false;
        }
    } catch (MalformedURLException mue) {
        mue.printStackTrace();
        throw mue;
    } catch (IOException ioe) {
        ioe.printStackTrace();
        throw ioe;
    }
}

但我如何在我的网络应用程序中使用api?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你的问题是api对你一般有用。

因此,如果是这种情况,在cas上启用restful api,当用户通过CAS服务器进行身份验证时,他将被允许访问配置为具有相同CAS服务器的SSO的其他应用程序(启用cas的服务) 。您也可以documentation建议使用POST来处理门票请求。

另一个原因是应用程序需要以编程方式访问CAS。假设一个casified应用程序可以代表经过身份验证的用户调用其他casified应用程序的REST API。为此,CAS Rest协议将完成工作