840e603f42ac512fdf7e93dcf09c67f008b98bab.svn-base 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package cn.com.goldenwater.dcproj.controller.sso;
  2. import cn.com.goldenwater.core.web.BaseController;
  3. import cn.com.goldenwater.core.web.BaseResponse;
  4. import cn.com.goldenwater.dcproj.model.BisInspAllRlationPers;
  5. import cn.com.goldenwater.dcproj.service.CdCasService;
  6. import cn.com.goldenwater.dcproj.utils.HttpClientUtils;
  7. import cn.com.goldenwater.dcproj.utils.StringUtils;
  8. import cn.com.goldenwater.id.util.UuidUtil;
  9. import com.alibaba.fastjson.JSONObject;
  10. import io.swagger.annotations.Api;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.util.DigestUtils;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestMethod;
  18. import org.springframework.web.bind.annotation.RequestParam;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import javax.servlet.http.HttpServletRequest;
  21. import java.io.UnsupportedEncodingException;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.regex.Pattern;
  25. /**
  26. * CdCasController
  27. * 成都市水务局 单点登录
  28. *
  29. * @author lxf
  30. * @version 1.0
  31. * @date 2022/12/08 18:26
  32. **/
  33. @Api(value = "成都单点登录", tags = "成都单点登录")
  34. @RestController
  35. @RequestMapping("/cas/cd")
  36. public class CdCasController extends BaseController {
  37. private static final Logger logger = LoggerFactory.getLogger(CdCasController.class);
  38. @Value("${cd.auth.baseurl}")
  39. private String cdAuthBaseUrl;
  40. @Value("${cd.auth.client_uuid}")
  41. private String cdAuthClientUuid;
  42. /**
  43. * 获取用户名 api
  44. */
  45. private static final String API_URL_GET_USERNAME = "/api/ps/token/getUserByToken";
  46. /**
  47. * 获取分部 api
  48. */
  49. private static final String API_URL_GET_SUBCOMPANY = "/api/hrm/resful/getHrmsubcompanyWithPage";
  50. /**
  51. * 获取部门 api
  52. */
  53. private static final String API_URL_GET_DEPARTMENT= "/api/hrm/resful/getHrmdepartmentWithPage";
  54. /**
  55. * 获取岗位 api
  56. */
  57. private static final String API_URL_GET_JOB= "/api/hrm/resful/getJobtitleInfoWithPage";
  58. /**
  59. * 获取人员信息 api
  60. */
  61. private static final String API_URL_GET_USERINFO= "/api/hrm/resful/getHrmUserInfoWithPage";
  62. @Autowired
  63. private CdCasService cdCasService;
  64. /**
  65. * 单点登录
  66. * @param ticket
  67. * @param request
  68. * @return
  69. * @throws Exception
  70. */
  71. @RequestMapping(value = "/validateTicketKey", method = RequestMethod.GET)
  72. public BaseResponse<Object> code(@RequestParam(value = "ticket", required = false) String ticket, HttpServletRequest request) {
  73. logger.debug("sso token ticket:"+ ticket);
  74. // request params
  75. Map<String, String> paramsMap = new HashMap<>(8);
  76. paramsMap.put("token", ticket);
  77. // 第一步 获取用户名
  78. logger.debug("1 start");
  79. String username = null;
  80. try {
  81. String httpGetResponse = HttpClientUtils.simpleGetInvoke(cdAuthBaseUrl.concat(API_URL_GET_USERNAME), paramsMap);
  82. logger.debug("sso username httpGetResponse-------------------------" + httpGetResponse + "-------------------------");
  83. if(null == httpGetResponse || "false".equals(httpGetResponse)){
  84. // 获取用户名 失败 说明登录凭证 无效
  85. logger.info("sso login false");
  86. return buildFailResponse("无法获取用户信息");
  87. }
  88. JSONObject jsonObjectResp = JSONObject.parseObject(httpGetResponse);
  89. Object obj = jsonObjectResp.get("username");
  90. if(null == obj || "" == String.valueOf(obj)){
  91. // 获取用户名 失败 说明登录凭证 无效
  92. logger.info("sso ticket error");
  93. return buildFailResponse("获取用户信息为空");
  94. }
  95. username = obj.toString();
  96. logger.debug("username-------------------------" + username + "-------------------------");
  97. // 真实姓名
  98. String realName = jsonObjectResp.getString("lastname");
  99. // 移动电话
  100. String phoneNoMobile = jsonObjectResp.getString("mobile");
  101. // 分部名称(处室)
  102. String subcompanyname = jsonObjectResp.getString("subcompanyname");
  103. // 部门名称
  104. String departmentname = jsonObjectResp.getString("departmentname");
  105. if( StringUtils.isNotEmpty(phoneNoMobile) && StringUtils.isNotEmpty(realName) && (Pattern.matches(REGEX_PHONE,username) || Pattern.matches(REGEX_PHONE,phoneNoMobile) ) ){
  106. Map<String,Object> ssoUserMap = new HashMap<>(8) ;
  107. // 登录名
  108. ssoUserMap.put("loginName",username);
  109. // 真实姓名
  110. ssoUserMap.put("userName",realName);
  111. if( Pattern.matches(REGEX_PHONE,username) ){
  112. // 登录名 格式为手机号码
  113. ssoUserMap.put("phone",username);
  114. }else{
  115. ssoUserMap.put("phone",phoneNoMobile);
  116. }
  117. // 分部名称(处室)
  118. ssoUserMap.put("subcompanyname",subcompanyname);
  119. // 部门名称
  120. ssoUserMap.put("departmentname",departmentname);
  121. logger.info("sso user validate");
  122. BisInspAllRlationPers bisInspAllRlationPers = cdCasService.validateUser(ssoUserMap);
  123. if (bisInspAllRlationPers == null) {
  124. return buildFailResponse(1001, "登陆名或密码错误", "", "");
  125. } else {
  126. logger.info("---------------sso user null -------------------------------");
  127. String uuid = UuidUtil.uuid();
  128. bisInspAllRlationPers = cdCasService.validateTicket(bisInspAllRlationPers, uuid, request);
  129. logger.info("---------------sso add -------------------------------");
  130. return buildSuccessResponse(bisInspAllRlationPers, uuid);
  131. }
  132. }else{
  133. return buildFailResponse("api获取用户信息为空");
  134. }
  135. } catch (Exception e) {
  136. return buildFailResponse("api获取用户信息失败");
  137. }
  138. }
  139. /**
  140. * 手机号 正则表达式
  141. */
  142. private static final String REGEX_PHONE = "^1[3|4|5|6|7|8|9][0-9]{9}$";
  143. /**
  144. *
  145. * @return
  146. */
  147. private Map<String,String> getRequestToken() {
  148. // 时间戳毫秒数
  149. long l = System.currentTimeMillis() ;
  150. String code = cdAuthClientUuid.concat(Long.toString(l)) ;
  151. try {
  152. String md5key = DigestUtils.md5DigestAsHex(code.getBytes("utf-8")).toUpperCase() ;
  153. //md5加密 然后转大写
  154. Map<String,String> map = new HashMap<>() ;
  155. map.put("key",md5key) ;
  156. map.put("ts",Long.toString(l)) ;
  157. return map;
  158. } catch (UnsupportedEncodingException e) {
  159. logger.error("生成请求token异常", e);
  160. }
  161. return null ;
  162. }
  163. }