第三方系统单点登陆NC Cloud
1 第三方单点登录NC Cloud概览
第三方单点登录的流程:
- 客户端向服务端发起登陆NC Cloud请求。
- 向NC Cloud服务器注册校验用户登陆和上下文信息。
- 如果校验成功NC Cloud服务器响应给客户服务器token信息
- 客户服务器将token响应给客户端
- 客户端收到token后拼接请求NC Cloud节点的url信息。
- NC Cloud收到请求并验证token是否合法,如果合法,则重定向到客户端请求的节点页面
详细流程图见下图:

2 第三方系统单点登录NC Cloud详细流程
2.1 在NC Cloud的系统注册表sm_oauth_security注册第三方系统
表中各字段含义如下:
- CLIENT_ID :第三方系统编码,必需项
- CLIENT_NAME: 第三方系统名称,非必需
- CLIENT_SECURITY: 第三方系统和NC Cloud共同维护的秘钥对
- PK_OAUTH_SECURITY: 主键,
- CLIENT_URL: 第三方系统的url (供NC Cloud集成访问第三方系统使用,本环节不需要)
- CLIENT_AUTHCLASS: 登录第三方系统授权类 (供NC Cloud集成访问第三方系统使用,本环节不需要)
- PK_GROUP:非本环节使用,设置为空
2.2 获取登录token
通过秘钥对获取登录token,第三方服务访问http://ip:port/service/genThirdPartyAccessToken 同时访问参数为:
- ype 获取方式,此方式为固定为 type_security且必需项
- dsname 数据源名称,必需项
- usercode NC Cloud用户编码,必需项
- langcode 多语语种,非必需项,为空默认中文语种
- busicentercode 账套编码,非必需项,为空登录时会循环在数据源中查找用户
- ts ,第三方系统加签的时间,用来生成校验秘钥对,必需项
- client_id第三方系统id,即第一步在sm_oauth_security注册的系统编码,必需项
- security第三方由共同维护的秘钥对根据给定的算法自己生成的秘钥值,必需项。 security是通过SHA256加签,其中是根据usercode,ts,client_security和当前系统时间动态拼接的字符串生成
注:8中的client_security为第三方密钥,当第三方不需要该密钥时,与1.1.1第三方系统注册中CLIENT_SECURITY参数保持一致。
代码如下:
1 String CLIENTID = "NCCceshi"; 2 3 String CLIENTSECURITY = "123qwe"; 4 5 //系统编码 6 String appCode = "CA_SYS"; 7 8 //当前待办需要打开的环境Url(即用友NC服务器)http://ip:port/ 9 String signUrl = http://127.0.0.1:8009/; 10 11 //当前系统使用的账套 12 String accCode = "NCC"; 13 14 //当前系统帐套编码关联的数据源 15 String ds = "desgin"; 16 17 //用户编码 18 String userCode = "test" 19 20 //获取用户单点凭证 21 String nccToken = HttpRequestUtil.getNCCToken(signUrl, ds, userCode, CLIENTID, CLIENTSECURITY, accCode); 22 23 String redirectUrl = signUrl + "/nccloud/resources/uap/rbac/thirdpartylogin/main/index.html?accesstoken=" + nccToken + "&redirect_uri=" + signUrl + "/nccloud/resources/workbench/public/common/main/index.html#/"; 24 25 // 跳转到主页桌面 26 resp.sendRedirect(redirectUrl);
token获取
1 public class HttpRequestUtil { 2 3 public static String getNCCToken(String url, String dsname, String usercode, String nccClient_id, String nccClient_secret, String accCode) throws Exception { 4 OutputStream os = null; 5 DataOutputStream dos = null; 6 InputStream is = null; 7 8 String returnFlag = ""; 9 10 try { 11 URL preUrl = new URL(url + "/service/genThirdPartyAccessToken"); 12 13 URLConnection uc = preUrl.openConnection(); 14 15 uc.setDoOutput(true); 16 17 uc.setUseCaches(false); 18 19 uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 20 21 uc.setRequestProperty("Content-Length", "10000" ); 22 23 uc.setRequestProperty("userid", usercode); 24 25 HttpURLConnection hc = (HttpURLConnection) uc; 26 27 hc.setRequestMethod("POST"); 28 29 StringBuffer sb = new StringBuffer(); 30 31 String security = genKey(usercode,usercode + nccClient_secret + (System.currentTimeMillis() + "").substring(0, 6)); 32 33 sb.append("type=type_security&dsname="+dsname+"&usercode="+usercode+"&client_id="+nccClient_id+"&security="+security+"&busicentercode="+accCode); 34 35 os = hc.getOutputStream(); 36 37 dos = new DataOutputStream(os); 38 39 dos.writeBytes(sb.toString()); 40 41 dos.flush(); 42 43 is = hc.getInputStream(); 44 45 int ch; 46 47 while ((ch = is.read()) != -1) { 48 returnFlag += String.valueOf((char) ch); 49 } 50 51 if(returnFlag == null || returnFlag.trim().length() == 0){ 52 throw new BusinessException("获取NCC Token失败,请检查"); 53 } 54 55 } catch (Exception e) { 56 e.printStackTrace(); 57 throw e; 58 } finally { 59 if (dos != null) { 60 try { 61 dos.close(); 62 } catch (Exception e2) { 63 } 64 } 65 if (os != null) { 66 try { 67 os.close(); 68 } catch (Exception e2) { 69 } 70 } 71 if (is != null) 72 try { 73 is.close(); 74 } catch (Exception e2) { 75 } 76 } 77 return returnFlag; 78 } 79 80 private static String genKey(String userid,String key) throws Exception{ 81 return new Base64().encodeToString(SignatureTookKit.digestSign(userid.getBytes(), key.getBytes())); 82 } 83 }
3 信任IP添加
在home\ierp\sf\nccssoConfig.xml中添加
1 <?xml version="1.0" encoding="UTF-8"?> 2 <NCCSSOConfigVO> 3 <authenticator id="default" classname="ncc.sso.bs.DefaultNCCSSOAuthenticator"> 4 <regTimeOut>200</regTimeOut> 5 <listParam key="IPAddress"> 6 <string>127.0.0.1</string> 7 </listParam> 8 </authenticator> 9 <authenticator id="type" classname="nc.sso.bs.AAA"> 10 <regTimeOut>200</regTimeOut> 11 <listParam key="IPAddress"> 12 <string>127.0.0.1</string> 13 </listParam> 14 </authenticator> 15 </NCCSSOConfigVO>
NCC2005产品本身单点登录有点问题,需要对应补丁包,否则可能会出现token获取成功,重定向后页面白板
有问题可以互相学习讨论
