|
|
@ -1,5 +1,6 @@
|
|
|
|
package com.java3y.austin.support.utils;
|
|
|
|
package com.java3y.austin.support.utils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
|
import cn.hutool.http.ContentType;
|
|
|
|
import cn.hutool.http.ContentType;
|
|
|
|
import cn.hutool.http.Header;
|
|
|
|
import cn.hutool.http.Header;
|
|
|
@ -14,9 +15,16 @@ import com.java3y.austin.common.constant.CommonConstant;
|
|
|
|
import com.java3y.austin.common.constant.SendChanelUrlConstant;
|
|
|
|
import com.java3y.austin.common.constant.SendChanelUrlConstant;
|
|
|
|
import com.java3y.austin.common.dto.account.DingDingWorkNoticeAccount;
|
|
|
|
import com.java3y.austin.common.dto.account.DingDingWorkNoticeAccount;
|
|
|
|
import com.java3y.austin.common.dto.account.GeTuiAccount;
|
|
|
|
import com.java3y.austin.common.dto.account.GeTuiAccount;
|
|
|
|
|
|
|
|
import com.java3y.austin.common.enums.ChannelType;
|
|
|
|
|
|
|
|
import com.java3y.austin.common.enums.EnumUtil;
|
|
|
|
import com.java3y.austin.support.dto.GeTuiTokenResultDTO;
|
|
|
|
import com.java3y.austin.support.dto.GeTuiTokenResultDTO;
|
|
|
|
import com.java3y.austin.support.dto.QueryTokenParamDTO;
|
|
|
|
import com.java3y.austin.support.dto.QueryTokenParamDTO;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取第三发token工具类
|
|
|
|
* 获取第三发token工具类
|
|
|
@ -24,21 +32,65 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
|
* @author wuhui
|
|
|
|
* @author wuhui
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Slf4j
|
|
|
|
@Slf4j
|
|
|
|
|
|
|
|
@Component
|
|
|
|
public class AccessTokenUtils {
|
|
|
|
public class AccessTokenUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private StringRedisTemplate redisTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取 对应渠道的accessToken
|
|
|
|
|
|
|
|
* 1,redis存在,则直接从redis取
|
|
|
|
|
|
|
|
* 2,redis不存在,调用底层方法去获取accessToken,并加入到redis中
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param sendChannel
|
|
|
|
|
|
|
|
* @param accountId 账号Id(数据库的主键)
|
|
|
|
|
|
|
|
* @param account 渠道的对应的账号详情
|
|
|
|
|
|
|
|
* @param refresh 是否要强制刷新现有的缓存accessToken
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
* @see com.java3y.austin.common.enums.ChannelType
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public String getAccessToken(Integer sendChannel, Integer accountId, Object account, Boolean refresh) {
|
|
|
|
|
|
|
|
String resultToken = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// expireTime跟渠道的accessToken失效有关(个推accessToken默认有效是1天,钉钉工作消息默认有效是2小时)
|
|
|
|
|
|
|
|
String accessTokenPrefix = EnumUtil.getEnumByCode(sendChannel, ChannelType.class).getAccessTokenPrefix();
|
|
|
|
|
|
|
|
Long expireTime = EnumUtil.getEnumByCode(sendChannel, ChannelType.class).getAccessTokenExpire();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
resultToken = redisTemplate.opsForValue().get(accessTokenPrefix + accountId);
|
|
|
|
|
|
|
|
if (StrUtil.isNotBlank(resultToken) && !refresh) {
|
|
|
|
|
|
|
|
return resultToken;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ChannelType.DING_DING_WORK_NOTICE.getCode().equals(sendChannel)) {
|
|
|
|
|
|
|
|
resultToken = getDingDingAccessToken(account);
|
|
|
|
|
|
|
|
} else if (ChannelType.PUSH.getCode().equals(sendChannel)) {
|
|
|
|
|
|
|
|
resultToken = getGeTuiAccessToken(account);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StrUtil.isNotBlank(resultToken)) {
|
|
|
|
|
|
|
|
redisTemplate.opsForValue().set(accessTokenPrefix + accountId, resultToken, expireTime, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
log.error("AccessTokenUtils#getAccessToken fail,sendChannel:[{}],accountId:[{}],error mgs:{}", sendChannel, accountId, Throwables.getStackTraceAsString(e));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultToken;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取钉钉 access_token
|
|
|
|
* 获取钉钉 access_token
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param account 钉钉工作消息 账号信息
|
|
|
|
* @param account 钉钉工作消息 账号信息
|
|
|
|
* @return 钉钉 access_token
|
|
|
|
* @return 钉钉 access_token
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static String getDingDingAccessToken(DingDingWorkNoticeAccount account) {
|
|
|
|
private String getDingDingAccessToken(Object account) {
|
|
|
|
String accessToken = "";
|
|
|
|
String accessToken = "";
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
DingDingWorkNoticeAccount dingWorkNoticeAccount = (DingDingWorkNoticeAccount) account;
|
|
|
|
DingTalkClient client = new DefaultDingTalkClient(SendChanelUrlConstant.DING_DING_TOKEN_URL);
|
|
|
|
DingTalkClient client = new DefaultDingTalkClient(SendChanelUrlConstant.DING_DING_TOKEN_URL);
|
|
|
|
OapiGettokenRequest req = new OapiGettokenRequest();
|
|
|
|
OapiGettokenRequest req = new OapiGettokenRequest();
|
|
|
|
req.setAppkey(account.getAppKey());
|
|
|
|
req.setAppkey(dingWorkNoticeAccount.getAppKey());
|
|
|
|
req.setAppsecret(account.getAppSecret());
|
|
|
|
req.setAppsecret(dingWorkNoticeAccount.getAppSecret());
|
|
|
|
req.setHttpMethod(CommonConstant.REQUEST_METHOD_GET);
|
|
|
|
req.setHttpMethod(CommonConstant.REQUEST_METHOD_GET);
|
|
|
|
OapiGettokenResponse rsp = client.execute(req);
|
|
|
|
OapiGettokenResponse rsp = client.execute(req);
|
|
|
|
accessToken = rsp.getAccessToken();
|
|
|
|
accessToken = rsp.getAccessToken();
|
|
|
@ -54,20 +106,21 @@ public class AccessTokenUtils {
|
|
|
|
* @param account 创建个推账号时的元信息
|
|
|
|
* @param account 创建个推账号时的元信息
|
|
|
|
* @return 个推的 access_token
|
|
|
|
* @return 个推的 access_token
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static String getGeTuiAccessToken(GeTuiAccount account) {
|
|
|
|
private String getGeTuiAccessToken(Object account) {
|
|
|
|
String accessToken = "";
|
|
|
|
String accessToken = "";
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String url = SendChanelUrlConstant.GE_TUI_BASE_URL + account.getAppId() + SendChanelUrlConstant.GE_TUI_AUTH;
|
|
|
|
GeTuiAccount geTuiAccount = (GeTuiAccount) account;
|
|
|
|
|
|
|
|
String url = SendChanelUrlConstant.GE_TUI_BASE_URL + geTuiAccount.getAppId() + SendChanelUrlConstant.GE_TUI_AUTH;
|
|
|
|
String time = String.valueOf(System.currentTimeMillis());
|
|
|
|
String time = String.valueOf(System.currentTimeMillis());
|
|
|
|
String digest = SecureUtil.sha256().digestHex(account.getAppKey() + time + account.getMasterSecret());
|
|
|
|
String digest = SecureUtil.sha256().digestHex(geTuiAccount.getAppKey() + time + geTuiAccount.getMasterSecret());
|
|
|
|
QueryTokenParamDTO param = QueryTokenParamDTO.builder()
|
|
|
|
QueryTokenParamDTO param = QueryTokenParamDTO.builder()
|
|
|
|
.timestamp(time)
|
|
|
|
.timestamp(time)
|
|
|
|
.appKey(account.getAppKey())
|
|
|
|
.appKey(geTuiAccount.getAppKey())
|
|
|
|
.sign(digest).build();
|
|
|
|
.sign(digest).build();
|
|
|
|
|
|
|
|
|
|
|
|
String body = HttpRequest.post(url).header(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue())
|
|
|
|
String body = HttpRequest.post(url).header(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue())
|
|
|
|
.body(JSON.toJSONString(param))
|
|
|
|
.body(JSON.toJSONString(param))
|
|
|
|
.timeout(20000)
|
|
|
|
.timeout(2000)
|
|
|
|
.execute().body();
|
|
|
|
.execute().body();
|
|
|
|
GeTuiTokenResultDTO geTuiTokenResultDTO = JSON.parseObject(body, GeTuiTokenResultDTO.class);
|
|
|
|
GeTuiTokenResultDTO geTuiTokenResultDTO = JSON.parseObject(body, GeTuiTokenResultDTO.class);
|
|
|
|
if (geTuiTokenResultDTO.getCode().equals(0)) {
|
|
|
|
if (geTuiTokenResultDTO.getCode().equals(0)) {
|
|
|
|