连接SSO CAS的代码示例

来源:互联网 发布:u盘装ubuntu 编辑:程序博客网 时间:2024/04/29 00:16

package test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;

public class Test {

/**
* 验证当前请求是否登录过
*
* @param req
* @return SSOResponse.isValid()表示了是否登录成功,userCode,userName等表示登录的用户代码和用户名
*/
public SSOResponse validateToken(HttpServletRequest req) {
HttpClient http = new HttpClient();
String token = this.getToken(req);
try {
HttpClientParams ps = http.getParams();
ps.setContentCharset("UTF-8");
ps.setHttpElementCharset("UTF-8");
ps.setCredentialCharset("UTF-8");
// 集成测试环境的SSO CAS的接口地址
String ssoUrl = "http://192.168.64.38:8083/cas/ssoToken";
PostMethod pm = new PostMethod(ssoUrl);
pm.addParameter(new NameValuePair("cmd", "validateSSO"));
// 应用代码为INTRAL
pm.addParameter(new NameValuePair("appCode", "INTRA"));
// SSO令牌,非常重要
pm.addParameter(new NameValuePair("token", "" + token));

// 其余值可以设为空
pm.addParameter(new NameValuePair("visitUrl", ""));
pm.addParameter(new NameValuePair("validatePrivilege", ""));
pm.addParameter(new NameValuePair("userIp", ""));
pm.addParameter(new NameValuePair("explorerType", ""));

int status = http.executeMethod(pm);
if (status == HttpStatus.SC_OK) {
byte[] data = pm.getResponseBody();
String resStr = new String(data, "UTF-8");
String t[] = this.splitString(resStr, "!@!");
if (t != null && t.length > 17) {
SSOResponse rt = new SSOResponse();
rt.setValid("true".equalsIgnoreCase(t[0]));
rt.setErrorCode(t[1]);
rt.setToken(t[2]);
rt.setUserId(t[3]);
rt.setUserCode(t[4]);
rt.setUserName(t[5]);
rt.setOrgId(t[6]);
rt.setOrgCode(t[7]);
rt.setOrgName(t[8]);
rt.setDepartmentId(t[9]);
rt.setDepartmentCode(t[10]);
rt.setDepartmentName(t[11]);
rt.setMemo(t[12]);
rt.setUserType(t[13]);
rt.setOtherInfo1(t[14]);
rt.setOtherInfo2(t[15]);
rt.setOtherInfo3(t[16]);
rt.setOtherInfo4(t[17]);
return rt;
}
}
return null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

private String getCookieValue(HttpServletRequest request, String name) {
Cookie[] cks = request.getCookies();
if (cks != null) {
for (int i = 0; i < cks.length; i++) {
String cookieName = cks[i].getName();
int n = cookieName.indexOf(".");
if (n > 0)
cookieName = cookieName.substring(0, n);
if (cookieName.equals(name))
return cks[i].getValue();
}
}
return null;
}

private String getToken(HttpServletRequest request) {
String token = getCookieValue(request, "T99BILLCOM");
if (token != null && token.trim().length() > 0) {
int n = token.indexOf(".");
if (n > 0)
token = token.substring(0, n);
return token;
} else {
token = getCookieValue(request, "T99BILLCOMSECURE");
if (token != null && token.trim().length() > 0) {
int n = token.indexOf(".");
if (n > 0)
token = token.substring(0, n);
return token;
}
}
return null;
}

private String[] splitString(String str, String delim) {
List<String> list = new ArrayList<String>();

String strTemp = str;
while (true) {
int n = strTemp.indexOf(delim);
if (n < 0) {
list.add(strTemp);
break;
}
list.add(strTemp.substring(0, n));
strTemp = strTemp.substring(n + delim.length());
}
String[] retStr = new String[list.size()];
for (int i = 0; i < list.size(); i++)
retStr[i] = (String) list.get(i);
return retStr;
}

}

class SSOResponse {
private static final long serialVersionUID = -3173520753149462168L;
/**
* 验证的结果
*/
private boolean valid;
/**
* 错误码
*/
private String errorCode = "";

/**
* 令牌
*/
private String token;
/**
* 会员ID
*/
private String userId;

/**
* 用户代码
*/
private String userCode;
/**
* 用户名称
*/
private String userName;
/**
* 组织ID
*/
private String orgId;
/**
* 组织代码
*/
private String orgCode;
/**
* 组织名称
*/
private String orgName;
/**
* 部门ID
*/
private String departmentId;
/**
* 部门代码
*/
private String departmentCode;
/**
* 部门名称
*/
private String departmentName;
/**
* 备注
*/
private String memo;

/**
* 令牌更新时间
*/
private long updateTime = System.currentTimeMillis();
/**
* 创建时间
*/
private Date createTime = new Date();

private String userType;
private String otherInfo1;
private String otherInfo2;
private String otherInfo3;
private String otherInfo4;

/**
* @return the valid
*/
public boolean isValid() {
return valid;
}

/**
* @param valid
* the valid to set
*/
public void setValid(boolean valid) {
this.valid = valid;
}

/**
* @return the errorCode
*/
public String getErrorCode() {
return errorCode;
}

/**
* @param errorCode
* the errorCode to set
*/
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public String getUserCode() {
return userCode;
}

public void setUserCode(String userCode) {
this.userCode = userCode;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getOrgId() {
return orgId;
}

public void setOrgId(String orgId) {
this.orgId = orgId;
}

public String getOrgCode() {
return orgCode;
}

public void setOrgCode(String orgCode) {
this.orgCode = orgCode;
}

public String getOrgName() {
return orgName;
}

public void setOrgName(String orgName) {
this.orgName = orgName;
}

public String getDepartmentId() {
return departmentId;
}

public void setDepartmentId(String departmentId) {
this.departmentId = departmentId;
}

public String getDepartmentCode() {
return departmentCode;
}

public void setDepartmentCode(String departmentCode) {
this.departmentCode = departmentCode;
}

public String getDepartmentName() {
return departmentName;
}

public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}

public String getMemo() {
return memo;
}

public void setMemo(String memo) {
this.memo = memo;
}

public long getUpdateTime() {
return updateTime;
}

public void setUpdateTime(long updateTime) {
this.updateTime = updateTime;
}

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

public void refreshUpdateTime() {
this.updateTime = System.currentTimeMillis();
}

public String getUserType() {
return userType;
}

public void setUserType(String userType) {
this.userType = userType;
}

public String getOtherInfo1() {
return otherInfo1;
}

public void setOtherInfo1(String otherInfo1) {
this.otherInfo1 = otherInfo1;
}

public String getOtherInfo2() {
return otherInfo2;
}

public void setOtherInfo2(String otherInfo2) {
this.otherInfo2 = otherInfo2;
}

public String getOtherInfo3() {
return otherInfo3;
}

public void setOtherInfo3(String otherInfo3) {
this.otherInfo3 = otherInfo3;
}

public String getOtherInfo4() {
return otherInfo4;
}

public void setOtherInfo4(String otherInfo4) {
this.otherInfo4 = otherInfo4;
}

public static long getSerialversionuid() {
return serialVersionUID;
}

}

0 0
原创粉丝点击