FJNXLoginService.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package com.xunmei.auth.service;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import cn.hutool.http.HttpRequest;
  4. import cn.hutool.http.HttpUtil;
  5. import cn.hutool.json.JSONObject;
  6. import com.union.api.TUnionTransInfo;
  7. import com.union.api.UnionEsscAPI;
  8. import com.xunmei.auth.form.LoginBody;
  9. import com.xunmei.common.core.utils.StringUtils;
  10. import io.netty.util.internal.StringUtil;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.stereotype.Component;
  15. import java.text.MessageFormat;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. /**
  20. * #统一运营门户登录信息
  21. * fjnx:
  22. * # 统一运营门户ip
  23. * host: 192.111.60.143
  24. * # 统一运营门户端口
  25. * port: 9191
  26. * # 统一运营门户定义的系统编码
  27. * systemCode: 61303
  28. * # 密管系统ip
  29. * passIp: 192.111.7.75
  30. * # 密管系统端口
  31. * passPort: 40105
  32. * # 密管系统对应标识
  33. * appCode: MSP
  34. * # 密管系统 秘钥
  35. * secretName: msp.uop-SM4.zek
  36. * # 使用token获取用户信息,get方式
  37. * webInfoUrl: http://{0}:{1}/yusp-group/api/session?systemCode={2}
  38. * # 用户登录接口 方式post
  39. * userLoginUrl: http://{0}:{1}/yusp-group/api/login?systemCode={2}
  40. * # 移动运营门户参数
  41. * fjnxApp:
  42. * # 移动运营平台地址
  43. * host: 192.111.37.181
  44. * #根据token获取移动运营平台账号信息 post方式
  45. * accountUrl: https://{0}/uaa/api/v1.0/user/account?access_token={1}
  46. * # 根据账号获取人员信息 post方式
  47. * userInfoUrl: https://{0}/sap/api/v1.0/user/user/find/{1}?access_token={2}
  48. */
  49. @Component
  50. public class FJNXLoginService{
  51. /**
  52. * 统一运营平台 服务ip地址
  53. */
  54. @Value("${fjnx.host}")
  55. private String host;
  56. /**
  57. * 统一运营平台 服务端口
  58. */
  59. @Value("${fjnx.port}")
  60. private String port;
  61. /**
  62. * 统一运营门户 给我们系统定义的 系统编号
  63. */
  64. @Value("${fjnx.systemCode}")
  65. private String systemCode;
  66. /**
  67. * 密管系统ip
  68. */
  69. @Value("${fjnx.passIp}")
  70. private String passIp;
  71. /**
  72. * 密管系统端口
  73. */
  74. @Value("${fjnx.passPort}")
  75. private Integer passPort;
  76. /**
  77. * 密管系统 对应标识
  78. */
  79. @Value("${fjnx.appCode}")
  80. private String appCode;
  81. /**
  82. * 密管系统 秘钥 msp.uop-SM4.zek
  83. */
  84. @Value("${fjnx.secretName}")
  85. private String secretName;
  86. /**
  87. * 统一运营门户 根据token获取用户信息
  88. * http://{0}:{1}/yusp-group/api/session?systemCode={2}
  89. */
  90. @Value("${fjnx.webInfoUrl}")
  91. private String webInfoUrl;
  92. /**
  93. * 统一运营门户 用户密码登录接口
  94. * userInfoUrl: http://{0}:{1}/yusp-group/api/login?systemCode={2}
  95. */
  96. @Value("${fjnx.userLoginUrl}")
  97. private String userLoginUrl;
  98. /**
  99. * 移动运营平台地址
  100. */
  101. @Value("${fjnxApp.host}")
  102. private String appHost;
  103. /**
  104. * 根据token获取移动运营平台账号信息 post方式
  105. * https://{0}/uaa/api/v1.0/user/account?access_token={1}
  106. */
  107. @Value("${fjnxApp.accountUrl}")
  108. private String accountUrl;
  109. /**
  110. * 根据账号获取人员信息 post方式
  111. * https://{0}/sap/api/v1.0/user/user/find/{1}?access_token={2}
  112. */
  113. @Value("${fjnxApp.userInfoUrl}")
  114. private String userInfoUrl;
  115. private static final Logger logger = LoggerFactory.getLogger(FJNXLoginService.class);
  116. /**
  117. * token在统一运平台验证token,获取用户信息后返回当前登录名
  118. * @param token
  119. * @return
  120. */
  121. public String getUserByWebToken(String token){
  122. String userName = null;
  123. String userInfoUrl = MessageFormat.format(webInfoUrl,host,port,systemCode);
  124. String result = HttpRequest.get(userInfoUrl)
  125. .header("Authorization","Bearer " + token)
  126. .header("Content-Type","application/json")
  127. .execute().body();
  128. logger.info("web端token获取用户信息结果:{}",result);
  129. if(StringUtils.isEmpty(result)){
  130. throw new RuntimeException("token验证失败");
  131. }
  132. JSONObject jsonObject = new JSONObject(result);
  133. String code = jsonObject.getStr("code");
  134. if(StringUtils.isNotEmpty(code) && "0".equals(code)){
  135. String data = jsonObject.getStr("data");
  136. JSONObject jsonData = new JSONObject(data);
  137. String userStatus = jsonData.getStr("userStatus");
  138. String loginName = jsonData.getStr("loginName");
  139. if(! "0".equals(userStatus)){
  140. userName = loginName;
  141. }
  142. }
  143. return userName;
  144. }
  145. /**
  146. * H5单点登录,去移动运营平台验证token
  147. * 1、获取当前登录账号信息
  148. * 2、根据账号信息获取用户信息
  149. * @param token
  150. * @return
  151. */
  152. public String getUserByAppToken(String token){
  153. String account_url = MessageFormat.format(accountUrl,appCode,token);
  154. String accountInfo = HttpUtil.post(account_url,new HashMap<>());
  155. if(StringUtils.isEmpty(accountInfo)){
  156. throw new RuntimeException("验证token失败,登录失败!");
  157. }
  158. JSONObject accountJson = new JSONObject(accountInfo);
  159. if("true".equals(accountJson.getStr("result"))){
  160. throw new RuntimeException("验证token失败,登录失败!");
  161. }
  162. JSONObject accountContent = new JSONObject(accountJson.getStr("data"));
  163. String account = accountContent.getStr("username");
  164. /*
  165. 开始获取登录账号信息
  166. */
  167. String user_info_url = MessageFormat.format(userInfoUrl,appHost,account,token);
  168. String userInfo = HttpUtil.post(user_info_url,new HashMap<>());
  169. JSONObject userJson = new JSONObject(userInfo);
  170. if(!"true".equals(userJson.getStr("result"))){
  171. throw new RuntimeException("远程获取用户信息失败,登录失败!");
  172. }
  173. JSONObject userContent = new JSONObject(userJson.getStr("data"));
  174. String sap_user = userContent.getStr("sap_user");
  175. JSONObject info = new JSONObject(userJson.getStr("sap_user"));
  176. String username = info.getStr("username");
  177. return username;
  178. }
  179. /**
  180. * 通过密管系统加密密码,然后登录到统一运营平台
  181. * @param login
  182. * @return
  183. */
  184. public String webLoginByPass(LoginBody login){
  185. /*
  186. * 加密前参数准备
  187. */
  188. List<String> ipList = new ArrayList<>();
  189. List<Integer> portList = new ArrayList<>();
  190. ipList.add(passIp);
  191. portList.add(passPort);
  192. //对登录用户的密码进行加密
  193. UnionEsscAPI shortApi = new UnionEsscAPI(ipList,portList,5,appCode,appCode,"");
  194. String pass = StringUtil.toHexString(login.getPassword().getBytes());
  195. TUnionTransInfo transInfo = shortApi.unionAPIServiceE160(1,secretName,"",0,1,pass,"",1);
  196. if(ObjectUtil.isNull(transInfo)){
  197. return null;
  198. }
  199. int isSuccess = transInfo.getIsSuccess();
  200. if(ObjectUtil.notEqual(transInfo.getResponseCode(),0) || ObjectUtil.notEqual(isSuccess,1)){
  201. logger.error("密码加密失败!{}",transInfo.getResponseRemark());
  202. return null;
  203. }
  204. if(ObjectUtil.isNull(transInfo.getReturnBody())){
  205. logger.error("密码加密失败,数据为空");
  206. return null;
  207. }
  208. //开始验证用户名密码
  209. String loginUrl = MessageFormat.format(userLoginUrl, host, port, systemCode);
  210. JSONObject object = new JSONObject();
  211. object.accumulate("loginName",login.getUsername());
  212. object.accumulate("pass",transInfo.getReturnBody().getData());
  213. object.accumulate("type",1);
  214. object.accumulate("systemCode",1);
  215. object.accumulate("passwordType",1);
  216. String loginContent = HttpRequest.get(loginUrl)
  217. .header("Content-Type","application/json")
  218. .contentType("application/json")
  219. .charset("utf-8")
  220. .body(object.toString())
  221. .execute().body();
  222. logger.info("登录后信息:{}",loginContent);
  223. JSONObject jsonObject = new JSONObject(loginContent);
  224. String code = jsonObject.getStr("code");
  225. if(StringUtils.isNotEmpty(code) && "0".equals(code)){
  226. JSONObject jsonData = new JSONObject(jsonObject.getStr("data"));
  227. String tokenThird = jsonData.getStr("token");
  228. logger.info("登录后的token:{}",tokenThird);
  229. return tokenThird;
  230. }
  231. logger.error("登录失败,{}",jsonObject.getStr("message"));
  232. return null;
  233. }
  234. }