|
@@ -1,6 +1,9 @@
|
|
|
package com.book.push.config;
|
|
|
|
|
|
+import com.book.dao.cps.pojo.AdminConfig;
|
|
|
import com.book.dao.cps.pojo.Platform;
|
|
|
+import com.book.push.service.dao.AdminConfigService;
|
|
|
+import com.book.push.service.redis.RedisService;
|
|
|
import com.book.push.service.wx.WxThirdPartService;
|
|
|
import com.book.push.utils.JsonUtils;
|
|
|
import com.book.push.utils.Md5Util;
|
|
@@ -9,26 +12,37 @@ import com.book.push.vo.ComponentInfo;
|
|
|
import lombok.Getter;
|
|
|
import lombok.NonNull;
|
|
|
import lombok.Setter;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import redis.clients.jedis.Jedis;
|
|
|
import redis.clients.jedis.util.Pool;
|
|
|
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
/**
|
|
|
* created in 2021/8/25
|
|
|
* Project: book-store
|
|
|
*
|
|
|
* @author win7
|
|
|
*/
|
|
|
-
|
|
|
+@Slf4j
|
|
|
public class WxOpenInRedisBookConfigStorage extends WxOpenInRedisConfigStorage {
|
|
|
@Getter
|
|
|
@Setter
|
|
|
private Platform platform;
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ private RedisService redisService;
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ private AdminConfigService adminConfigService;
|
|
|
|
|
|
public WxOpenInRedisBookConfigStorage(@NonNull Pool<Jedis> jedisPool, String keyPrefix) {
|
|
|
super(jedisPool, keyPrefix);
|
|
|
}
|
|
|
- public WxOpenInRedisBookConfigStorage(Platform platform,@NonNull Pool<Jedis> jedisPool, String keyPrefix) {
|
|
|
+
|
|
|
+ public WxOpenInRedisBookConfigStorage(Platform platform, @NonNull Pool<Jedis> jedisPool, String keyPrefix) {
|
|
|
super(jedisPool, keyPrefix);
|
|
|
this.platform = platform;
|
|
|
}
|
|
@@ -39,7 +53,7 @@ public class WxOpenInRedisBookConfigStorage extends WxOpenInRedisConfigStorage {
|
|
|
|
|
|
public String getVerifyTicketCacheKey() {
|
|
|
if (this.getComponentAppId() == null) {
|
|
|
- System.out.println("getVerifyTicketCacheKey is null,please set component appid");
|
|
|
+ log.error("getVerifyTicketCacheKey is null,please set component appid");
|
|
|
throw new NullPointerException("ComponentAppId is null,please set component appid");
|
|
|
}
|
|
|
return WxThirdPartService.VERIFY_TICKET_KEY_PREFIX + this.getComponentAppId();
|
|
@@ -52,23 +66,18 @@ public class WxOpenInRedisBookConfigStorage extends WxOpenInRedisConfigStorage {
|
|
|
|
|
|
public String getComponentTokenCacheKey() {
|
|
|
if (this.getComponentAppId() == null || this.getComponentAppSecret() == null || this.getComponentVerifyTicket() == null) {
|
|
|
- System.out.println("getVerifyTicketCacheKey is null,please set component appid");
|
|
|
-// throw new NullPointerException("ComponentAppId " +
|
|
|
-// "must not be null,please set component appid");
|
|
|
- }
|
|
|
- if (this.getComponentAppSecret() == null) {
|
|
|
- System.out.println("ComponentAppSecret is null, please set ComponentAppSecret");
|
|
|
- }
|
|
|
- if (this.getComponentVerifyTicket() == null) {
|
|
|
- System.out.println("ComponentVerifyTicket is null, please set ComponentVerifyTicket");
|
|
|
+ log.warn("getVerifyTicketCacheKey is null,please set component appid");
|
|
|
+ throw new NullPointerException("verifyTicketCacheKey or ComponentAppSecret or ComponentVerifyTicket is null");
|
|
|
}
|
|
|
+
|
|
|
ComponentInfo componentInfo = new ComponentInfo(getComponentAppId(), getComponentAppSecret(), getComponentVerifyTicket());
|
|
|
+ System.out.println(JsonUtils.toJsonNoSort(componentInfo));
|
|
|
return WxThirdPartService.COMPONENT_TOKEN_KEY_PREFIX + Md5Util.md5(JsonUtils.toJsonNoSort(componentInfo));
|
|
|
}
|
|
|
|
|
|
|
|
|
public String getAuthorizerTokenCacheKey(AuthorizerInfo authorizerInfo) {
|
|
|
-
|
|
|
+ System.out.println(JsonUtils.toJsonNoSort(authorizerInfo));
|
|
|
return WxThirdPartService.COMPONENT_TOKEN_KEY_PREFIX + Md5Util.md5(JsonUtils.toJsonNoSort(authorizerInfo));
|
|
|
}
|
|
|
|
|
@@ -89,4 +98,147 @@ public class WxOpenInRedisBookConfigStorage extends WxOpenInRedisConfigStorage {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ public void updateComponentVerifyTicketFromPhp() {
|
|
|
+ //s:95:"ticket@@@1CdDrkNz3kKKABPA9msNC3OqfjsBELmW7vhJzo0wwti2F5xzbNK0eGyGQkx2NmHO_sBkGRFPnDbZJovXmksh9w";
|
|
|
+ String verifyTicketCacheKey = getVerifyTicketCacheKey();
|
|
|
+ String phpComponentVerifyTicket = redisService.get(verifyTicketCacheKey);
|
|
|
+ Long expiresTime = redisService.getExpiresTime(verifyTicketCacheKey);
|
|
|
+ if (phpComponentVerifyTicket == null) {
|
|
|
+ throw new NullPointerException("phpComponentVerifyTicket is null ");
|
|
|
+ }
|
|
|
+ int begin = phpComponentVerifyTicket.indexOf("ticket@@@");
|
|
|
+ int end = phpComponentVerifyTicket.indexOf(";");
|
|
|
+ String substring = phpComponentVerifyTicket.substring(begin, end - 1);
|
|
|
+ setComponentVerifyTicket(substring, expiresTime);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void updateComponentAccessTokenFromPhp() {
|
|
|
+ // a:2:{s:22:"component_access_token";s:157:"48_YOUUo8XLX1_R7jUvlDsnm1oxowmYcHcRVkdw8LrSLVMRbQlfbD8dWHwobcEWmINKZM-5s6vFdr_9dDSgBALEuBQ1XyTH3VjZrDKIat-UccLXSSs4x5uVUaNIjMbW4o0m13Y8HrBBVm1pcjE-WYLaAFAEGT";s:10:"expires_in";i:7200;}
|
|
|
+ String componentTokenCacheKey = getComponentTokenCacheKey();
|
|
|
+ System.out.println(componentTokenCacheKey);
|
|
|
+ String phpComponentToken = redisService.get(componentTokenCacheKey);
|
|
|
+ if (StringUtils.isNotEmpty(phpComponentToken)) {
|
|
|
+ String componentToken = parsePhpAccessToken(phpComponentToken);
|
|
|
+ setComponentAccessToken(componentToken);
|
|
|
+ updateComponentAccessToken(componentToken, redisService.getExpiresTime(componentTokenCacheKey).intValue());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private String parsePhpAccessToken(String phpComponentToken) {
|
|
|
+ String[] split = phpComponentToken.split(";");
|
|
|
+ String s = split[1];
|
|
|
+ String[] split1 = s.split(":");
|
|
|
+ String s1 = split1[2];
|
|
|
+ String componentToken = s1.replace("\"", "");
|
|
|
+ return componentToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String phpComponentVerifyTicket = "s:95:\"ticket@@@1CdDrkNz3kKKABPA9msNC3OqfjsBELmW7vhJzo0wwti2F5xzbNK0eGyGQkx2NmHO_sBkGRFPnDbZJovXmksh9w\";";
|
|
|
+ if (phpComponentVerifyTicket == null) {
|
|
|
+ throw new NullPointerException("phpComponentVerifyTicket is null ");
|
|
|
+ }
|
|
|
+ int begin = phpComponentVerifyTicket.indexOf("ticket@@@");
|
|
|
+ int end = phpComponentVerifyTicket.indexOf(";");
|
|
|
+ String substring = phpComponentVerifyTicket.substring(begin, end - 1);
|
|
|
+ System.out.println(substring);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void updateAllAuthorizerAccessToken() {
|
|
|
+ List<AdminConfig> list = adminConfigService.selectByPlatformId(platform.getId());
|
|
|
+ if (list == null) {
|
|
|
+ throw new NullPointerException("WxOpenInRedisBookConfigStorage adminConfigList is null,please set it first");
|
|
|
+ }
|
|
|
+ updateAllAuthorizerAccessToken(list);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void updateAllAuthorizerAccessToken(List<AdminConfig> list) {
|
|
|
+ for (AdminConfig adminConfig : list) {
|
|
|
+
|
|
|
+ updateAuthorizerAccessTokenFromPhp(adminConfig);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public boolean isComponentVerifyTicketExpired(){
|
|
|
+ Long expiresTime = redisService.getExpiresTime(this.componentVerifyTicketKey);
|
|
|
+ return expiresTime == null || expiresTime < 10L;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setComponentVerifyTicket(String componentVerifyTicket, long second) {
|
|
|
+ redisService.set(this.componentVerifyTicketKey, componentVerifyTicket, second);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getComponentVerifyTicket() {
|
|
|
+ String componentVerifyTicket = super.getComponentVerifyTicket();
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(componentVerifyTicket) || isComponentVerifyTicketExpired()) {
|
|
|
+ updateComponentVerifyTicketFromPhp();
|
|
|
+ }
|
|
|
+ return super.getComponentVerifyTicket();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isComponentAccessTokenExpired() {
|
|
|
+ boolean componentAccessTokenExpired = super.isComponentAccessTokenExpired();
|
|
|
+ if (componentAccessTokenExpired) {
|
|
|
+ updateComponentAccessTokenFromPhp();
|
|
|
+ }
|
|
|
+ return super.isComponentAccessTokenExpired();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isAuthorizerAccessTokenExpired(String appId) {
|
|
|
+ boolean authorizerAccessTokenExpired = super.isAuthorizerAccessTokenExpired(appId);
|
|
|
+ if (authorizerAccessTokenExpired) {
|
|
|
+ AdminConfig adminConfig = adminConfigService.selectByAppid(appId);
|
|
|
+ updateAuthorizerAccessTokenFromPhp(adminConfig);
|
|
|
+ }
|
|
|
+ return super.isAuthorizerAccessTokenExpired(appId);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateAuthorizerAccessTokenFromPhp(AdminConfig adminConfig) {
|
|
|
+ String authorizerAppid = adminConfig.getAppid();
|
|
|
+
|
|
|
+ String json = adminConfig.getJson().toString();
|
|
|
+ AdminConfig.Info info = JsonUtils.getObject(json, AdminConfig.Info.class);
|
|
|
+ AdminConfig.AuthorizationInfo authorization_info = info.getAuthorization_info();
|
|
|
+ String authorizer_refresh_token = authorization_info.getAuthorizer_refresh_token();
|
|
|
+
|
|
|
+ //update refresh_token
|
|
|
+ updateAuthorizerRefreshToken(authorizerAppid, authorizer_refresh_token);
|
|
|
+
|
|
|
+
|
|
|
+ //更新token
|
|
|
+ AuthorizerInfo authorizerInfo = new AuthorizerInfo(this.getComponentAppId(), authorizerAppid, authorizer_refresh_token);
|
|
|
+ String authorizerTokenCacheKey = getAuthorizerTokenCacheKey(authorizerInfo);
|
|
|
+ //a:2:{s:23:"authorizer_access_token";s:157:"48_7NeCGMcCP63fE7auTG3I181GaH7sNeEFWj7hRIPkGkFBo00IsrhzSlauigVUdClbNfYvpH7bdZ3Oe04_kgf8-7_QoJGEX_wvDKSWz4AFThq6idME2zjk57_9Z2HKwL-Ba7uwapgwpTPQAcsRNABiAIDQDE";s:10:"expires_in";i:7200;}
|
|
|
+
|
|
|
+ System.out.println(authorizerTokenCacheKey);
|
|
|
+ String tokenString = redisService.get(authorizerTokenCacheKey);
|
|
|
+ if (StringUtils.isEmpty(tokenString)) {
|
|
|
+ log.warn("authorizer_access_token is null in php redis ");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String token = parsePhpAccessToken(tokenString);
|
|
|
+ if (token == null) {
|
|
|
+ log.warn("AuthorizerToken is null in redis for key :" + authorizerTokenCacheKey);
|
|
|
+ } else {
|
|
|
+ updateAuthorizerAccessToken(authorizerAppid, token, redisService.getExpiresTime(authorizerTokenCacheKey).intValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|