⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ssomanager.java

📁 演示网站Portal单点登陆SSO的简单使用
💻 JAVA
字号:
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import sun.misc.BASE64Encoder;

/**
 * Title:SSOManager Description: Copyright: Copyright (c) 2007 Company: ebupt
 * 
 * @author guoshengxing
 * @version 1.0
 */
public class SSOManager {

	public static void main(String argv[]) {
		Calendar cal = Calendar.getInstance();
		String Time14 = new SimpleDateFormat("yyyyMMddHHmmSS").format(cal
				.getTime());
		System.out.println(Time14);
		/*
		 * try { InputStream is = new FileInputStream(
		 * "D:\\EclipsePr\\WapUtil\\SSO.xml"); SSOManager sa = new SSOManager();
		 * System.out.println(sa.buildResponseMsg(is)); } catch
		 * (FileNotFoundException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */
	}
}

class SSOPdu {

	public boolean unpack(String xmlString) {
		DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder dombuilder = domfac.newDocumentBuilder();
			Document doc = dombuilder.parse(new InputSource(new StringReader(
					xmlString)));
			parse(doc);
			return true;
		} catch (ParserConfigurationException e) {
			System.out.println(e);
		} catch (SAXException e) {
			System.out.println(e);
		} catch (IOException e) {
			System.out.println(e);
		}
		return false;
	}

	public boolean unpack(InputStream xmlStream) {
		DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder dombuilder = domfac.newDocumentBuilder();
			Document doc = dombuilder.parse(xmlStream);
			parse(doc);
			return true;
		} catch (ParserConfigurationException e) {
			System.out.println(e);
		} catch (SAXException e) {
			System.out.println(e);
		} catch (IOException e) {
			System.out.println(e);
		}
		return false;
	}

	public void parse(Document doc) {
		return;
	}

	public String pack() {
		return null;
	}
}

class SSOSessionReqPdu extends SSOPdu {
	private String SessionID = "";

	private String ISMPID = "";

	private String MSISDN = "";

	private String EchoURL = "";

	private String Timeout = "";

	private String TimeStamp = "";

	private String Authenticator = "";

	public String pack() {
		if (TimeStamp.equals("")) {
			Calendar cal = Calendar.getInstance();
			TimeStamp = new SimpleDateFormat("yyyyMMddHHmmSS").format(cal
					.getTime());
		}

		String AuthMd5 = MD5Util.MD5(SSOConstants.SHARE_KEY + "$" + SessionID
				+ "$" + ISMPID + "$" + MSISDN + "$" + EchoURL + "$" + Timeout
				+ "$" + TimeStamp);

		DesEncrypter encrypter = new DesEncrypter();
		String encryptedMSISDN = encrypter.base64DesEncrypt(MSISDN,
				SSOConstants.SHARE_KEY);

		Authenticator = new BASE64Encoder().encode(AuthMd5.getBytes());

		String value = "<?xml version = \"1.0\"?>\n"
				+ "<SSOMessage version=\"1.0\">\n\t" + "<SSOParas>\n\t\t"
				+ "<SessionID>" + SessionID + "</SessionID>\n\t\t" + "<ISMPID>"
				+ ISMPID + "</ISMPID>\n\t\t" + "<MSISDN>" + encryptedMSISDN
				+ "</MSISDN>\n\t\t" + "<EchoURL>" + EchoURL
				+ "</EchoURL>\n\t\t" + "<Timeout>" + Timeout
				+ "</Timeout>\n\t\t" + "<TimeStamp>" + TimeStamp
				+ "</TimeStamp>\n\t\t" + "<Authenticator>" + Authenticator
				+ "</Authenticator>\n\t" + "</SSOParas>\n" + "</SSOMessage>";
		return value;
	}

