!54 获取个推、钉钉第三方token补偿机制

Merge pull request !54 from 伍辉/feat-getAccessToken
pull/57/head
Java3y 1 year ago committed by Gitee
commit 6f7ed66121
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

@ -2,11 +2,6 @@ package com.java3y.austin.cron.handler;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.google.common.base.Throwables;
import com.java3y.austin.common.constant.CommonConstant;
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.dto.account.DingDingWorkNoticeAccount;
@ -14,6 +9,7 @@ import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.support.config.SupportThreadPoolConfig;
import com.java3y.austin.support.dao.ChannelAccountDao;
import com.java3y.austin.support.domain.ChannelAccount;
import com.java3y.austin.support.utils.AccessTokenUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -34,9 +30,6 @@ import java.util.List;
@Slf4j
public class RefreshDingDingAccessTokenHandler {
private static final String URL = "https://oapi.dingtalk.com/gettoken";
@Autowired
private StringRedisTemplate redisTemplate;
@ -54,7 +47,7 @@ public class RefreshDingDingAccessTokenHandler {
List<ChannelAccount> accountList = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(CommonConstant.FALSE, ChannelType.DING_DING_WORK_NOTICE.getCode());
for (ChannelAccount channelAccount : accountList) {
DingDingWorkNoticeAccount account = JSON.parseObject(channelAccount.getAccountConfig(), DingDingWorkNoticeAccount.class);
String accessToken = getAccessToken(account);
String accessToken = AccessTokenUtils.getDingDingAccessToken(account);
if (StrUtil.isNotBlank(accessToken)) {
redisTemplate.opsForValue().set(SendAccountConstant.DING_DING_ACCESS_TOKEN_PREFIX + channelAccount.getId(), accessToken);
}
@ -62,25 +55,4 @@ public class RefreshDingDingAccessTokenHandler {
});
}
/**
* access_token
*
* @param account
* @return
*/
private String getAccessToken(DingDingWorkNoticeAccount account) {
String accessToken = "";
try {
DingTalkClient client = new DefaultDingTalkClient(URL);
OapiGettokenRequest req = new OapiGettokenRequest();
req.setAppkey(account.getAppKey());
req.setAppsecret(account.getAppSecret());
req.setHttpMethod(CommonConstant.REQUEST_METHOD_GET);
OapiGettokenResponse rsp = client.execute(req);
accessToken = rsp.getAccessToken();
} catch (Exception e) {
log.error("RefreshDingDingAccessTokenHandler#getAccessToken fail:{}", Throwables.getStackTraceAsString(e));
}
return accessToken;
}
}

@ -1,21 +1,15 @@
package com.java3y.austin.cron.handler;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
import com.java3y.austin.common.constant.CommonConstant;
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.dto.account.GeTuiAccount;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.cron.dto.getui.GeTuiTokenResultDTO;
import com.java3y.austin.cron.dto.getui.QueryTokenParamDTO;
import com.java3y.austin.support.config.SupportThreadPoolConfig;
import com.java3y.austin.support.dao.ChannelAccountDao;
import com.java3y.austin.support.domain.ChannelAccount;
import com.java3y.austin.support.utils.AccessTokenUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -53,7 +47,7 @@ public class RefreshGeTuiAccessTokenHandler {
List<ChannelAccount> accountList = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(CommonConstant.FALSE, ChannelType.PUSH.getCode());
for (ChannelAccount channelAccount : accountList) {
GeTuiAccount account = JSON.parseObject(channelAccount.getAccountConfig(), GeTuiAccount.class);
String accessToken = getAccessToken(account);
String accessToken = AccessTokenUtils.getGeTuiAccessToken(account);
if (StrUtil.isNotBlank(accessToken)) {
redisTemplate.opsForValue().set(SendAccountConstant.GE_TUI_ACCESS_TOKEN_PREFIX + channelAccount.getId(), accessToken);
}
@ -61,35 +55,6 @@ public class RefreshGeTuiAccessTokenHandler {
});
}
/**
* access_token
*
* @param account
* @return
*/
private String getAccessToken(GeTuiAccount account) {
String accessToken = "";
try {
String url = "https://restapi.getui.com/v2/" + account.getAppId() + "/auth";
String time = String.valueOf(System.currentTimeMillis());
String digest = SecureUtil.sha256().digestHex(account.getAppKey() + time + account.getMasterSecret());
QueryTokenParamDTO param = QueryTokenParamDTO.builder()
.timestamp(time)
.appKey(account.getAppKey())
.sign(digest).build();
String body = HttpRequest.post(url).header(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue())
.body(JSON.toJSONString(param))
.timeout(20000)
.execute().body();
GeTuiTokenResultDTO geTuiTokenResultDTO = JSON.parseObject(body, GeTuiTokenResultDTO.class);
if (geTuiTokenResultDTO.getCode().equals(0)) {
accessToken = geTuiTokenResultDTO.getData().getToken();
}
} catch (Exception e) {
log.error("RefreshGeTuiAccessTokenHandler#getAccessToken fail:{}", Throwables.getStackTraceAsString(e));
}
return accessToken;
}
}

@ -25,6 +25,7 @@ import com.java3y.austin.handler.handler.BaseHandler;
import com.java3y.austin.handler.handler.Handler;
import com.java3y.austin.support.config.SupportThreadPoolConfig;
import com.java3y.austin.support.domain.MessageTemplate;
import com.java3y.austin.support.utils.AccessTokenUtils;
import com.java3y.austin.support.utils.AccountUtils;
import com.java3y.austin.support.utils.LogUtils;
import lombok.extern.slf4j.Slf4j;
@ -70,7 +71,7 @@ public class DingDingWorkNoticeHandler extends BaseHandler implements Handler {
try {
DingDingWorkNoticeAccount account = accountUtils.getAccountById(taskInfo.getSendAccount(), DingDingWorkNoticeAccount.class);
OapiMessageCorpconversationAsyncsendV2Request request = assembleParam(account, taskInfo);
String accessToken = redisTemplate.opsForValue().get(SendAccountConstant.DING_DING_ACCESS_TOKEN_PREFIX + taskInfo.getSendAccount());
String accessToken = getAccessToken(account, Long.valueOf(taskInfo.getSendAccount()));
OapiMessageCorpconversationAsyncsendV2Response response = new DefaultDingTalkClient(SEND_URL).execute(request, accessToken);
// 发送成功后记录TaskId用于消息撤回(支持当天的)
@ -183,7 +184,7 @@ public class DingDingWorkNoticeHandler extends BaseHandler implements Handler {
SupportThreadPoolConfig.getPendingSingleThreadPool().execute(() -> {
try {
DingDingWorkNoticeAccount account = accountUtils.getAccountById(messageTemplate.getSendAccount(), DingDingWorkNoticeAccount.class);
String accessToken = redisTemplate.opsForValue().get(SendAccountConstant.DING_DING_ACCESS_TOKEN_PREFIX + messageTemplate.getSendAccount());
String accessToken = getAccessToken(account, Long.valueOf(messageTemplate.getSendAccount()));
while (redisTemplate.opsForList().size(DING_DING_RECALL_KEY_PREFIX + messageTemplate.getId()) > 0) {
String taskId = redisTemplate.opsForList().leftPop(DING_DING_RECALL_KEY_PREFIX + messageTemplate.getId());
DingTalkClient client = new DefaultDingTalkClient(RECALL_URL);
@ -205,7 +206,7 @@ public class DingDingWorkNoticeHandler extends BaseHandler implements Handler {
public void pull(Long accountId) {
try {
DingDingWorkNoticeAccount account = accountUtils.getAccountById(accountId.intValue(), DingDingWorkNoticeAccount.class);
String accessToken = redisTemplate.opsForValue().get(SendAccountConstant.DING_DING_ACCESS_TOKEN_PREFIX + accountId);
String accessToken = getAccessToken(account, accountId);
DingTalkClient client = new DefaultDingTalkClient(PULL_URL);
OapiMessageCorpconversationGetsendresultRequest req = new OapiMessageCorpconversationGetsendresultRequest();
req.setAgentId(Long.valueOf(account.getAgentId()));
@ -215,5 +216,26 @@ public class DingDingWorkNoticeHandler extends BaseHandler implements Handler {
log.error("DingDingWorkNoticeHandler#pull fail:{}", Throwables.getStackTraceAsString(e));
}
}
/**
* token
*
* @param account
* @param accountId ID
* @return token
*/
private String getAccessToken(DingDingWorkNoticeAccount account, Long accountId) {
String accessToken = redisTemplate.opsForValue().get(SendAccountConstant.DING_DING_ACCESS_TOKEN_PREFIX + accountId);
if (StrUtil.isNotBlank(accessToken)) {
return accessToken;
}
accessToken = AccessTokenUtils.getDingDingAccessToken(account);
if (StrUtil.isNotBlank(accessToken)) {
redisTemplate.opsForValue().set(SendAccountConstant.DING_DING_ACCESS_TOKEN_PREFIX + accountId, accessToken);
} else {
log.error("DingDingWorkNoticeHandler#getAccessToken fail accessToken{} accountId{} ", accessToken, accountId);
}
return accessToken;
}
}

@ -2,6 +2,7 @@ package com.java3y.austin.handler.handler.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
@ -19,6 +20,7 @@ import com.java3y.austin.handler.domain.push.getui.SendPushResult;
import com.java3y.austin.handler.handler.BaseHandler;
import com.java3y.austin.handler.handler.Handler;
import com.java3y.austin.support.domain.MessageTemplate;
import com.java3y.austin.support.utils.AccessTokenUtils;
import com.java3y.austin.support.utils.AccountUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -58,7 +60,7 @@ public class PushHandler extends BaseHandler implements Handler {
try {
GeTuiAccount account = accountUtils.getAccountById(taskInfo.getSendAccount(), GeTuiAccount.class);
String token = redisTemplate.opsForValue().get(SendAccountConstant.GE_TUI_ACCESS_TOKEN_PREFIX + taskInfo.getSendAccount());
String token = getAccessToken(taskInfo, account);
PushParam pushParam = PushParam.builder().token(token).appId(account.getAppId()).taskInfo(taskInfo).build();
String result;
@ -145,6 +147,26 @@ public class PushHandler extends BaseHandler implements Handler {
return taskId;
}
/**
* token
*
* @param taskInfo
* @param account
* @return token
*/
private String getAccessToken(TaskInfo taskInfo, GeTuiAccount account) {
String token = redisTemplate.opsForValue().get(SendAccountConstant.GE_TUI_ACCESS_TOKEN_PREFIX + taskInfo.getSendAccount());
if (StrUtil.isNotBlank(token)) {
return token;
}
token = AccessTokenUtils.getGeTuiAccessToken(account);
if (StrUtil.isNotBlank(token)) {
redisTemplate.opsForValue().set(SendAccountConstant.GE_TUI_ACCESS_TOKEN_PREFIX + taskInfo.getSendAccount(), token);
} else {
log.error("PushHandler#getAccessToken fail taskInfo:{} account:{}", taskInfo, account);
}
return token;
}
private SendPushParam assembleParam(PushContentModel pushContentModel) {
return assembleParam(pushContentModel, null);

@ -0,0 +1,37 @@
package com.java3y.austin.support.dto;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author 3y
* @date 2022/5/8
* <p>
* https://docs.getui.com/getui/server/rest_v2/token/
*/
@NoArgsConstructor
@Data
@AllArgsConstructor
@Builder
public class GeTuiTokenResultDTO {
@JSONField(name = "msg")
private String msg;
@JSONField(name = "code")
private Integer code;
@JSONField(name = "data")
private DataDTO data;
@NoArgsConstructor
@Data
public static class DataDTO {
@JSONField(name = "expire_time")
private String expireTime;
@JSONField(name = "token")
private String token;
}
}

@ -0,0 +1,36 @@
package com.java3y.austin.support.dto;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* token
*
* @author 3y
* https://docs.getui.com/getui/server/rest_v2/token/
*/
@NoArgsConstructor
@Data
@Builder
@AllArgsConstructor
public class QueryTokenParamDTO {
/**
* sign
*/
@JSONField(name = "sign")
private String sign;
/**
* timestamp
*/
@JSONField(name = "timestamp")
private String timestamp;
/**
* appkey
*/
@JSONField(name = "appkey")
private String appKey;
}

@ -0,0 +1,83 @@
package com.java3y.austin.support.utils;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.google.common.base.Throwables;
import com.java3y.austin.common.constant.CommonConstant;
import com.java3y.austin.common.dto.account.DingDingWorkNoticeAccount;
import com.java3y.austin.common.dto.account.GeTuiAccount;
import com.java3y.austin.support.dto.GeTuiTokenResultDTO;
import com.java3y.austin.support.dto.QueryTokenParamDTO;
import lombok.extern.slf4j.Slf4j;
/**
* token
*/
@Slf4j
public class AccessTokenUtils {
/**
* token
*/
private static final String DING_DING_TOKEN_URL = "https://oapi.dingtalk.com/gettoken";
/**
* access_token
*
* @param account
* @return access_token
*/
public static String getDingDingAccessToken(DingDingWorkNoticeAccount account) {
String accessToken = "";
try {
DingTalkClient client = new DefaultDingTalkClient(DING_DING_TOKEN_URL);
OapiGettokenRequest req = new OapiGettokenRequest();
req.setAppkey(account.getAppKey());
req.setAppsecret(account.getAppSecret());
req.setHttpMethod(CommonConstant.REQUEST_METHOD_GET);
OapiGettokenResponse rsp = client.execute(req);
accessToken = rsp.getAccessToken();
} catch (Exception e) {
log.error("AccessTokenUtils#getDingDingAccessToken fail:{}", Throwables.getStackTraceAsString(e));
}
return accessToken;
}
/**
* access_token
*
* @param account
* @return access_token
*/
public static String getGeTuiAccessToken(GeTuiAccount account) {
String accessToken = "";
try {
String url = "https://restapi.getui.com/v2/" + account.getAppId() + "/auth";
String time = String.valueOf(System.currentTimeMillis());
String digest = SecureUtil.sha256().digestHex(account.getAppKey() + time + account.getMasterSecret());
QueryTokenParamDTO param = QueryTokenParamDTO.builder()
.timestamp(time)
.appKey(account.getAppKey())
.sign(digest).build();
String body = HttpRequest.post(url).header(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue())
.body(JSON.toJSONString(param))
.timeout(20000)
.execute().body();
GeTuiTokenResultDTO geTuiTokenResultDTO = JSON.parseObject(body, GeTuiTokenResultDTO.class);
if (geTuiTokenResultDTO.getCode().equals(0)) {
accessToken = geTuiTokenResultDTO.getData().getToken();
}
} catch (Exception e) {
log.error("AccessTokenUtils#getGeTuiAccessToken fail:{}", Throwables.getStackTraceAsString(e));
}
return accessToken;
}
}
Loading…
Cancel
Save