f715f3728babfe9d8bd15dc95c1f91f2bbdc21af.svn-base 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.SdCasService;
  6. import cn.com.goldenwater.dcproj.service.SdTCasService;
  7. import cn.com.goldenwater.dcproj.utils.HttpClientUtils;
  8. import cn.com.goldenwater.id.util.UuidUtil;
  9. import com.alibaba.fastjson.JSON;
  10. import io.swagger.annotations.Api;
  11. import net.sf.json.JSONObject;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Value;
  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.util.HashMap;
  22. import java.util.Map;
  23. import static cn.com.goldenwater.dcproj.utils.HttpClientUtils.CONTENT_CHARSET;
  24. @Api(value = "山东单点登录", tags = "山东单点登录")
  25. @RestController
  26. @RequestMapping("/cas/sdt")
  27. public class SdTCasController extends BaseController {
  28. private Logger logger = LoggerFactory.getLogger(getClass());
  29. @Value("${sdt.auth.baseurl}")
  30. private String sdtAuthBaseUrl;
  31. @Value("${sdt.auth.client_agentid}")
  32. private String sdtAuthClientAgentId;
  33. @Value("${sdt.auth.client_screct}")
  34. private String sdtAuthClientScrect;
  35. @Value("${sdt.auth.client_corpid}")
  36. private String sdtAuthClientCorpid;
  37. @Autowired
  38. SdTCasService sdtCasService;
  39. @RequestMapping(value = "/validateTicketKey1", method = RequestMethod.GET)
  40. public BaseResponse<Object> code(@RequestParam(value = "code", required = false) String code,HttpServletRequest request) throws Exception {
  41. //根据corpid获取token
  42. Map<String, String> headerParams = new HashMap<>();
  43. headerParams.put("", "");
  44. Map<String, String> params = new HashMap<>();
  45. params.put("corpid", sdtAuthClientCorpid);
  46. params.put("corpsecret", sdtAuthClientScrect);
  47. String ret = HttpClientUtils.simpleGetInvoke(sdtAuthBaseUrl + "/cgi-bin/gettoken", params);
  48. // String ret = "{\"errcode\":0, \"errmsg\":\"ok\", \"access_token\":\"accesstoken000001\", \"expires_in\":72006}";
  49. logger.info("sessionId ret-------------------------" + ret + "-------------------------");
  50. //字符串转json 获取token
  51. String accessToken = JSONObject.fromObject(ret).get("access_token").toString();
  52. // 根据accessToken、code调用 调用6.4 获取 用户编码
  53. //根据token获取用户信息
  54. Map<String, String> tokenParam = new HashMap<>();
  55. tokenParam.put("access_token", accessToken);
  56. tokenParam.put("code", code);
  57. String user = HttpClientUtils.simpleGetInvoke(sdtAuthBaseUrl + "/cgi-bin/user/getuserinfo", tokenParam);
  58. // String user = "{\"errcode\":0, \"errmsg\":\"ok\", \"UserId\":\"USERID\", \"DeviceId\":\"DEVICEID\", \"user_ticket\":\"USER_TICKET\", \"expires_in\":7200, \"usertype\":2}";
  59. // user 用户编码信息 解析
  60. logger.info("-----------------------------get sdt user " + user + "-----------------------------");
  61. String useriId = JSONObject.fromObject(user).get("UserId").toString();
  62. // String mobile = JSONObject.fromObject(JSONObject.fromObject(user).get("user").toString()).get("mobile").toString();
  63. logger.info("-----------------------------get sdt mobile " + useriId + "-----------------------------");
  64. // 根据用户编码 获取用户信息
  65. Map<String, String> userIdParam = new HashMap<>();
  66. userIdParam.put("access_token",accessToken);
  67. userIdParam.put("userid",useriId);
  68. // userIdParam.put("avatar_addr",avatarAddr);
  69. String userInfo = HttpClientUtils.simpleGetInvoke(sdtAuthBaseUrl + "/cgi-bin/user/get", userIdParam);
  70. // String userInfo = "{\"errcode\":0, \"errmsg\":\"ok\", \"userid\":\"lihaichao\", \"name\":\"李海超\", \"mobile\":\"13401079738\", \"hide_mobile\":0}";
  71. logger.info("-----------------------------get sdt userInfo " + userInfo + "-----------------------------");
  72. //
  73. //手机号默认登录,如果没有则增加用户设置督查权限。参数,有用户名,密码,手机号,默认角色
  74. Map<String, Object> ssoUserMap = jsonToMap(userInfo);
  75. logger.info("---------------sso login-------------------------------");
  76. if (ssoUserMap != null) {
  77. logger.info("---------------sso user not null -------------------------------");
  78. BisInspAllRlationPers bisInspAllRlationPers = sdtCasService.validateUser(ssoUserMap);
  79. if (bisInspAllRlationPers == null) {
  80. return buildFailResponse(1001, "登陆名或密码错误", "", "");
  81. } else {
  82. logger.info("---------------sso user null -------------------------------");
  83. String uuid = UuidUtil.uuid();
  84. bisInspAllRlationPers = sdtCasService.validateTicket(bisInspAllRlationPers, uuid, request);
  85. logger.info("---------------sso add -------------------------------");
  86. return buildSuccessResponse(bisInspAllRlationPers, uuid);
  87. }
  88. } else {
  89. logger.info("---------------sso no user -------------------------------");
  90. return buildFailResponse("无法获取用户信息");
  91. }
  92. }
  93. public Map<String, Object> jsonToMap(String json) {
  94. Map<String, Object> map = new HashMap<>();
  95. map = JSON.parseObject(JSON.parse(json).toString(), HashMap.class);
  96. return map;
  97. }
  98. }