OAuthController.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.bowintek.practice.controller;
  2. import com.alibaba.druid.support.logging.Log;
  3. import com.alibaba.druid.support.logging.LogFactory;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.bowintek.practice.AppConfig;
  6. import com.bowintek.practice.filter.exception.*;
  7. import com.bowintek.practice.model.SysUser;
  8. import com.bowintek.practice.services.service.AccountService;
  9. import com.bowintek.practice.services.service.UserService;
  10. import com.bowintek.practice.services.service.system.LogService;
  11. import com.bowintek.practice.util.*;
  12. import com.bowintek.practice.vo.user.UserModel;
  13. import com.fasterxml.jackson.databind.JsonNode;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.web.bind.annotation.GetMapping;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import java.net.URLDecoder;
  20. import java.net.URLEncoder;
  21. import java.time.LocalDate;
  22. import java.time.LocalDateTime;
  23. import java.util.ArrayList;
  24. import java.util.Date;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. @RestController
  28. @RequestMapping(value = "/api/oauth")
  29. @Slf4j
  30. public class OAuthController {
  31. @Autowired
  32. private AppConfig appConfig;
  33. @Autowired
  34. private RemoteHelper remoteHelper;
  35. @Autowired
  36. private JsonMapper jsonMapper;
  37. @Autowired
  38. private StringUtils stringUtils;
  39. /*@Autowired
  40. private StudentService studentService;*/
  41. @Autowired
  42. private AccountService accountService;
  43. @Autowired
  44. private UserService userService;
  45. @Autowired
  46. private DateUtils dateUtils;
  47. @Autowired
  48. private DesUtils desUtils;
  49. @Autowired
  50. private LogService logService;
  51. @GetMapping("/getOAuthUrl")
  52. public BaseResponse<String> getOAuthUrl(String appType, String oauthType) {
  53. String url = "";
  54. /* String serviceUrl = "web".equals(appType) ? appConfig.oauthconfig_webServiceUrl : appConfig.oauthconfig_mobileServiceUrl;
  55. if ("oauth".equals(oauthType)) {
  56. url = appConfig.oauthconfig_oauthUrl + "/auth/oauth/authorize?";
  57. url += "response_type=code";
  58. url += "&client_id=" + appConfig.oauthconfig_client_id;
  59. url += "&redirect_uri=" + serviceUrl;
  60. url += "&state=login";
  61. log.info("getOAuthUrl-oauth:" + url);
  62. } else {
  63. url = appConfig.oauthconfig_oauthUrl + "/auth/cas/login?";
  64. if ("web".equals(appType))
  65. url += "client_id=" + appConfig.oauthconfig_client_id;
  66. else
  67. url += "service=" + serviceUrl;
  68. log.info("getOAuthUrl-cas:" + "appType:" + appType + ",url:" + url);
  69. }*/
  70. return RespGenerstor.success(url);
  71. }
  72. @GetMapping("/getToken")
  73. public BaseResponse<String> getToken(String code) {
  74. JsonNode result;
  75. String reData;
  76. Map<String, String> getParams = new HashMap<>();
  77. getParams.put("client_id", appConfig.oauthconfig_client_id);
  78. getParams.put("client_secret", appConfig.oauthconfig_client_secret);
  79. getParams.put("code", code);
  80. // "{\"code\":1,\"data\":{\"access_token\":\"dd48df1557d39e1dc80f25285835a199ceb87aa9\",\"expires_in\":2592000,\"token_type\":\"bearer\",\"scope\":\"default,js_api\",\"refresh_token\":\"999bcbceba23582f6056e8dff1d3d8a04274cd5e\"},\"message\":\"操作成功\",\"timestamp\":1556107384}"
  81. reData = remoteHelper.post(getParams, appConfig.oauthconfig_oauthUrl + "/auth/oauth/access_token", "UTF-8");
  82. log.info("getToken:" + reData);
  83. result = jsonMapper.jsonToObject(reData, JsonNode.class);
  84. if (result != null && result.get("code") != null && "1".equals(result.get("code").toString()) && result.get("data") != null)
  85. return RespGenerstor.success(result.get("data").get("access_token"));
  86. else
  87. return RespGenerstor.success("");
  88. }
  89. @GetMapping("/oauthLogin")
  90. public BaseResponse<String> oauthLogin(String type, String token, String tm) {
  91. log.info("oauthLogin:ticket , tm " + tm + " , type " + type);
  92. String userCode = "";
  93. String reData;
  94. Map<String, String> getParams = new HashMap<>();
  95. getParams.put("Authorization", "Bearer ${token}");
  96. reData = remoteHelper.getJson( new HashMap<>(), appConfig.oauthconfig_oauthUrl + "/api/v2/sys/user/currentuser", "UTF-8", getParams);
  97. log.info("getUserInfo-cas:result " + reData);
  98. JSONObject jsonData = JSONObject.parseObject(reData);
  99. if (!"success".equals(jsonData.getString("code"))) {
  100. log.info("登录失败:" + jsonData.getString("msg"));
  101. return RespGenerstor.success(false);
  102. }
  103. userCode = jsonData.getJSONObject("data").getString("id");
  104. log.info("oauthLogin:token " + token + " , userCode " + userCode);
  105. if (stringUtils.IsNullOrEmpty(userCode))
  106. return RespGenerstor.success(false);
  107. SysUser sysUser = userService.getUserByLoginID(userCode);
  108. String userID = sysUser != null ? sysUser.getUserID() : null;
  109. if (stringUtils.IsNullOrEmpty(userID))
  110. return RespGenerstor.success(false);
  111. UserModel user = accountService.getUserByUserID(userID);
  112. user.token = TokenUtils.sign(user.getUserId() + '|' + user.getUserTypeId());
  113. user.dataRangeList =new ArrayList<>();
  114. user.permissionList = accountService.getUserPerms(user.getUserId());
  115. logService.save("登录", "", "单点登录", user.userId);
  116. return RespGenerstor.success(user);
  117. }
  118. }