Oauth2Api.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package com.zhiqiyun.open.router.apis;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.dliyun.oap.framework.annotation.ServiceMethod;
  5. import com.dliyun.oap.framework.annotation.ServiceMethodBean;
  6. import com.dliyun.oap.framework.response.OapResponse;
  7. import com.zhiqiyun.open.core.service.SystemConfigService;
  8. import com.zhiqiyun.open.router.request.oauth2.*;
  9. import lombok.extern.slf4j.Slf4j;
  10. import okhttp3.*;
  11. import org.apache.commons.codec.digest.DigestUtils;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import java.io.IOException;
  15. import java.util.Arrays;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import java.util.Objects;
  19. @Slf4j
  20. @ServiceMethodBean
  21. public class Oauth2Api {
  22. @Autowired
  23. private OkHttpClient okHttpClient;
  24. @Autowired
  25. private SystemConfigService systemConfigService;
  26. /**
  27. * 调试成功
  28. *
  29. * @param request
  30. * @return
  31. * @throws IOException
  32. */
  33. @ServiceMethod(method = "oauth2.login", title = "用户密码登录")
  34. public OapResponse login(Oauth2LoginRequest request) throws IOException {
  35. String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
  36. String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
  37. Map<String, String> paramValues = new HashMap<>();
  38. paramValues.put("grant_type", "password");
  39. paramValues.put("scope", "all");
  40. paramValues.put("username", request.getUserName());
  41. paramValues.put("password", DigestUtils.md5Hex(request.getPassword()));
  42. paramValues.put("login_type", "person");
  43. paramValues.put("type", "account");
  44. FormBody.Builder formBuilder = new FormBody.Builder();
  45. paramValues.forEach(formBuilder::add);
  46. Request.Builder builder = new Request.Builder();
  47. builder.url(String.format("%s/blade-auth/oauth/token", hostAddress));
  48. builder.header("Authorization", "Basic " + clientSecret);
  49. builder.header("Content-Type", "application/x-www-form-urlencoded");
  50. builder.post(formBuilder.build());
  51. Response resp = this.okHttpClient.newCall(builder.build()).execute();
  52. if (resp.isSuccessful()) {
  53. String result = Objects.requireNonNull(resp.body()).string();
  54. JSONObject jsonObject = JSON.parseObject(result);
  55. return OapResponse.success().setBody(jsonObject);
  56. } else {
  57. try {
  58. String result = Objects.requireNonNull(resp.body()).string();
  59. log.error(result);
  60. JSONObject jsonObject = JSON.parseObject(result);
  61. return OapResponse.fail(jsonObject.getString("error"), jsonObject.getString("error_description"));
  62. } catch (Exception e) {
  63. log.error("", e);
  64. return OapResponse.fail("NETWORK_ERROR", "网络异常" + resp.message());
  65. }
  66. }
  67. }
  68. /**
  69. * 调试成功
  70. *
  71. * @param request
  72. * @return
  73. * @throws IOException
  74. */
  75. @ServiceMethod(method = "oauth2.account.info", title = "获取用户信息")
  76. public OapResponse userInfo(Oauth2UserInfoRequest request) throws IOException {
  77. String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
  78. String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
  79. Request.Builder builder = new Request.Builder();
  80. builder.url(String.format("%s/blade-user/account/info", hostAddress));
  81. builder.header("Authorization", "Basic " + clientSecret);
  82. builder.header("Blade-Auth", "bearer " + request.getAccessToken());
  83. builder.header("Content-Type", "application/x-www-form-urlencoded");
  84. return this.getResponse(builder);
  85. }
  86. @ServiceMethod(method = "oauth2.validate.token", title = "验证登录状态")
  87. public OapResponse validateToken(Oauth2ValidateTokenRequest request) throws IOException {
  88. String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
  89. String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
  90. RequestBody requestBody = RequestBody.create("{}", MediaType.parse("application/json; charset=utf-8"));
  91. Request.Builder builder = new Request.Builder();
  92. builder.url(String.format("%s/blade-auth/validate/token", hostAddress));
  93. builder.header("Authorization", "Basic " + clientSecret);
  94. builder.header("Blade-Auth", "bearer " + request.getAccessToken());
  95. builder.header("Content-Type", "application/json");
  96. builder.post(requestBody);
  97. return this.getResponse(builder);
  98. }
  99. @ServiceMethod(method = "oauth2.wechat.login", title = "微信授权登录")
  100. public OapResponse oauthWechatLogin(Oauth2WechatMpLoginRequest request) throws IOException {
  101. String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
  102. String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
  103. Request.Builder builder = new Request.Builder();
  104. HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse(String.format("%s/blade-auth/oauth/auth-login/%s", hostAddress, request.getSource()))).newBuilder();
  105. urlBuilder.addQueryParameter("uuid", request.getUuid());
  106. urlBuilder.addQueryParameter("name", request.getName());
  107. urlBuilder.addQueryParameter("realName", request.getRealName());
  108. if (request.getSex() != null) {
  109. urlBuilder.addQueryParameter("sex", request.getSex().toString());
  110. }
  111. urlBuilder.addQueryParameter("email", request.getEmail());
  112. urlBuilder.addQueryParameter("phone", request.getPhone());
  113. log.info(urlBuilder.build().toString());
  114. builder.url(urlBuilder.build());
  115. builder.header("Authorization", "Basic " + clientSecret);
  116. builder.header("Content-Type", "application/x-www-form-urlencode");
  117. builder.get();
  118. return this.getResponse(builder);
  119. }
  120. // @ServiceMethod(method = "oauth2.wechat.mp.bind", title = "微信公众号账号绑定")
  121. // public OapResponse oauthWechatBind(Oauth2WechatMpBindRequest request) throws IOException {
  122. //
  123. // String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
  124. // String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
  125. //
  126. // Map<String, String> paramValues = new HashMap<>();
  127. // paramValues.put("uuid", request.getUuid());
  128. // paramValues.put("username", request.getUsername());
  129. // paramValues.put("password", request.getPassword());
  130. //
  131. // RequestBody body = RequestBody.create(JSON.toJSONString(paramValues), MediaType.parse("application/json; charset=utf-8"));
  132. //
  133. // Request.Builder builder = new Request.Builder();
  134. // builder.url(String.format("%s/blade-auth/oauth/bind/%s", hostAddress, request.getSource()));
  135. // builder.header("Authorization", "Basic " + clientSecret);
  136. // builder.header("Content-Type", "application/json");
  137. // builder.post(body);
  138. //
  139. // return this.getResponse(builder);
  140. // }
  141. //
  142. // @ServiceMethod(method = "oauth2.wechat.mp.register.bind", title = "微信公众号注册绑定")
  143. // public OapResponse oauthWechatRegisterBind(Oauth2WechatMpRegisterBindRequest request) throws IOException {
  144. //
  145. // String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
  146. // String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
  147. //
  148. // Map<String, String> paramValues = new HashMap<>();
  149. // paramValues.put("uuid", request.getUuid());
  150. // paramValues.put("username", request.getUsername());
  151. // paramValues.put("password", request.getPassword());
  152. //
  153. // RequestBody body = RequestBody.create(JSON.toJSONString(paramValues), MediaType.parse("application/json; charset=utf-8"));
  154. //
  155. // Request.Builder builder = new Request.Builder();
  156. // builder.url(String.format("%s/blade-auth/oauth/register/bind/%s", hostAddress, request.getSource()));
  157. // builder.header("Authorization", "Basic " + clientSecret);
  158. // builder.header("Content-Type", "application/json");
  159. // builder.post(body);
  160. //
  161. // return this.getResponse(builder);
  162. // }
  163. /**
  164. * 已经对接成功
  165. *
  166. * @param request
  167. * @return
  168. * @throws IOException
  169. */
  170. @ServiceMethod(method = "oauth2.register", title = "注册用户")
  171. public OapResponse oauthRegister(Oauth2RegisterRequest request) throws IOException {
  172. String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
  173. String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
  174. Map<String, String> paramValues = new HashMap<>();
  175. paramValues.put("account", request.getAccount());
  176. paramValues.put("password", request.getPassword());
  177. RequestBody requestBody = RequestBody.create(JSON.toJSONString(paramValues), MediaType.parse("application/json; charset=utf-8"));
  178. Request.Builder builder = new Request.Builder();
  179. builder.url(String.format("%s/blade-user/account/submit", hostAddress));
  180. builder.header("Authorization", "Basic " + clientSecret);
  181. builder.header("Content-Type", "application/json");
  182. builder.post(requestBody);
  183. return this.getResponse(builder);
  184. }
  185. /**
  186. * 已经对接成功
  187. *
  188. * @param request
  189. * @return
  190. * @throws IOException
  191. */
  192. @ServiceMethod(method = "oauth2.update.info", title = "更新用户信息")
  193. public OapResponse oauthUpdateInfo(Oauth2UpdateInfoRequest request) throws IOException {
  194. String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
  195. String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
  196. Map<String, String> paramValues = new HashMap<>();
  197. paramValues.put("name", request.getName());
  198. paramValues.put("realName", request.getRealName());
  199. if (request.getSex() != null) {
  200. paramValues.put("sex", request.getSex().toString());
  201. }
  202. if (StringUtils.isNotBlank(request.getEmail())) {
  203. paramValues.put("email", request.getEmail());
  204. }
  205. if (StringUtils.isNotBlank(request.getPhone())) {
  206. paramValues.put("phone", request.getPhone());
  207. }
  208. RequestBody requestBody = RequestBody.create(JSON.toJSONString(paramValues), MediaType.parse("application/json; charset=utf-8"));
  209. Request.Builder builder = new Request.Builder();
  210. builder.url(String.format("%s/blade-user/account/update-info", hostAddress));
  211. builder.header("Authorization", "Basic " + clientSecret);
  212. builder.header("Blade-Auth", "bearer " + request.getAccessToken());
  213. builder.header("Content-Type", "application/json");
  214. builder.post(requestBody);
  215. return this.getResponse(builder);
  216. }
  217. /**
  218. * 已经对接成功
  219. *
  220. * @param request
  221. * @return
  222. * @throws IOException
  223. */
  224. @ServiceMethod(method = "oauth2.update.password", title = "修改密码")
  225. public OapResponse oauthUpdatePassword(Oauth2UpdatePasswordRequest request) throws IOException {
  226. String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
  227. String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
  228. Map<String, String> paramValues = new HashMap<>();
  229. paramValues.put("oldPassword", DigestUtils.md5Hex(request.getOldPassword()));
  230. paramValues.put("newPassword", DigestUtils.md5Hex(request.getNewPassword()));
  231. FormBody.Builder formBuilder = new FormBody.Builder();
  232. paramValues.forEach(formBuilder::add);
  233. Request.Builder builder = new Request.Builder();
  234. builder.url(String.format("%s/blade-user/account/update-password", hostAddress));
  235. builder.header("Authorization", "Basic " + clientSecret);
  236. builder.header("Blade-Auth", "bearer " + request.getAccessToken());
  237. builder.header("Content-Type", "application/x-www-form-urlencode");
  238. builder.post(formBuilder.build());
  239. return this.getResponse(builder);
  240. }
  241. private OapResponse getResponse(Request.Builder builder) throws IOException {
  242. Response resp = this.okHttpClient.newCall(builder.build()).execute();
  243. if (resp.isSuccessful()) {
  244. String result = Objects.requireNonNull(resp.body()).string();
  245. log.info(result);
  246. JSONObject jsonObject = JSON.parseObject(result);
  247. String code = jsonObject.getString("code");
  248. String msg = jsonObject.getString("msg");
  249. JSONObject data = jsonObject.getJSONObject("data");
  250. if (StringUtils.equals("200", code)) {
  251. return OapResponse.success().setBody(data);
  252. }
  253. return OapResponse.fail("REMOTE_ERROR_" + code, msg);
  254. } else {
  255. String body = Objects.requireNonNull(resp.body()).string();
  256. log.info(body);
  257. if (Arrays.asList(401, 400).contains(resp.code())) {
  258. JSONObject object = JSON.parseObject(body);
  259. String msg = object.getString("msg");
  260. if (StringUtils.isBlank(msg)) {
  261. msg = object.getString("error_description");
  262. }
  263. return OapResponse.fail("ERROR", msg);
  264. } else {
  265. return OapResponse.fail("NETWORK_ERROR", resp.message());
  266. }
  267. }
  268. }
  269. }