import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
public class CasUtil {
//Cas server address
static final String server =
"http://localhost:9000/cas/login";
public static void main(String[] args)
throws IOException {
//Login and get the cookie
Cookie cookie = getTicketGrantingTicket(server,
"13082838818",
"13082838818");
if (cookie != null) {
System.out.println(cookie);
}
}
private static Cookie getTicketGrantingTicket(String server,
String username, String password) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(server);
//Login parameters
List nvps = new ArrayList();
nvps.add(new BasicNameValuePair("username", username));
nvps.add(new BasicNameValuePair("password", password));
String[] dynamicPara = doCasLoginRequest(client, server);
nvps.add(new BasicNameValuePair("lt", dynamicPara[0]));
nvps.add(new BasicNameValuePair("execution", dynamicPara[1]));
nvps.add(new BasicNameValuePair("_eventId",
"submit"));
//Font Code
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
try {
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
if (entity != null) {
//CASTGC: the default cookie name
Cookie cookie = getCookieValue(client, "CASTGC");
entity.consumeContent();
return cookie;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static Cookie getCookieValue(DefaultHttpClient httpclient,
String name) {
List cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
return null;
} else {
for (int i = 0; i < cookies.size(); i++) {
Cookie cookie = cookies.get(i);
if (cookie.getName().equalsIgnoreCase(name)) {
return cookie;
}
}
}
return null;
}
//Simulate the login action and get the dynamic parameters
lt and execution
private static String[] doCasLoginRequest(DefaultHttpClient httpclient,
String url) throws IOException {
String[] result = new String[2];
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
BufferedReader rd = new BufferedReader(new InputStreamReader(
entity.getContent(), "UTF-8"));
String tempLine = rd.readLine();
String sLt = "
String sEx = "
while (tempLine != null) {
int iLt = tempLine.indexOf(sLt);
int iEx = tempLine.indexOf(sEx);
if (iLt != -1) {
String s1 = tempLine.substring(iLt + sLt.length());
int index1 = s1.indexOf("\"");
if (index1 != -1)
result[0] = s1.substring(0, index1);
}
if (iEx != -1) {
String s1 = tempLine.substring(iEx + sEx.length());
int index1 = s1.indexOf("\"");
if (index1 != -1)
result[1] = s1.substring(0, index1);
}
tempLine = rd.readLine();
}
if (entity != null) {
entity.consumeContent();
}
return result;
}
//Cookie convert
private javax.servlet.http.Cookie
convertToServletCookie(Cookie cookie) {
javax.servlet.http.Cookie retCookie = new javax.servlet.http.Cookie(
cookie.getName(), cookie.getValue());
retCookie.setComment(cookie.getComment());
retCookie.setDomain(cookie.getDomain());
retCookie.setHttpOnly(false);
retCookie.setSecure(false);
retCookie.setPath(cookie.getPath());
retCookie.setVersion(cookie.getVersion());
retCookie.setMaxAge((int) ((cookie.getExpiryDate().getTime() - System
.currentTimeMillis()) / 1000));
return retCookie;
}
}