	public void parse(Document doc) {
		Element root = doc.getDocumentElement();
		NodeList SSOParas = root.getChildNodes();

		for (int i = 0; i < SSOParas.getLength(); i++) {
			Node SSOpara = SSOParas.item(i);
			if (SSOpara.getNodeType() == Node.ELEMENT_NODE) {
				for (Node node = SSOpara.getFirstChild(); node != null; node = node
						.getNextSibling()) {
					if (node.getNodeType() == Node.ELEMENT_NODE) {
						System.out.print("[" + node.getNodeName() + "]=");
						System.out.println("("
								+ node.getFirstChild().getNodeValue() + ")");
						if (node.getNodeName().equals("SessionID")) {
							SessionID = node.getFirstChild().getNodeValue();
							// System.out.println(SessionID);
						}
						if (node.getNodeName().equals("ISMPID")) {
							ISMPID = node.getFirstChild().getNodeValue();
							// System.out.println(ISMPID);
						}
						if (node.getNodeName().equals("MSISDN")) {
							String encryptedMSISDN = node.getFirstChild()
									.getNodeValue();
							DesEncrypter decrypter = new DesEncrypter();
							MSISDN = decrypter.base64DesDecrypt(
									encryptedMSISDN, SSOConstants.SHARE_KEY);
							// System.out.println(MSISDN);
						}
						if (node.getNodeName().equals("EchoURL")) {
							EchoURL = node.getFirstChild().getNodeValue();
							// System.out.println(EchoURL);
						}
						if (node.getNodeName().equals("Timeout")) {
							Timeout = node.getFirstChild().getNodeValue();
							// System.out.println(Timeout);
						}
						if (node.getNodeName().equals("TimeStamp")) {
							TimeStamp = node.getFirstChild().getNodeValue();
							// System.out.println(TimeStamp);
						}
						if (node.getNodeName().equals("Authenticator")) {
							Authenticator = node.getFirstChild().getNodeValue();
							// System.out.println(Authenticator);
						}
					}
				}
			}
		}
	}

	public String getAuthenticator() {
		return Authenticator;
	}

	public String getEchoURL() {
		return EchoURL;
	}

	public void setEchoURL(String echoURL) {
		EchoURL = echoURL;
	}

	public String getISMPID() {
		return ISMPID;
	}

	public void setISMPID(String ismpid) {
		ISMPID = ismpid;
	}

	public String getMSISDN() {
		return MSISDN;
	}

	public void setMSISDN(String msisdn) {
		MSISDN = msisdn;
	}

	public String getSessionID() {
		return SessionID;
	}

	public void setSessionID(String sessionID) {
		SessionID = sessionID;
	}

	public String getTimeout() {
		return Timeout;
	}

	public void setTimeout(String timeout) {
		Timeout = timeout;
	}

	public String getTimeStamp() {
		return TimeStamp;
	}

	public void setTimeStamp(String timeStamp) {
		TimeStamp = timeStamp;
	}
}

class SSOSessionRspPdu extends SSOPdu {

	private String ResultCode = "";

	private String Session = "";

	private String TimeStamp = "";

	private String Authenticator = "";

	public String pack() {
		if (TimeStamp.equals("")) {
			Calendar cal = Calendar.getInstance();
			TimeStamp = new SimpleDateFormat("yyyyMMddHHmmSS").format(cal
					.getTime());
		}

		String AuthMd5 = MD5Util.MD5(SSOConstants.SHARE_KEY + "$" + ResultCode
				+ "$" + TimeStamp);

		Authenticator = new BASE64Encoder().encode(AuthMd5.getBytes());

		String value = "<?xml version = \"1.0\"?>\n"
				+ "<SSOMessage version=\"1.0\">\n\t" + "<SSOParas>\n\t\t"
				+ "<ResultCode>" + ResultCode + "</ResultCode>\n\t\t"
				+ "<TimeStamp>" + TimeStamp + "</TimeStamp>\n\t\t"
				+ "<Session>" + Session + "</Session>\n\t\t"
				+ "<Authenticator>" + Authenticator + "</Authenticator>\n\t"
				+ "</SSOParas>\n" + "</SSOMessage>";
		return value;
	}

