| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- package com.zhiqiyun.open.router.apis;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.dliyun.oap.framework.annotation.ServiceMethod;
- import com.dliyun.oap.framework.annotation.ServiceMethodBean;
- import com.dliyun.oap.framework.response.OapResponse;
- import com.zhiqiyun.open.core.service.SystemConfigService;
- import com.zhiqiyun.open.router.request.oauth2.*;
- import lombok.extern.slf4j.Slf4j;
- import okhttp3.*;
- import org.apache.commons.codec.digest.DigestUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Objects;
- @Slf4j
- @ServiceMethodBean
- public class Oauth2Api {
- @Autowired
- private OkHttpClient okHttpClient;
- @Autowired
- private SystemConfigService systemConfigService;
- /**
- * 调试成功
- *
- * @param request
- * @return
- * @throws IOException
- */
- @ServiceMethod(method = "oauth2.login", title = "用户密码登录")
- public OapResponse login(Oauth2LoginRequest request) throws IOException {
- String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
- String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
- Map<String, String> paramValues = new HashMap<>();
- paramValues.put("grant_type", "password");
- paramValues.put("scope", "all");
- paramValues.put("username", request.getUserName());
- paramValues.put("password", DigestUtils.md5Hex(request.getPassword()));
- paramValues.put("login_type", "person");
- paramValues.put("type", "account");
- FormBody.Builder formBuilder = new FormBody.Builder();
- paramValues.forEach(formBuilder::add);
- Request.Builder builder = new Request.Builder();
- builder.url(String.format("%s/blade-auth/oauth/token", hostAddress));
- builder.header("Authorization", "Basic " + clientSecret);
- builder.header("Content-Type", "application/x-www-form-urlencoded");
- builder.post(formBuilder.build());
- Response resp = this.okHttpClient.newCall(builder.build()).execute();
- if (resp.isSuccessful()) {
- String result = Objects.requireNonNull(resp.body()).string();
- JSONObject jsonObject = JSON.parseObject(result);
- return OapResponse.success().setBody(jsonObject);
- } else {
- try {
- String result = Objects.requireNonNull(resp.body()).string();
- log.error(result);
- JSONObject jsonObject = JSON.parseObject(result);
- return OapResponse.fail(jsonObject.getString("error"), jsonObject.getString("error_description"));
- } catch (Exception e) {
- log.error("", e);
- return OapResponse.fail("NETWORK_ERROR", "网络异常" + resp.message());
- }
- }
- }
- /**
- * 调试成功
- *
- * @param request
- * @return
- * @throws IOException
- */
- @ServiceMethod(method = "oauth2.account.info", title = "获取用户信息")
- public OapResponse userInfo(Oauth2UserInfoRequest request) throws IOException {
- String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
- String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
- Request.Builder builder = new Request.Builder();
- builder.url(String.format("%s/blade-user/account/info", hostAddress));
- builder.header("Authorization", "Basic " + clientSecret);
- builder.header("Blade-Auth", "bearer " + request.getAccessToken());
- builder.header("Content-Type", "application/x-www-form-urlencoded");
- return this.getResponse(builder);
- }
- @ServiceMethod(method = "oauth2.validate.token", title = "验证登录状态")
- public OapResponse validateToken(Oauth2ValidateTokenRequest request) throws IOException {
- String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
- String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
- RequestBody requestBody = RequestBody.create("{}", MediaType.parse("application/json; charset=utf-8"));
- Request.Builder builder = new Request.Builder();
- builder.url(String.format("%s/blade-auth/validate/token", hostAddress));
- builder.header("Authorization", "Basic " + clientSecret);
- builder.header("Blade-Auth", "bearer " + request.getAccessToken());
- builder.header("Content-Type", "application/json");
- builder.post(requestBody);
- return this.getResponse(builder);
- }
- @ServiceMethod(method = "oauth2.wechat.login", title = "微信授权登录")
- public OapResponse oauthWechatLogin(Oauth2WechatMpLoginRequest request) throws IOException {
- String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
- String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
- Request.Builder builder = new Request.Builder();
- HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse(String.format("%s/blade-auth/oauth/auth-login/%s", hostAddress, request.getSource()))).newBuilder();
- urlBuilder.addQueryParameter("uuid", request.getUuid());
- urlBuilder.addQueryParameter("name", request.getName());
- urlBuilder.addQueryParameter("realName", request.getRealName());
- if (request.getSex() != null) {
- urlBuilder.addQueryParameter("sex", request.getSex().toString());
- }
- urlBuilder.addQueryParameter("email", request.getEmail());
- urlBuilder.addQueryParameter("phone", request.getPhone());
- log.info(urlBuilder.build().toString());
- builder.url(urlBuilder.build());
- builder.header("Authorization", "Basic " + clientSecret);
- builder.header("Content-Type", "application/x-www-form-urlencode");
- builder.get();
- return this.getResponse(builder);
- }
- // @ServiceMethod(method = "oauth2.wechat.mp.bind", title = "微信公众号账号绑定")
- // public OapResponse oauthWechatBind(Oauth2WechatMpBindRequest request) throws IOException {
- //
- // String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
- // String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
- //
- // Map<String, String> paramValues = new HashMap<>();
- // paramValues.put("uuid", request.getUuid());
- // paramValues.put("username", request.getUsername());
- // paramValues.put("password", request.getPassword());
- //
- // RequestBody body = RequestBody.create(JSON.toJSONString(paramValues), MediaType.parse("application/json; charset=utf-8"));
- //
- // Request.Builder builder = new Request.Builder();
- // builder.url(String.format("%s/blade-auth/oauth/bind/%s", hostAddress, request.getSource()));
- // builder.header("Authorization", "Basic " + clientSecret);
- // builder.header("Content-Type", "application/json");
- // builder.post(body);
- //
- // return this.getResponse(builder);
- // }
- //
- // @ServiceMethod(method = "oauth2.wechat.mp.register.bind", title = "微信公众号注册绑定")
- // public OapResponse oauthWechatRegisterBind(Oauth2WechatMpRegisterBindRequest request) throws IOException {
- //
- // String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
- // String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
- //
- // Map<String, String> paramValues = new HashMap<>();
- // paramValues.put("uuid", request.getUuid());
- // paramValues.put("username", request.getUsername());
- // paramValues.put("password", request.getPassword());
- //
- // RequestBody body = RequestBody.create(JSON.toJSONString(paramValues), MediaType.parse("application/json; charset=utf-8"));
- //
- // Request.Builder builder = new Request.Builder();
- // builder.url(String.format("%s/blade-auth/oauth/register/bind/%s", hostAddress, request.getSource()));
- // builder.header("Authorization", "Basic " + clientSecret);
- // builder.header("Content-Type", "application/json");
- // builder.post(body);
- //
- // return this.getResponse(builder);
- // }
- /**
- * 已经对接成功
- *
- * @param request
- * @return
- * @throws IOException
- */
- @ServiceMethod(method = "oauth2.register", title = "注册用户")
- public OapResponse oauthRegister(Oauth2RegisterRequest request) throws IOException {
- String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
- String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
- Map<String, String> paramValues = new HashMap<>();
- paramValues.put("account", request.getAccount());
- paramValues.put("password", request.getPassword());
- RequestBody requestBody = RequestBody.create(JSON.toJSONString(paramValues), MediaType.parse("application/json; charset=utf-8"));
- Request.Builder builder = new Request.Builder();
- builder.url(String.format("%s/blade-user/account/submit", hostAddress));
- builder.header("Authorization", "Basic " + clientSecret);
- builder.header("Content-Type", "application/json");
- builder.post(requestBody);
- return this.getResponse(builder);
- }
- /**
- * 已经对接成功
- *
- * @param request
- * @return
- * @throws IOException
- */
- @ServiceMethod(method = "oauth2.update.info", title = "更新用户信息")
- public OapResponse oauthUpdateInfo(Oauth2UpdateInfoRequest request) throws IOException {
- String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
- String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
- Map<String, String> paramValues = new HashMap<>();
- paramValues.put("name", request.getName());
- paramValues.put("realName", request.getRealName());
- if (request.getSex() != null) {
- paramValues.put("sex", request.getSex().toString());
- }
- if (StringUtils.isNotBlank(request.getEmail())) {
- paramValues.put("email", request.getEmail());
- }
- if (StringUtils.isNotBlank(request.getPhone())) {
- paramValues.put("phone", request.getPhone());
- }
- RequestBody requestBody = RequestBody.create(JSON.toJSONString(paramValues), MediaType.parse("application/json; charset=utf-8"));
- Request.Builder builder = new Request.Builder();
- builder.url(String.format("%s/blade-user/account/update-info", hostAddress));
- builder.header("Authorization", "Basic " + clientSecret);
- builder.header("Blade-Auth", "bearer " + request.getAccessToken());
- builder.header("Content-Type", "application/json");
- builder.post(requestBody);
- return this.getResponse(builder);
- }
- /**
- * 已经对接成功
- *
- * @param request
- * @return
- * @throws IOException
- */
- @ServiceMethod(method = "oauth2.update.password", title = "修改密码")
- public OapResponse oauthUpdatePassword(Oauth2UpdatePasswordRequest request) throws IOException {
- String hostAddress = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "hostAddress");
- String clientSecret = this.systemConfigService.getStringValue("OAUTH2_API_CONFIG", "clientSecret");
- Map<String, String> paramValues = new HashMap<>();
- paramValues.put("oldPassword", DigestUtils.md5Hex(request.getOldPassword()));
- paramValues.put("newPassword", DigestUtils.md5Hex(request.getNewPassword()));
- FormBody.Builder formBuilder = new FormBody.Builder();
- paramValues.forEach(formBuilder::add);
- Request.Builder builder = new Request.Builder();
- builder.url(String.format("%s/blade-user/account/update-password", hostAddress));
- builder.header("Authorization", "Basic " + clientSecret);
- builder.header("Blade-Auth", "bearer " + request.getAccessToken());
- builder.header("Content-Type", "application/x-www-form-urlencode");
- builder.post(formBuilder.build());
- return this.getResponse(builder);
- }
- private OapResponse getResponse(Request.Builder builder) throws IOException {
- Response resp = this.okHttpClient.newCall(builder.build()).execute();
- if (resp.isSuccessful()) {
- String result = Objects.requireNonNull(resp.body()).string();
- log.info(result);
- JSONObject jsonObject = JSON.parseObject(result);
- String code = jsonObject.getString("code");
- String msg = jsonObject.getString("msg");
- JSONObject data = jsonObject.getJSONObject("data");
- if (StringUtils.equals("200", code)) {
- return OapResponse.success().setBody(data);
- }
- return OapResponse.fail("REMOTE_ERROR_" + code, msg);
- } else {
- String body = Objects.requireNonNull(resp.body()).string();
- log.info(body);
- if (Arrays.asList(401, 400).contains(resp.code())) {
- JSONObject object = JSON.parseObject(body);
- String msg = object.getString("msg");
- if (StringUtils.isBlank(msg)) {
- msg = object.getString("error_description");
- }
- return OapResponse.fail("ERROR", msg);
- } else {
- return OapResponse.fail("NETWORK_ERROR", resp.message());
- }
- }
- }
- }
|