JSP exampleΒΆ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<%
    //Suppose you have already asked user to authenticate himself through a login form
    //The following is the action part of the login form

    //CHANGE THEM:
    String secret = "YOUR_SHARED_SECRET";
    String username = "USER_NAME";
    String email = "USER_EMAIL";
    String site = "YOUR_SITE_NAME";
    String displayName = "";//optional
    String phone = "";//optional
    String organization = "";//optional
    //END OF CHANGE THEM

    String redirectURL = "http://www.ihelpdesk.com/desktop/SsoServlet.hd?c=" + site + "&";

    String hash = org.apache.commons.codec.digest.DigestUtils.shaHex(secret+"@@"+username+"@@"+email);
    String parameters = "username="+username+"&email="+email;

    parameters +="&displayName="+displayName+"&phone="+phone+"&organization="+organization;

    parameters = new String(org.apache.commons.codec.binary.Base64.encodeBase64(parameters.getBytes()));

    //If usePostSubmit is true, then parameters will be submitted thru HTTP POST
    boolean usePostSubmit = false;
    if(usePostSubmit)
    {
        redirectURL += "&parameters="+parameters+"&hash=" + hash;
        response.sendRedirect(redirectURL);
    }
    else
    {
        StringBuilder formBuilder = new StringBuilder();
        formBuilder.append("<html><head><title>SSO Redirect</title></head><body>");
        formBuilder.append("<form id=\"ssoRedirectForm\" name=\"ssoRedirectForm\" method=\"POST\" action=\"").append(redirectURL).append("\"");
        formBuilder.append("<input type=\"hidden\" name=\"parameters\" value=\"").append(parameters).append("\"");
        formBuilder.append("<input type=\"hidden\" name=\"hash\" value=\"").append(hash).append("\"");
        formBuilder.append("</form><script type=\"text/javascript\">document.ssoRedirectForm.submit();</script></body></html>");
        out.print(formBuilder.toString());
    }
%>