	public void parse(Document doc) {
		Element root = doc.getDocumentElement();
		NodeList SSOParas = root.getChildNodes();

		for (int i = 0; i < SSOParas.getLength(); i++) {
			Node SSOpara = SSOParas.item(i);
			if (SSOpara.getNodeType() == Node.ELEMENT_NODE) {
				for (Node node = SSOpara.getFirstChild(); node != null; node = node
						.getNextSibling()) {
					if (node.getNodeType() == Node.ELEMENT_NODE) {
						System.out.print("[" + node.getNodeName() + "]=");
						System.out.println("("
								+ node.getFirstChild().getNodeValue() + ")");
						if (node.getNodeName().equals("Session")) {
							Session = node.getFirstChild().getNodeValue();
							// System.out.println(SessionID);
						}
						if (node.getNodeName().equals("ResultCode")) {
							ResultCode = node.getFirstChild().getNodeValue();
							// System.out.println(ISMPID);
						}
						if (node.getNodeName().equals("TimeStamp")) {
							TimeStamp = node.getFirstChild().getNodeValue();
							// System.out.println(TimeStamp);
						}
						if (node.getNodeName().equals("Authenticator")) {
							Authenticator = node.getFirstChild().getNodeValue();
							// System.out.println(Authenticator);
						}
					}
				}
			}
		}
	}

	public String getAuthenticator() {
		return Authenticator;
	}

	public String getSession() {
		return Session;
	}

	public void setSession(String session) {
		Session = session;
	}

	public String getResultCode() {
		return ResultCode;
	}

	public void setResultCode(String resultcode) {
		ResultCode = resultcode;
	}

	public String getTimeStamp() {
		return TimeStamp;
	}

	public void setTimeStamp(String timeStamp) {
		TimeStamp = timeStamp;
	}
}

class SSOSessionKeepReqPdu extends SSOPdu {
	private String DeviceType = "";

	private String DeviceID = "";

	private String TimeStamp = "";

	private String Authenticator = "";

	public SSOSessionKeepReqPdu() {
	}

	public String pack() {
		if (TimeStamp.equals("")) {
			Calendar cal = Calendar.getInstance();
			TimeStamp = new SimpleDateFormat("yyyyMMddHHmmSS").format(cal
					.getTime());
		}

		String AuthMd5 = MD5Util.MD5(SSOConstants.SHARE_KEY + "$" + DeviceType
				+ "$" + DeviceID + "$" + TimeStamp);

		Authenticator = new BASE64Encoder().encode(AuthMd5.getBytes());

		String value = "<?xml version = \"1.0\"?>\n"
				+ "<SSOMessage version=\"1.0\">\n\t" + "<SSOParas>\n\t\t"
				+ "<DeviceType>" + DeviceType + "</DeviceType>\n\t\t"
				+ "<DeviceID>" + DeviceID + "</DeviceID>\n\t\t" + "<TimeStamp>"
				+ TimeStamp + "</TimeStamp>\n\t\t" + "<Authenticator>"
				+ Authenticator + "</Authenticator>\n\t" + "</SSOParas>\n"
				+ "</SSOMessage>";
		return value;
	}

	public void parse(Document doc) {
		Element root = doc.getDocumentElement();
		NodeList SSOParas = root.getChildNodes();

		for (int i = 0; i < SSOParas.getLength(); i++) {
			Node SSOpara = SSOParas.item(i);
			if (SSOpara.getNodeType() == Node.ELEMENT_NODE) {
				for (Node node = SSOpara.getFirstChild(); node != null; node = node
						.getNextSibling()) {
					if (node.getNodeType() == Node.ELEMENT_NODE) {
						System.out.print("[" + node.getNodeName() + "]=");
						System.out.println("("
								+ node.getFirstChild().getNodeValue() + ")");
						if (node.getNodeName().equals("DeviceType")) {
							DeviceType = node.getFirstChild().getNodeValue();
							// System.out.println(ISMPID);
						}
						if (node.getNodeName().equals("DeviceID")) {
							DeviceID = node.getFirstChild().getNodeValue();
							// System.out.println(ISMPID);
						}
						if (node.getNodeName().equals("TimeStamp")) {
							TimeStamp = node.getFirstChild().getNodeValue();
							// System.out.println(TimeStamp);
						}
						if (node.getNodeName().equals("Authenticator")) {
							Authenticator = node.getFirstChild().getNodeValue();
							// System.out.println(Authenticator);
						}
					}
				}
			}
		}
	}

