先摘录一段单点登录的定义,来自百度百科。
单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一。SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。
单点登录的基本流程:用户发起单点登陆请求----->>通过请求信息获取用户编号usercode------>>利用usercode在系统中注册登录信息------->>利用注册的信息单点登录到NC系统。
以下类请根据具体项目需求开放和注释部分代码
一、登录请求处理类
/**
* NC单点登录Servlet类
*/
public class NCLoginServlet extends HttpServlet {
/** 字段描述 */
private static final long serialVersionUID = 1L;
// 传输协议
private String scheme = null;
// NC主机地址
private String serverName = null;
// NC主机端口
private int serverPort = 80;
// 传递过来的用户账号
private String usercode = null;
// 屏幕宽度
private String width = null;
// 屏幕高度
private String height = null;
public static String getFromBASE64(String s) {
if (s == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
}
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// 接收前台传递过来的信息
scheme = request.getScheme(); // 传输协议
serverName = request.getServerName(); // NC主机地址
serverPort = request.getServerPort(); // NC主机端口
width = request.getParameter("width"); // 屏幕宽度
height = request.getParameter("height");// 屏幕高度
NcWebTools ncweb = new NcWebTools();
// 取得传递过来的域用户信息,解密
//usercode = NcWebTools.SSOUnicodeToString(request.getParameter("ssoid"));
usercode = request.getParameter("ssoid");
//CA二次认证
// if(!ncweb.isVerifyPass(usercode)){
// String url = scheme + "://" + serverName + ":" + serverPort + "/ncsso/error/error106.jsp";
// response.sendRedirect(url); // 重定向至错误页面
// return;
// }
// 检查用户是否存在于NC系统中
String username = ncweb.isExistInNC(usercode);
if (username == null) {
String url = scheme + "://" + serverName + ":" + serverPort + "/error.jsp";
response.sendRedirect(url); // 重定向至错误页面
return;
}
// 检查用户是否已分配权限
// if (!ncweb.isExistRole(usercode)) {
// String url = scheme + "://" + serverName + ":" + serverPort + "/ncsso/error/error103.jsp";
// response.sendRedirect(url); // 重定向至错误页面
// return;
// }
// 重定向至NC主界面
sendToNC(response, ncweb);
} catch (Exception ex) {
ex.printStackTrace();
throw new ServletException(ex.getMessage());
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
/**
*
* <P>
* 重定向至NC主界面
* </P>
*
* @param response
* @throws Exception
*/
private void sendToNC(HttpServletResponse response, NcWebTools ncweb) throws Exception {
// 随机生成一个key
String randomKey = IDMaker.makeID(20);
// 组装注册请求url
String ssurl = scheme + "://" + serverName + ":" + serverPort + "/service/RegisterServlet?key=" + randomKey + "&language=simpchn" // 语言
+ "&usercode=" + usercode // 用户编码
+ "&width=" + width // 屏幕宽度
+ "&height=" + height; // 屏幕高度
// 获取登录公司
String pk_corp = ncweb.getCorpByUsercode(usercode);
if (pk_corp != null && !"".equals(pk_corp)) {
ssurl += "&pkcorp=" + pk_corp;
}
Logger.debug(ssurl);
ncweb.connect(ssurl); // 注册
// 拼装NC单点登录URL
String url = scheme + "://" + serverName + ":" + serverPort + "/login.jsp?key=" + randomKey + "&width=" + width // 屏幕宽度
+ "&height=" + height; // 屏幕高度;
// String url = "Uclient://start/"+scheme + "://" + serverName + ":" + serverPort+"/?ssoKey="+ randomKey/*+"&uiloader=nc.ui.sm.login.Loader3"*/;
// 重定向到指定的地址
// response.sendRedirect(url);
PrintWriter w = response.getWriter();
w.print(url);
//w.print("<html><head><script>window.location='"+url+"';</script></head><body></body></html>");
return;
}
}
二、各项辅助类
- IDMaker 用于生成注册信息(key)
/**
* key生成器
*/
public class IDMaker {
private final static String CODES = "0123456789abcdefghijklmnopqrstuvwxyz+";
private final static String DIGIT_CODES = "0123456789+";
private final static String CHAR_CODES = "abcdefghijklmnopqrstuvwxyz+";
// 组成ID基本元素的数量
public static final int ID_ELEMENT_COUNT = 36;
public static final int TYPE_ALL = 0;
public static final int TYPE_CHAR = 1;
public static final int TYPE_DIGIT = 2;
/**
* 返回长度为idLen的编码
*
* @return java.lang.String
* @param idLen
* int
*/
public final static String makeID(int idLen, int idType) {
if (idType == TYPE_CHAR) {
return makeCharID(idLen);
}
if (idType == TYPE_DIGIT) {
return makeDigitID(idLen);
}
return makeID(idLen);
}
/**
* 返回长度为idLen的编码
*
* @return java.lang.String
* @param idLen
* int
*/
private final static String makeDigitID(int idLen) {
if (idLen < 1 || idLen > 64) {
return null;
}
String id = null;
if (id == null)
id = "";
int n;
StringBuffer sb = new StringBuffer(id);
for (int i = 0; i < idLen; i++) {
n = (int) (java.lang.Math.random() * 10);
sb.append(DIGIT_CODES.substring(n, n + 1));
}
return sb.toString();
}
/**
* 返回长度为idLen的编码
*
* @return java.lang.String
* @param idLen
* int
*/
private final static String makeCharID(int idLen) {
if (idLen < 1 || idLen > 64) {
return null;
}
String id = null;
if (id == null)
id = "";
int n;
StringBuffer sb = new StringBuffer(id);
for (int i = 0; i < idLen; i++) {
n = (int)