| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- package com.xunmei.auth.service;
- import cn.hutool.core.util.ObjectUtil;
- import cn.hutool.http.HttpRequest;
- import cn.hutool.http.HttpUtil;
- import cn.hutool.json.JSONObject;
- import com.union.api.TUnionTransInfo;
- import com.union.api.UnionEsscAPI;
- import com.xunmei.auth.form.LoginBody;
- import com.xunmei.common.core.utils.StringUtils;
- import io.netty.util.internal.StringUtil;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import java.text.MessageFormat;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- /**
- * #统一运营门户登录信息
- * fjnx:
- * # 统一运营门户ip
- * host: 192.111.60.143
- * # 统一运营门户端口
- * port: 9191
- * # 统一运营门户定义的系统编码
- * systemCode: 61303
- * # 密管系统ip
- * passIp: 192.111.7.75
- * # 密管系统端口
- * passPort: 40105
- * # 密管系统对应标识
- * appCode: MSP
- * # 密管系统 秘钥
- * secretName: msp.uop-SM4.zek
- * # 使用token获取用户信息,get方式
- * webInfoUrl: http://{0}:{1}/yusp-group/api/session?systemCode={2}
- * # 用户登录接口 方式post
- * userLoginUrl: http://{0}:{1}/yusp-group/api/login?systemCode={2}
- * # 移动运营门户参数
- * fjnxApp:
- * # 移动运营平台地址
- * host: 192.111.37.181
- * #根据token获取移动运营平台账号信息 post方式
- * accountUrl: https://{0}/uaa/api/v1.0/user/account?access_token={1}
- * # 根据账号获取人员信息 post方式
- * userInfoUrl: https://{0}/sap/api/v1.0/user/user/find/{1}?access_token={2}
- */
- @Component
- public class FJNXLoginService{
- /**
- * 统一运营平台 服务ip地址
- */
- @Value("${fjnx.host}")
- private String host;
- /**
- * 统一运营平台 服务端口
- */
- @Value("${fjnx.port}")
- private String port;
- /**
- * 统一运营门户 给我们系统定义的 系统编号
- */
- @Value("${fjnx.systemCode}")
- private String systemCode;
- /**
- * 密管系统ip
- */
- @Value("${fjnx.passIp}")
- private String passIp;
- /**
- * 密管系统端口
- */
- @Value("${fjnx.passPort}")
- private Integer passPort;
- /**
- * 密管系统 对应标识
- */
- @Value("${fjnx.appCode}")
- private String appCode;
- /**
- * 密管系统 秘钥 msp.uop-SM4.zek
- */
- @Value("${fjnx.secretName}")
- private String secretName;
- /**
- * 统一运营门户 根据token获取用户信息
- * http://{0}:{1}/yusp-group/api/session?systemCode={2}
- */
- @Value("${fjnx.webInfoUrl}")
- private String webInfoUrl;
- /**
- * 统一运营门户 用户密码登录接口
- * userInfoUrl: http://{0}:{1}/yusp-group/api/login?systemCode={2}
- */
- @Value("${fjnx.userLoginUrl}")
- private String userLoginUrl;
- /**
- * 移动运营平台地址
- */
- @Value("${fjnxApp.host}")
- private String appHost;
- /**
- * 根据token获取移动运营平台账号信息 post方式
- * https://{0}/uaa/api/v1.0/user/account?access_token={1}
- */
- @Value("${fjnxApp.accountUrl}")
- private String accountUrl;
- /**
- * 根据账号获取人员信息 post方式
- * https://{0}/sap/api/v1.0/user/user/find/{1}?access_token={2}
- */
- @Value("${fjnxApp.userInfoUrl}")
- private String userInfoUrl;
- private static final Logger logger = LoggerFactory.getLogger(FJNXLoginService.class);
- /**
- * token在统一运平台验证token,获取用户信息后返回当前登录名
- * @param token
- * @return
- */
- public String getUserByWebToken(String token){
- String userName = null;
- String userInfoUrl = MessageFormat.format(webInfoUrl,host,port,systemCode);
- String result = HttpRequest.get(userInfoUrl)
- .header("Authorization","Bearer " + token)
- .header("Content-Type","application/json")
- .execute().body();
- logger.info("web端token获取用户信息结果:{}",result);
- if(StringUtils.isEmpty(result)){
- throw new RuntimeException("token验证失败");
- }
- JSONObject jsonObject = new JSONObject(result);
- String code = jsonObject.getStr("code");
- if(StringUtils.isNotEmpty(code) && "0".equals(code)){
- String data = jsonObject.getStr("data");
- JSONObject jsonData = new JSONObject(data);
- String userStatus = jsonData.getStr("userStatus");
- String loginName = jsonData.getStr("loginName");
- if(! "0".equals(userStatus)){
- userName = loginName;
- }
- }
- return userName;
- }
- /**
- * H5单点登录,去移动运营平台验证token
- * 1、获取当前登录账号信息
- * 2、根据账号信息获取用户信息
- * @param token
- * @return
- */
- public String getUserByAppToken(String token){
- String account_url = MessageFormat.format(accountUrl,appCode,token);
- String accountInfo = HttpUtil.post(account_url,new HashMap<>());
- if(StringUtils.isEmpty(accountInfo)){
- throw new RuntimeException("验证token失败,登录失败!");
- }
- JSONObject accountJson = new JSONObject(accountInfo);
- if("true".equals(accountJson.getStr("result"))){
- throw new RuntimeException("验证token失败,登录失败!");
- }
- JSONObject accountContent = new JSONObject(accountJson.getStr("data"));
- String account = accountContent.getStr("username");
- /*
- 开始获取登录账号信息
- */
- String user_info_url = MessageFormat.format(userInfoUrl,appHost,account,token);
- String userInfo = HttpUtil.post(user_info_url,new HashMap<>());
- JSONObject userJson = new JSONObject(userInfo);
- if(!"true".equals(userJson.getStr("result"))){
- throw new RuntimeException("远程获取用户信息失败,登录失败!");
- }
- JSONObject userContent = new JSONObject(userJson.getStr("data"));
- String sap_user = userContent.getStr("sap_user");
- JSONObject info = new JSONObject(userJson.getStr("sap_user"));
- String username = info.getStr("username");
- return username;
- }
- /**
- * 通过密管系统加密密码,然后登录到统一运营平台
- * @param login
- * @return
- */
- public String webLoginByPass(LoginBody login){
- /*
- * 加密前参数准备
- */
- List<String> ipList = new ArrayList<>();
- List<Integer> portList = new ArrayList<>();
- ipList.add(passIp);
- portList.add(passPort);
- //对登录用户的密码进行加密
- UnionEsscAPI shortApi = new UnionEsscAPI(ipList,portList,5,appCode,appCode,"");
- String pass = StringUtil.toHexString(login.getPassword().getBytes());
- TUnionTransInfo transInfo = shortApi.unionAPIServiceE160(1,secretName,"",0,1,pass,"",1);
- if(ObjectUtil.isNull(transInfo)){
- return null;
- }
- int isSuccess = transInfo.getIsSuccess();
- if(ObjectUtil.notEqual(transInfo.getResponseCode(),0) || ObjectUtil.notEqual(isSuccess,1)){
- logger.error("密码加密失败!{}",transInfo.getResponseRemark());
- return null;
- }
- if(ObjectUtil.isNull(transInfo.getReturnBody())){
- logger.error("密码加密失败,数据为空");
- return null;
- }
- //开始验证用户名密码
- String loginUrl = MessageFormat.format(userLoginUrl, host, port, systemCode);
- JSONObject object = new JSONObject();
- object.accumulate("loginName",login.getUsername());
- object.accumulate("pass",transInfo.getReturnBody().getData());
- object.accumulate("type",1);
- object.accumulate("systemCode",1);
- object.accumulate("passwordType",1);
- String loginContent = HttpRequest.get(loginUrl)
- .header("Content-Type","application/json")
- .contentType("application/json")
- .charset("utf-8")
- .body(object.toString())
- .execute().body();
- logger.info("登录后信息:{}",loginContent);
- JSONObject jsonObject = new JSONObject(loginContent);
- String code = jsonObject.getStr("code");
- if(StringUtils.isNotEmpty(code) && "0".equals(code)){
- JSONObject jsonData = new JSONObject(jsonObject.getStr("data"));
- String tokenThird = jsonData.getStr("token");
- logger.info("登录后的token:{}",tokenThird);
- return tokenThird;
- }
- logger.error("登录失败,{}",jsonObject.getStr("message"));
- return null;
- }
- }
|