Points To Remember
- Make sure that your CAS Server is up and running. How to set up CAS Rest api with JDBC Authentication.
- You have created a database and have dummy data in it.
Program : Test CAS Rest Api from a Java Code
You can use the following piece of code to test the CAS Rest API.import java.io.BufferedReader;You will get the following type of response if everything is working fine.
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
public class TestCASRest {
public static void main(String... args) throws Exception
{
String username ="ekansh@ekiras.com";
String password ="password";
validateFromCAS(username,password);
}
public static boolean validateFromCAS(String username, String password) throws Exception
{
String url = "https://ekansh:8443/cas/v1/tickets";
try
{
HttpsURLConnection hsu = (HttpsURLConnection)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("string s = "+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://ekansh:8090/";
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 = (HttpsURLConnection)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;
}
}
static URLConnection openConn(String urlk) throws MalformedURLException, IOException
{
URL url = new URL(urlk);
HttpsURLConnection hsu = (HttpsURLConnection) url.openConnection();
hsu.setDoInput(true);
hsu.setDoOutput(true);
hsu.setRequestMethod("POST");
return hsu;
}
static void closeConn(HttpsURLConnection c)
{
c.disconnect();
}
}
string s = username=admin%40gmail.com&password=igdefaultIf the authentication fails at the server then you will get a response like the following
201
https://ekansh:8443/cas/v1/tickets/TGT-3-eeSgMb35iv3TkqYzqekyhjZcGyNwKKq2s94reeoRINtdKJBvjx-ekansh
Tgt is : TGT-3-eeSgMb35iv3TkqYzqekyhjZcGyNwKKq2s94reeoRINtdKJBvjx-ekansh
Service url is : service=http%3A%2F%2Fekansh%3A8090%2F
https://ekansh:8443/cas/v1/tickets/TGT-3-eeSgMb35iv3TkqYzqekyhjZcGyNwKKq2s94reeoRINtdKJBvjx-ekansh
Response code is: 200
200
ST-3-Uagfse2pVB7hR5GL09WB-ekansh
string s = username=ekansh%40ekiras.com&password=pwdTGT - It is the token by the ticket granting token.
400
ST-3-Uagfse2pVB7hR5GL09WB-ekansh - It is the service token.
No comments :
Post a Comment