提交 2decea9b 编写于 作者: laizhilong's avatar laizhilong

添加一些判断

上级 a8a4746c
......@@ -15,6 +15,7 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
......@@ -98,19 +99,23 @@ public class UserDingtalkRegisterService {
// 获取结果实体
HttpEntity entity = response.getEntity();
JSONObject entityJson = null;
if (entity != null) {
if (StringUtils.isEmpty(entity)) {
throw new BadRequestAlertException("钉钉服务端返回结果为空", "UserDingtalkRegisterService", "");
} else {
//按指定编码转换结果实体为String类型
entityJson = JSONObject.parseObject(EntityUtils.toString(entity, "UTF-8"));
}
// 是否获取钉钉用户信息成功
if (entityJson.containsKey("user_info")) {
returnObj = entityJson.getJSONObject("user_info");
} else {
throw new BadRequestAlertException("code获取钉钉用户信息失败", "UserDingtalkRegisterService", "");
throw new BadRequestAlertException("获取钉钉授权用户个人信息失败", "UserDingtalkRegisterService", "");
}
} catch (NoSuchAlgorithmException | InvalidKeyException | IOException e) {
}
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw new BadRequestAlertException("钉钉生成安全签名失败", "UserDingtalkRegisterService", "");
} catch (IOException e) {
throw new BadRequestAlertException("连接钉钉服务端失败", "UserDingtalkRegisterService", "");
}
return returnObj;
......
......@@ -49,43 +49,58 @@ public class UserWechatRegisterService {
public JSONObject requestWechatUserByCode(String code, String state, String wechatAppId, String wechatappsecret) {
JSONObject returnObj = null;
try {
// 1.根据code获取access_token
String getAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wechatAppId + "&secret=" + wechatappsecret + "&code=" + code + "&grant_type=authorization_code";
// 1.根据code获取access_token、openid、refresh_token
String getAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid=" + wechatAppId +
"&secret=" + wechatappsecret +
"&code=" + code +
"&grant_type=authorization_code";
JSONObject responseObj = JSONObject.parseObject(HttpUtils.get(getAccessTokenUrl, null, null));
if (!responseObj.containsKey("access_token") && !responseObj.containsKey("openid")) {
throw new BadRequestAlertException("获取access_token失败!", "UserWechatRegisterService", "");
throw new BadRequestAlertException("微信服务端获取access_token失败!", "UserWechatRegisterService", "");
}
String access_token = responseObj.getString("access_token");
String openid = responseObj.getString("openid");
String refresh_token = responseObj.getString("refresh_token");
// 2.检验授权凭证(access_token)是否有效
String checkAccessTokenUrl = "https://api.weixin.qq.com/sns/auth?access_token=" + access_token + "&openid=" + responseObj.get("openid");
// 2.检验授权凭证(access_token)是否有效,无效则需要刷新access_token
String checkAccessTokenUrl = "https://api.weixin.qq.com/sns/auth?" +
"access_token=" + access_token +
"&openid=" + openid;
JSONObject responseObj2 = JSONObject.parseObject(HttpUtils.get(checkAccessTokenUrl, null, null));
if (responseObj2.getInteger("errcode") != 0) {
// access_token已失效,使用refresh_token进行刷新
String refreshAccess_token = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + wechatAppId + "&grant_type=refresh_token&refresh_token=" + refresh_token;
// access_token已失效,使用refresh_token刷新access_token
String refreshAccess_token = "https://api.weixin.qq.com/sns/oauth2/refresh_token?" +
"appid=" + wechatAppId +
"&grant_type=refresh_token" +
"&refresh_token=" + refresh_token;
JSONObject responseObj3 = JSONObject.parseObject(HttpUtils.get(refreshAccess_token, null, null));
if (!responseObj3.containsKey("access_token") || !responseObj3.containsKey("openid")) {
throw new BadRequestAlertException("重新获取access_token失败!", "UserWechatRegisterService", "");
throw new BadRequestAlertException("微信服务端刷新access_token失败!", "UserWechatRegisterService", "");
}
openid = responseObj3.getString("openid");
// 重新拿到access_token、openid、refresh_token
access_token = responseObj3.getString("access_token");
openid = responseObj3.getString("openid");
refresh_token = responseObj3.getString("refresh_token");
}
// 3.access_token有效,拉取用户信息(需scope为 snsapi_userinfo)
String getweChatUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
// 3.access_token有效,拉取用户信息
String getweChatUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo?" +
"access_token=" + access_token +
"&openid=" + openid +
"&lang=zh_CN";
returnObj = JSONObject.parseObject(HttpUtils.get(getweChatUserInfoUrl, null, null));
if (StringUtils.isEmpty(returnObj) || returnObj.containsKey("errcode")) {
throw new BadRequestAlertException("拉取微信用户信息失败!", "UserWechatRegisterService", "");
if (StringUtils.isEmpty(returnObj)) {
throw new BadRequestAlertException("微信服务端返回结果为空!", "UserWechatRegisterService", "");
} else if (!returnObj.containsKey("openid")) {
throw new BadRequestAlertException("获取微信用户信息失败!", "UserWechatRegisterService", "");
}
} catch (UnsupportedEncodingException e) {
throw new BadRequestAlertException("获取微信授权用户相关信息失败!", "UserWechatRegisterService", "");
throw new BadRequestAlertException("连接微信服务端失败!", "UserWechatRegisterService", "");
}
return returnObj;
}
}
\ No newline at end of file
......@@ -218,7 +218,8 @@ public class UserWechatRegisterResource {
if (StringUtils.isEmpty(openid)) {
// 通过code获取微信用户信息
JSONObject returnObj = userWechatRegisterService.requestWechatUserByCode(code, state ,WechatAppid , WechatAppsecret);
if (!StringUtils.isEmpty(returnObj) && !returnObj.containsKey("errcode")) {
if (!StringUtils.isEmpty(returnObj)) {
if (returnObj.containsKey("openid")) {
// 微信注册
String uuid = UUID.randomUUID().toString();
ibzuser.setPassword(password);
......@@ -228,6 +229,11 @@ public class UserWechatRegisterResource {
ibzuser.setNickname(returnObj.getString("nickname"));
ibzuser.setAvatar(returnObj.getString("openid"));
userWechatRegisterService.toRegister(ibzuser);
}else {
throw new BadRequestAlertException("获取微信授权用户相关信息失败!", "UserWechatRegisterResource", "");
}
}else {
throw new BadRequestAlertException("获取微信授权用户相关信息失败!", "UserWechatRegisterResource", "");
}
} else {
// 已经有微信用户信息直接注册
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册