	public String getAuthenticator() {
		return Authenticator;
	}

	public String getTimeStamp() {
		return TimeStamp;
	}

	public void setTimeStamp(String timeStamp) {
		TimeStamp = timeStamp;
	}

	public String getDeviceID() {
		return DeviceID;
	}

	public void setDeviceID(String deviceID) {
		DeviceID = deviceID;
	}

	public String getDeviceType() {
		return DeviceType;
	}

	public void setDeviceType(String deviceType) {
		DeviceType = deviceType;
	}
}

class SSOSessionKeepRspPdu extends SSOPdu {

	private String ResultCode = "";

	private String TimeStamp = "";

	private String Authenticator = "";

	public SSOSessionKeepRspPdu() {
	}

	public String pack() {
		if (TimeStamp.equals("")) {
			Calendar cal = Calendar.getInstance();
			TimeStamp = new SimpleDateFormat("yyyyMMddHHmmSS").format(cal
					.getTime());
		}

		String AuthMd5 = MD5Util.MD5(SSOConstants.SHARE_KEY + "$" + ResultCode
				+ "$" + TimeStamp);

		Authenticator = new BASE64Encoder().encode(AuthMd5.getBytes());

		String value = "<?xml version = \"1.0\"?>\n"
				+ "<SSOMessage version=\"1.0\">\n\t" + "<SSOParas>\n\t\t"
				+ "<ResultCode>" + ResultCode + "</ResultCode>\n\t\t"
				+ "<TimeStamp>" + TimeStamp + "</TimeStamp>\n\t\t"
				+ "<Authenticator>" + Authenticator + "</Authenticator>\n\t"
				+ "</SSOParas>\n" + "</SSOMessage>";
		return value;
	}

	public void parse(Document doc) {
		Element root = doc.getDocumentElement();
		NodeList SSOParas = root.getChildNodes();

		for (int i = 0; i < SSOParas.getLength(); i++) {
			Node SSOpara = SSOParas.item(i);
			if (SSOpara.getNodeType() == Node.ELEMENT_NODE) {
				for (Node node = SSOpara.getFirstChild(); node != null; node = node
						.getNextSibling()) {
					if (node.getNodeType() == Node.ELEMENT_NODE) {
						System.out.print("[" + node.getNodeName() + "]=");
						System.out.println("("
								+ node.getFirstChild().getNodeValue() + ")");
						if (node.getNodeName().equals("ResultCode")) {
							ResultCode = node.getFirstChild().getNodeValue();
							// System.out.println(ISMPID);
						}
						if (node.getNodeName().equals("TimeStamp")) {
							TimeStamp = node.getFirstChild().getNodeValue();
							// System.out.println(TimeStamp);
						}
						if (node.getNodeName().equals("Authenticator")) {
							Authenticator = node.getFirstChild().getNodeValue();
							// System.out.println(Authenticator);
						}
					}
				}
			}
		}
	}

	public String getAuthenticator() {
		return Authenticator;
	}

	public String getResultCode() {
		return ResultCode;
	}

	public void setResultCode(String resultcode) {
		ResultCode = resultcode;
	}

	public String getTimeStamp() {
		return TimeStamp;
	}

	public void setTimeStamp(String timeStamp) {
		TimeStamp = timeStamp;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -