提交 4779be16 编写于 作者: nancy's avatar nancy

修改access_token存储逻辑,按照appkey分别存储多个应用的access_token,以适配多个钉钉应用接入情况

上级 34658fa5
...@@ -28,6 +28,7 @@ import org.apache.http.impl.client.HttpClients; ...@@ -28,6 +28,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.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.crypto.Mac; import javax.crypto.Mac;
...@@ -38,7 +39,7 @@ import java.security.InvalidKeyException; ...@@ -38,7 +39,7 @@ import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.sql.Wrapper; import java.sql.Wrapper;
import java.util.Date; import java.util.*;
/** /**
* 实体[IBZUSER] 钉钉用户注册接口实现 * 实体[IBZUSER] 钉钉用户注册接口实现
...@@ -57,24 +58,22 @@ public class UserDingtalkRegisterService { ...@@ -57,24 +58,22 @@ public class UserDingtalkRegisterService {
private ISysUserAuthService sysUserAuthService; private ISysUserAuthService sysUserAuthService;
// private long lastRefreshTime=System.currentTimeMillis()-7200001;
// private String accessToken="";
private long lastRefreshTime=System.currentTimeMillis()-7200001; // public boolean isExpire()
private String accessToken=""; // {
public boolean isExpire() // if(System.currentTimeMillis()<(lastRefreshTime+7200000))
{ // {
if(System.currentTimeMillis()<(lastRefreshTime+7200000)) // System.currentTimeMillis();
{ // return false;
System.currentTimeMillis(); // }
return false; // return true;
} // }
return true;
}
public synchronized String getAccessToken(String appKey,String appSecret) public synchronized String getAccessToken(String appKey,String appSecret)
{ {
if(isExpire()) { if (isExpire(appKey)) {
DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken"); DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest(); OapiGettokenRequest request = new OapiGettokenRequest();
request.setAppkey(appKey); request.setAppkey(appKey);
...@@ -82,16 +81,17 @@ public class UserDingtalkRegisterService { ...@@ -82,16 +81,17 @@ public class UserDingtalkRegisterService {
request.setHttpMethod("GET"); request.setHttpMethod("GET");
try { try {
OapiGettokenResponse response = client.execute(request); OapiGettokenResponse response = client.execute(request);
if(response.getErrcode()!=0||StringUtils.isEmpty(response.getAccessToken())) if (response.getErrcode() != 0 || StringUtils.isEmpty(response.getAccessToken()))
throw new BadRequestAlertException("获取access_token失败","UserDingtalkRegisterService",response.getErrmsg()); throw new BadRequestAlertException("获取access_token失败", "UserDingtalkRegisterService", response.getErrmsg());
lastRefreshTime = System.currentTimeMillis(); // lastRefreshTime = System.currentTimeMillis();
accessToken = response.getAccessToken(); // accessToken = response.getAccessToken();
setTokenInfo(appKey, response.getAccessToken(), System.currentTimeMillis());
} catch (ApiException e) { } catch (ApiException e) {
e.printStackTrace(); e.printStackTrace();
throw new InternalServerErrorException("获取access_token失败"); throw new InternalServerErrorException("获取access_token失败");
} }
} }
return accessToken; return getToken(appKey);
} }
@Autowired @Autowired
...@@ -101,7 +101,8 @@ public class UserDingtalkRegisterService { ...@@ -101,7 +101,8 @@ public class UserDingtalkRegisterService {
{ {
return getOpenAccess(id,true); return getOpenAccess(id,true);
} }
public SysOpenAccess getOpenAccess(String id,boolean throwEx)
public SysOpenAccess getOpenAccess(String id, boolean throwEx)
{ {
final String accessid = StringUtils.isEmpty(id)?"dingtalk":id; final String accessid = StringUtils.isEmpty(id)?"dingtalk":id;
SysOpenAccess sysOpenAccess=sysOpenAccessService.getOne(Wrappers.<SysOpenAccess>lambdaQuery().eq(SysOpenAccess::getOpenType,"dingtalk"). SysOpenAccess sysOpenAccess=sysOpenAccessService.getOne(Wrappers.<SysOpenAccess>lambdaQuery().eq(SysOpenAccess::getOpenType,"dingtalk").
...@@ -115,7 +116,7 @@ public class UserDingtalkRegisterService { ...@@ -115,7 +116,7 @@ public class UserDingtalkRegisterService {
if(!accessToken.equals(sysOpenAccess.getAccessToken())) if(!accessToken.equals(sysOpenAccess.getAccessToken()))
{ {
sysOpenAccess.setAccessToken(accessToken); sysOpenAccess.setAccessToken(accessToken);
sysOpenAccess.setExpiresTime(new Timestamp(lastRefreshTime)); sysOpenAccess.setExpiresTime(new Timestamp(getLastRefreshTime(sysOpenAccess.getAccessKey())));
sysOpenAccessService.update(sysOpenAccess); sysOpenAccessService.update(sysOpenAccess);
} }
}catch (Exception e) { }catch (Exception e) {
...@@ -222,5 +223,76 @@ public class UserDingtalkRegisterService { ...@@ -222,5 +223,76 @@ public class UserDingtalkRegisterService {
return returnObj; return returnObj;
} }
private static Map<String, List<String>> tokenMapping = new HashMap<>();
/**
* 适配当UAA接入多个钉钉应用的情况。
*
* @param appkey 钉钉appkey
* @return
*/
public boolean isExpire(String appkey) {
if (!tokenMapping.containsKey(appkey))
return true;
List<String> accessTokenInfo = tokenMapping.get(appkey);
if (CollectionUtils.isEmpty(accessTokenInfo) || accessTokenInfo.size() != 2)
return true;
String accessToken = accessTokenInfo.get(0);
String refreshtime = accessTokenInfo.get(1);
if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(refreshtime))
return true;
if (System.currentTimeMillis() < (Long.valueOf(refreshtime) + 7100000)) {
System.currentTimeMillis();
return false;
}
return true;
}
private void setTokenInfo(String appkey, String token, Long refreshTime) {
if (StringUtils.isEmpty(token) || StringUtils.isEmpty(refreshTime))
return;
if (!tokenMapping.containsKey(appkey)) {
List<String> tokenInfo = new ArrayList<>(2);
tokenMapping.put(appkey, tokenInfo);
}
List<String> tokenInfo = tokenMapping.get(appkey);
tokenInfo.set(0, token);
tokenInfo.set(1, String.valueOf(refreshTime));
}
private String getToken(String appkey){
if(!StringUtils.isEmpty(appkey)){
List<String> tokenInfo = tokenMapping.get(appkey);
if(!CollectionUtils.isEmpty(tokenInfo) && tokenInfo.size()==2)
return tokenMapping.get(appkey).get(0);
}
return null;
}
/**
* 钉钉默认超时时间为2小时(7200秒),防止程序卡点,换成7100秒(100秒预留)
*/
private Long getLastRefreshTime(String appkey){
if(!StringUtils.isEmpty(appkey)){
List<String> tokenInfo = tokenMapping.get(appkey);
if(!CollectionUtils.isEmpty(tokenInfo) && tokenInfo.size()==2) {
String refreshtime = tokenMapping.get(appkey).get(1);
try {
return Long.valueOf(refreshtime);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return System.currentTimeMillis()-7100001;
}
} }
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册