f9a5094285d9a07fb61a621ef3c8af2e97a40ff6.svn-base 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.HblxCasService;
  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. @Api(value = "山东单点登录", tags = "山东单点登录")
  24. @RestController
  25. @RequestMapping("/cas/hblx")
  26. public class HbLanXinCasController extends BaseController {
  27. private Logger logger = LoggerFactory.getLogger(getClass());
  28. @Value("${hblx.auth.baseurl}")
  29. private String hbLxBaseUrl;
  30. @Value("${hblx.auth.client_appid}")
  31. private String hbLxClientAppid;
  32. @Value("${hblx.auth.client_screct}")
  33. private String hbLxClientScrect;
  34. @Autowired
  35. HblxCasService hblxCasService;
  36. @RequestMapping(value = "/validateTicketKey1", method = RequestMethod.GET)
  37. public BaseResponse<Object> code(@RequestParam(value = "code", required = false) String code,HttpServletRequest request) throws Exception {
  38. //获取应用访问token
  39. Map<String, String> headerParams = new HashMap<>();
  40. headerParams.put("", "");
  41. Map<String, String> params = new HashMap<>();
  42. params.put("grant_type", "client_credential");
  43. params.put("appid", hbLxClientAppid);
  44. params.put("secret", hbLxClientScrect);
  45. String ret = HttpClientUtils.simpleGetInvoke(hbLxBaseUrl + "/v1/apptoken/create", params);
  46. logger.info("sessionId ret-------------------------" + ret + "-------------------------");
  47. String appToken = JSONObject.fromObject(JSONObject.fromObject(ret).get("data")).get("appToken").toString();
  48. //获取人员访问token
  49. Map<String, String> headerParams_1 = new HashMap<>();
  50. headerParams_1.put("", "");
  51. Map<String, String> params_1 = new HashMap<>();
  52. params_1.put("app_token", appToken);
  53. params_1.put("grant_type", "authorization_code");
  54. params_1.put("code", code);
  55. String ret1 = HttpClientUtils.simpleGetInvoke(hbLxBaseUrl + "/v1/usertoken/create", params_1);
  56. logger.info("sessionId ret-------------------------" + ret1 + "-------------------------");
  57. String userToken = JSONObject.fromObject(JSONObject.fromObject(ret1).get("data")).get("userToken").toString();
  58. String staffId = JSONObject.fromObject(JSONObject.fromObject(ret1).get("data")).get("staffId").toString();
  59. //根据token获取用户信息
  60. Map<String, String> tokenParam = new HashMap<>();
  61. tokenParam.put("app_token", appToken);
  62. String user = HttpClientUtils.simpleGetInvoke(hbLxBaseUrl + "/v1/staffs/"+staffId+"/infor/fetch", tokenParam);
  63. logger.info("-----------------------------get user " + user + "-----------------------------");
  64. String userInfo = JSONObject.fromObject(user).get("data").toString();
  65. Map<String, Object> ssoUserMap = jsonToMap(userInfo);
  66. logger.info("---------------sso login-------------------------------");
  67. if (ssoUserMap != null) {
  68. logger.info("---------------sso user not null -------------------------------");
  69. ssoUserMap.put("staffId",staffId);
  70. BisInspAllRlationPers bisInspAllRlationPers = hblxCasService.validateUser(ssoUserMap);
  71. if (bisInspAllRlationPers == null) {
  72. return buildFailResponse(1001, "登陆名或密码错误", "", "");
  73. } else {
  74. logger.info("---------------sso user null -------------------------------");
  75. String uuid = UuidUtil.uuid();
  76. bisInspAllRlationPers = hblxCasService.validateTicket(bisInspAllRlationPers, uuid, request);
  77. logger.info("---------------sso add -------------------------------");
  78. return buildSuccessResponse(bisInspAllRlationPers, uuid);
  79. }
  80. } else {
  81. logger.info("---------------sso no user -------------------------------");
  82. return buildFailResponse("无法获取用户信息");
  83. }
  84. }
  85. public Map<String, Object> jsonToMap(String json) {
  86. Map<String, Object> map = new HashMap<>();
  87. map = JSON.parseObject(JSON.parse(json).toString(), HashMap.class);
  88. return map;
  89. }
  90. }