第三方系統單點登陸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獲取成功,重定向后頁面白板
有問題可以互相學習討論