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

添加一些判断

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