cas 4.0 配置mysql_CAS 4.0 配置开发手冊

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值