🎨 #1377 开放平台redis存储类进行抽象重构,方便扩展,并提供redisson的实现

This commit is contained in:
yydzxz 2020-01-13 18:28:08 +08:00 committed by Binary Wang
parent 9f33b34740
commit 75421148e2
14 changed files with 527 additions and 142 deletions

View File

@ -242,6 +242,12 @@
<version>2.9.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.12.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@ -24,6 +24,11 @@
<artifactId>jedis</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -6,6 +6,7 @@ import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
@ -26,6 +27,9 @@ public class WxMpStorageAutoConfiguration {
@Autowired(required = false)
private JedisPool jedisPool;
@Autowired(required = false)
private RedissonClient redissonClient;
@Bean
@ConditionalOnMissingBean(WxMpConfigStorage.class)
public WxMpConfigStorage wxMpInMemoryConfigStorage() {

View File

@ -24,6 +24,11 @@
<artifactId>jedis</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -6,7 +6,12 @@ import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenInRedissonConfigStorage;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -28,6 +33,9 @@ public class WxOpenStorageAutoConfiguration {
@Autowired(required = false)
private JedisPool jedisPool;
@Autowired(required = false)
private RedissonClient redissonClient;
@Value("${wx.open.config-storage.redis.host:}")
private String redisHost;
@ -40,12 +48,20 @@ public class WxOpenStorageAutoConfiguration {
if (type == WxOpenProperties.StorageType.redis) {
return getWxOpenInRedisConfigStorage();
}
if (type == WxOpenProperties.StorageType.jedis){
return getWxOpenInRedisConfigStorage();
}
if (type == WxOpenProperties.StorageType.redisson){
return getWxOpenInRedissonConfigStorage();
}
return getWxOpenInMemoryConfigStorage();
}
private WxOpenInMemoryConfigStorage getWxOpenInMemoryConfigStorage() {
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage();
setWxOpenInfo(config);
config.setWxOpenInfo(properties.getAppId(),properties.getSecret(), properties.getToken(), properties.getAesKey());
return config;
}
@ -55,17 +71,21 @@ public class WxOpenStorageAutoConfiguration {
poolToUse = getJedisPool();
}
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(poolToUse);
setWxOpenInfo(config);
config.setWxOpenInfo(properties.getAppId(),properties.getSecret(), properties.getToken(), properties.getAesKey());
return config;
}
private void setWxOpenInfo(WxOpenConfigStorage config) {
config.setComponentAppId(properties.getAppId());
config.setComponentAppSecret(properties.getSecret());
config.setComponentToken(properties.getToken());
config.setComponentAesKey(properties.getAesKey());
private WxOpenInRedissonConfigStorage getWxOpenInRedissonConfigStorage(){
RedissonClient redissonClientToUse = this.redissonClient;
if(redissonClient == null){
redissonClientToUse = getRedissonClient();
}
WxOpenInRedissonConfigStorage config = new WxOpenInRedissonConfigStorage(redissonClientToUse);
config.setWxOpenInfo(properties.getAppId(),properties.getSecret(), properties.getToken(), properties.getAesKey());
return config;
}
private JedisPool getJedisPool() {
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
RedisProperties redis = storage.getRedis();
@ -90,4 +110,16 @@ public class WxOpenStorageAutoConfiguration {
redis.getTimeout(), redis.getPassword(), redis.getDatabase());
return pool;
}
private RedissonClient getRedissonClient(){
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
RedisProperties redis = storage.getRedis();
Config config = new Config();
config.useSingleServer()
.setAddress("redis://" + redis.getHost() + ":" + redis.getPort())
.setPassword(redis.getPassword());
config.setTransportMode(TransportMode.NIO);
return Redisson.create(config);
}
}

View File

@ -63,6 +63,14 @@ public class WxOpenProperties {
/**
* redis.
*/
redis
redis,
/**
* jedis.
*/
jedis,
/**
* redisson.
*/
redisson
}
}

View File

@ -87,6 +87,10 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -128,4 +128,13 @@ public interface WxOpenConfigStorage {
* @param expiresInSeconds 过期时间以秒为单位
*/
void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds);
/**
* 设置第三方平台基础信息
* @param componentAppId 第三方平台 appid
* @param componentAppSecret 第三方平台 appsecret
* @param componentToken 消息校验Token
* @param componentAesKey 消息加解密Key
*/
void setWxOpenInfo(String componentAppId, String componentAppSecret, String componentToken, String componentAesKey);
}

View File

@ -0,0 +1,47 @@
package me.chanjar.weixin.open.api.impl;
import org.apache.commons.lang3.StringUtils;
/**
* @author yangyidian
* @date 2020/01/09
**/
public abstract class AbstractWxOpenInRedisConfigStorage extends WxOpenInMemoryConfigStorage {
protected final static String COMPONENT_VERIFY_TICKET_KEY = "wechat_component_verify_ticket:";
protected final static String COMPONENT_ACCESS_TOKEN_KEY = "wechat_component_access_token:";
protected final static String AUTHORIZER_REFRESH_TOKEN_KEY = "wechat_authorizer_refresh_token:";
protected final static String AUTHORIZER_ACCESS_TOKEN_KEY = "wechat_authorizer_access_token:";
protected final static String JSAPI_TICKET_KEY = "wechat_jsapi_ticket:";
protected final static String CARD_API_TICKET_KEY = "wechat_card_api_ticket:";
/**
* redis 存储的 key 的前缀可为空
*/
protected String keyPrefix;
protected String componentVerifyTicketKey;
protected String componentAccessTokenKey;
protected String authorizerRefreshTokenKey;
protected String authorizerAccessTokenKey;
protected String jsapiTicketKey;
protected String cardApiTicket;
@Override
public void setComponentAppId(String componentAppId) {
super.setComponentAppId(componentAppId);
String prefix = StringUtils.isBlank(keyPrefix) ? "" :
(StringUtils.endsWith(keyPrefix, ":") ? keyPrefix : (keyPrefix + ":"));
componentVerifyTicketKey = prefix + COMPONENT_VERIFY_TICKET_KEY.concat(componentAppId);
componentAccessTokenKey = prefix + COMPONENT_ACCESS_TOKEN_KEY.concat(componentAppId);
authorizerRefreshTokenKey = prefix + AUTHORIZER_REFRESH_TOKEN_KEY.concat(componentAppId);
authorizerAccessTokenKey = prefix + AUTHORIZER_ACCESS_TOKEN_KEY.concat(componentAppId);
this.jsapiTicketKey = JSAPI_TICKET_KEY.concat(componentAppId);
this.cardApiTicket = CARD_API_TICKET_KEY.concat(componentAppId);
}
protected String getKey(String prefix, String appId) {
return prefix.endsWith(":") ? prefix.concat(appId) : prefix.concat(":").concat(appId);
}
}

View File

@ -2,6 +2,7 @@ package me.chanjar.weixin.open.api.impl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import lombok.Data;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.mp.bean.WxMpHostConfig;
@ -23,6 +24,7 @@ import java.util.concurrent.locks.ReentrantLock;
*
* @author <a href="https://github.com/007gzs">007</a>
*/
@Data
public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
private String componentAppId;
private String componentAppSecret;
@ -43,60 +45,7 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
private Map<String, Token> jsapiTickets = new ConcurrentHashMap<>();
private Map<String, Token> cardApiTickets = new ConcurrentHashMap<>();
@Override
public String getComponentAppId() {
return componentAppId;
}
@Override
public void setComponentAppId(String componentAppId) {
this.componentAppId = componentAppId;
}
@Override
public String getComponentAppSecret() {
return componentAppSecret;
}
@Override
public void setComponentAppSecret(String componentAppSecret) {
this.componentAppSecret = componentAppSecret;
}
@Override
public String getComponentToken() {
return componentToken;
}
@Override
public void setComponentToken(String componentToken) {
this.componentToken = componentToken;
}
@Override
public String getComponentAesKey() {
return componentAesKey;
}
@Override
public void setComponentAesKey(String componentAesKey) {
this.componentAesKey = componentAesKey;
}
@Override
public String getComponentVerifyTicket() {
return componentVerifyTicket;
}
@Override
public void setComponentVerifyTicket(String componentVerifyTicket) {
this.componentVerifyTicket = componentVerifyTicket;
}
@Override
public String getComponentAccessToken() {
return componentAccessToken;
}
@Override
public boolean isComponentAccessTokenExpired() {
@ -113,51 +62,6 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
updateComponentAccessToken(componentAccessToken.getComponentAccessToken(), componentAccessToken.getExpiresIn());
}
@Override
public String getHttpProxyHost() {
return httpProxyHost;
}
public void setHttpProxyHost(String httpProxyHost) {
this.httpProxyHost = httpProxyHost;
}
@Override
public int getHttpProxyPort() {
return httpProxyPort;
}
public void setHttpProxyPort(int httpProxyPort) {
this.httpProxyPort = httpProxyPort;
}
@Override
public String getHttpProxyUsername() {
return httpProxyUsername;
}
public void setHttpProxyUsername(String httpProxyUsername) {
this.httpProxyUsername = httpProxyUsername;
}
@Override
public String getHttpProxyPassword() {
return httpProxyPassword;
}
public void setHttpProxyPassword(String httpProxyPassword) {
this.httpProxyPassword = httpProxyPassword;
}
@Override
public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
return apacheHttpClientBuilder;
}
public ApacheHttpClientBuilder setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
return this.apacheHttpClientBuilder = apacheHttpClientBuilder;
}
@Override
public WxMpConfigStorage getWxMpConfigStorage(String appId) {
return new WxOpenInnerConfigStorage(this, appId);
@ -174,6 +78,14 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
this.componentExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L;
}
@Override
public void setWxOpenInfo(String componentAppId, String componentAppSecret, String componentToken, String componentAesKey) {
setComponentAppId(componentAppId);
setComponentAppSecret(componentAppSecret);
setComponentToken(componentToken);
setComponentAesKey(componentAesKey);
}
@Override
public boolean autoRefreshToken() {
return true;

View File

@ -1,6 +1,5 @@
package me.chanjar.weixin.open.api.impl;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.util.Pool;
@ -8,26 +7,9 @@ import redis.clients.util.Pool;
/**
* @author <a href="https://github.com/007gzs">007</a>
*/
public class WxOpenInRedisConfigStorage extends WxOpenInMemoryConfigStorage {
private final static String COMPONENT_VERIFY_TICKET_KEY = "wechat_component_verify_ticket:";
private final static String COMPONENT_ACCESS_TOKEN_KEY = "wechat_component_access_token:";
private final static String AUTHORIZER_REFRESH_TOKEN_KEY = "wechat_authorizer_refresh_token:";
private final static String AUTHORIZER_ACCESS_TOKEN_KEY = "wechat_authorizer_access_token:";
private final static String JSAPI_TICKET_KEY = "wechat_jsapi_ticket:";
private final static String CARD_API_TICKET_KEY = "wechat_card_api_ticket:";
public class WxOpenInRedisConfigStorage extends AbstractWxOpenInRedisConfigStorage {
protected final Pool<Jedis> jedisPool;
/**
* redis 存储的 key 的前缀可为空
*/
private String keyPrefix;
private String componentVerifyTicketKey;
private String componentAccessTokenKey;
private String authorizerRefreshTokenKey;
private String authorizerAccessTokenKey;
private String jsapiTicketKey;
private String cardApiTicket;
public WxOpenInRedisConfigStorage(Pool<Jedis> jedisPool) {
this.jedisPool = jedisPool;
@ -42,19 +24,6 @@ public class WxOpenInRedisConfigStorage extends WxOpenInMemoryConfigStorage {
this.jedisPool = jedisPool;
}
@Override
public void setComponentAppId(String componentAppId) {
super.setComponentAppId(componentAppId);
String prefix = StringUtils.isBlank(keyPrefix) ? "" :
(StringUtils.endsWith(keyPrefix, ":") ? keyPrefix : (keyPrefix + ":"));
componentVerifyTicketKey = prefix + COMPONENT_VERIFY_TICKET_KEY.concat(componentAppId);
componentAccessTokenKey = prefix + COMPONENT_ACCESS_TOKEN_KEY.concat(componentAppId);
authorizerRefreshTokenKey = prefix + AUTHORIZER_REFRESH_TOKEN_KEY.concat(componentAppId);
authorizerAccessTokenKey = prefix + AUTHORIZER_ACCESS_TOKEN_KEY.concat(componentAppId);
this.jsapiTicketKey = JSAPI_TICKET_KEY.concat(componentAppId);
this.cardApiTicket = CARD_API_TICKET_KEY.concat(componentAppId);
}
@Override
public String getComponentVerifyTicket() {
try (Jedis jedis = this.jedisPool.getResource()) {
@ -97,10 +66,6 @@ public class WxOpenInRedisConfigStorage extends WxOpenInMemoryConfigStorage {
}
}
private String getKey(String prefix, String appId) {
return prefix.endsWith(":") ? prefix.concat(appId) : prefix.concat(":").concat(appId);
}
@Override
public String getAuthorizerRefreshToken(String appId) {
try (Jedis jedis = this.jedisPool.getResource()) {

View File

@ -0,0 +1,128 @@
package me.chanjar.weixin.open.api.impl;
import java.util.concurrent.TimeUnit;
import org.redisson.api.RedissonClient;
/**
* @author yangyidian
* @date 2020/01/06
**/
public class WxOpenInRedissonConfigStorage extends AbstractWxOpenInRedisConfigStorage{
private RedissonClient redissonClient;
public WxOpenInRedissonConfigStorage(RedissonClient redissonClient, String keyPrefix) {
this.keyPrefix = keyPrefix;
this.redissonClient = redissonClient;
}
public WxOpenInRedissonConfigStorage(RedissonClient redissonClient) {
this.redissonClient = redissonClient;
}
@Override
public String getComponentVerifyTicket() {
Object value = redissonClient.getBucket(this.componentVerifyTicketKey).get();
return value == null ? null : value.toString();
}
@Override
public void setComponentVerifyTicket(String componentVerifyTicket) {
redissonClient.getBucket(this.componentVerifyTicketKey).set(componentVerifyTicket);
}
@Override
public String getComponentAccessToken() {
Object value = redissonClient.getBucket(this.componentAccessTokenKey).get();
return value == null ? null : value.toString();
}
@Override
public boolean isComponentAccessTokenExpired() {
return redissonClient.getBucket(this.componentAccessTokenKey).remainTimeToLive() < 2;
}
@Override
public void expireComponentAccessToken() {
redissonClient.getBucket(this.componentAccessTokenKey).expire(0, TimeUnit.SECONDS);
}
@Override
public void updateComponentAccessToken(String componentAccessToken, int expiresInSeconds) {
redissonClient.getBucket(this.componentAccessTokenKey).set(componentAccessToken, expiresInSeconds - 200, TimeUnit.SECONDS);
}
@Override
public String getAuthorizerRefreshToken(String appId) {
Object value = redissonClient.getBucket(this.getKey(this.authorizerRefreshTokenKey, appId)).get();
return value == null ? null : value.toString();
}
@Override
public void setAuthorizerRefreshToken(String appId, String authorizerRefreshToken) {
redissonClient.getBucket(this.getKey(this.authorizerRefreshTokenKey, appId)).set(authorizerRefreshToken);
}
@Override
public String getAuthorizerAccessToken(String appId) {
Object value = redissonClient.getBucket(this.getKey(this.authorizerAccessTokenKey, appId)).get();
return value == null ? null : value.toString();
}
@Override
public boolean isAuthorizerAccessTokenExpired(String appId) {
return redissonClient.getBucket(this.getKey(this.authorizerAccessTokenKey, appId)).remainTimeToLive() < 2;
}
@Override
public void expireAuthorizerAccessToken(String appId) {
redissonClient.getBucket(this.getKey(this.authorizerAccessTokenKey, appId)).expire(0, TimeUnit.SECONDS);
}
@Override
public void updateAuthorizerAccessToken(String appId, String authorizerAccessToken, int expiresInSeconds) {
redissonClient.getBucket(this.getKey(this.authorizerAccessTokenKey, appId)).set(authorizerAccessToken, expiresInSeconds - 200, TimeUnit.SECONDS);
}
@Override
public String getJsapiTicket(String appId) {
Object value = redissonClient.getBucket(this.getKey(this.jsapiTicketKey, appId)).get();
return value == null ? null : value.toString();
}
@Override
public boolean isJsapiTicketExpired(String appId) {
return redissonClient.getBucket(this.getKey(this.jsapiTicketKey, appId)).remainTimeToLive() < 2;
}
@Override
public void expireJsapiTicket(String appId) {
redissonClient.getBucket(this.getKey(this.jsapiTicketKey, appId)).expire(0, TimeUnit.SECONDS);
}
@Override
public void updateJsapiTicket(String appId, String jsapiTicket, int expiresInSeconds) {
redissonClient.getBucket(this.getKey(this.jsapiTicketKey, appId)).set(jsapiTicket, expiresInSeconds - 200, TimeUnit.SECONDS);
}
@Override
public String getCardApiTicket(String appId) {
Object value = redissonClient.getBucket(this.getKey(this.cardApiTicket, appId)).get();
return value == null ? null : value.toString();
}
@Override
public boolean isCardApiTicketExpired(String appId) {
return redissonClient.getBucket(this.getKey(this.cardApiTicket, appId)).remainTimeToLive() < 2;
}
@Override
public void expireCardApiTicket(String appId) {
redissonClient.getBucket(this.getKey(this.cardApiTicket, appId)).expire(0 ,TimeUnit.SECONDS);
}
@Override
public void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds) {
redissonClient.getBucket(this.getKey(this.cardApiTicket, appId)).set(cardApiTicket, expiresInSeconds - 200, TimeUnit.SECONDS);
}
}

View File

@ -0,0 +1,131 @@
package me.chanjar.weixin.open.api.impl;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import redis.clients.jedis.JedisPool;
public class WxOpenInRedisConfigStorageTest {
private WxOpenConfigStorage wxOpenConfigStorage;
private JedisPool pool;
@BeforeClass
public void setWxOpenConfigStorage(){
pool = new JedisPool("127.0.0.1", 6379);
this.wxOpenConfigStorage = new WxOpenInRedisConfigStorage(pool);
this.wxOpenConfigStorage.setWxOpenInfo("ComponentAppId", "ComponentAppSecret", "ComponentToken","ComponentAesKey");
this.wxOpenConfigStorage.setComponentVerifyTicket("ComponentVerifyTicket");
}
@AfterClass
public void clearResource(){
pool.close();
}
@Test
public void testGetComponentVerifyTicket() {
String componentVerifyTicket = this.wxOpenConfigStorage.getComponentVerifyTicket();
Assert.assertEquals(componentVerifyTicket, "ComponentVerifyTicket");
}
@Test
public void testSetComponentVerifyTicket() {
this.wxOpenConfigStorage.setComponentVerifyTicket("new ComponentVerifyTicket");
String componentVerifyTicket = this.wxOpenConfigStorage.getComponentVerifyTicket();
Assert.assertEquals(componentVerifyTicket, "new ComponentVerifyTicket");
}
@Test
public void testIsComponentAccessTokenExpired() {
String responseContent = "{\"component_access_token\": \"new componentAccessToken\", \"expires_in\": 10000}";
WxOpenComponentAccessToken componentAccessToken = WxOpenComponentAccessToken.fromJson(responseContent);
this.wxOpenConfigStorage.updateComponentAccessToken(componentAccessToken);
boolean expired = this.wxOpenConfigStorage.isComponentAccessTokenExpired();
Assert.assertEquals(expired, false);
this.wxOpenConfigStorage.expireComponentAccessToken();
expired = this.wxOpenConfigStorage.isComponentAccessTokenExpired();
Assert.assertEquals(expired, true);
}
@Test
public void testGetAuthorizerRefreshToken() {
String appid = "appid1";
this.wxOpenConfigStorage.setAuthorizerRefreshToken(appid, "AuthorizerRefreshToken 1");
String authorizerAccessToken = this.wxOpenConfigStorage.getAuthorizerRefreshToken(appid);
Assert.assertEquals(authorizerAccessToken, "AuthorizerRefreshToken 1");
this.wxOpenConfigStorage.setAuthorizerRefreshToken(appid, "AuthorizerRefreshToken 2");
authorizerAccessToken = this.wxOpenConfigStorage.getAuthorizerRefreshToken(appid);
Assert.assertEquals(authorizerAccessToken, "AuthorizerRefreshToken 2");
}
@Test
public void testGetAuthorizerAccessToken() {
String appid = "appid1";
String responseContent = "{\"authorizer_access_token\": \"new authorizer_access_token\",\"expires_in\": 100000}";
WxOpenAuthorizerAccessToken wxOpenAuthorizerAccessToken = WxOpenAuthorizerAccessToken.fromJson(responseContent);
this.wxOpenConfigStorage.updateAuthorizerAccessToken(appid, wxOpenAuthorizerAccessToken);
String authorizerAccessToken = this.wxOpenConfigStorage.getAuthorizerAccessToken(appid);
Assert.assertEquals(authorizerAccessToken, "new authorizer_access_token");
}
@Test
public void testIsAuthorizerAccessTokenExpired() {
String appid = "appid1";
String responseContent = "{\"authorizer_access_token\": \"new authorizer_access_token\",\"expires_in\": 100000}";
WxOpenAuthorizerAccessToken wxOpenAuthorizerAccessToken = WxOpenAuthorizerAccessToken.fromJson(responseContent);
this.wxOpenConfigStorage.updateAuthorizerAccessToken(appid, wxOpenAuthorizerAccessToken);
String authorizerAccessToken = this.wxOpenConfigStorage.getAuthorizerAccessToken(appid);
Assert.assertEquals(authorizerAccessToken, "new authorizer_access_token");
boolean expired = this.wxOpenConfigStorage.isAuthorizerAccessTokenExpired(appid);
Assert.assertEquals(expired, false);
this.wxOpenConfigStorage.expireAuthorizerAccessToken(appid);
expired = this.wxOpenConfigStorage.isAuthorizerAccessTokenExpired(appid);
Assert.assertEquals(expired, true);
}
@Test
public void testGetJsapiTicket() {
String appid = "appid1";
this.wxOpenConfigStorage.updateJsapiTicket(appid, "jsapiTicket", 100000);
String jsapiTicket = this.wxOpenConfigStorage.getJsapiTicket(appid);
Assert.assertEquals(jsapiTicket, "jsapiTicket");
boolean expired = this.wxOpenConfigStorage.isJsapiTicketExpired(appid);
Assert.assertEquals(expired, false);
this.wxOpenConfigStorage.expireJsapiTicket(appid);
jsapiTicket = this.wxOpenConfigStorage.getJsapiTicket(appid);
Assert.assertEquals(jsapiTicket, null);
expired = this.wxOpenConfigStorage.isJsapiTicketExpired(appid);
Assert.assertEquals(expired, true);
}
@Test
public void testGetCardApiTicket() {
String appid = "appid1";
this.wxOpenConfigStorage.updateCardApiTicket(appid, "new CardApiTicket", 10000);
String cardApiTicket = this.wxOpenConfigStorage.getCardApiTicket(appid);
Assert.assertEquals(cardApiTicket, "new CardApiTicket");
boolean expired = this.wxOpenConfigStorage.isCardApiTicketExpired(appid);
Assert.assertEquals(expired, false);
this.wxOpenConfigStorage.expireCardApiTicket(appid);
expired = this.wxOpenConfigStorage.isCardApiTicketExpired(appid);
Assert.assertEquals(expired, true);
}
}

View File

@ -0,0 +1,129 @@
package me.chanjar.weixin.open.api.impl;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class WxOpenInRedissonConfigStorageTest {
private WxOpenConfigStorage wxOpenConfigStorage;
@BeforeClass
public void setWxOpenConfigStorage(){
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379")
.setDatabase(0);
config.setTransportMode(TransportMode.NIO);
RedissonClient redisson = Redisson.create(config);
this.wxOpenConfigStorage = new WxOpenInRedissonConfigStorage(redisson);
this.wxOpenConfigStorage.setWxOpenInfo("ComponentAppId", "ComponentAppSecret", "ComponentToken","ComponentAesKey");
this.wxOpenConfigStorage.setComponentVerifyTicket("ComponentVerifyTicket");
}
@Test
public void testGetComponentVerifyTicket() {
String componentVerifyTicket = this.wxOpenConfigStorage.getComponentVerifyTicket();
Assert.assertEquals(componentVerifyTicket, "ComponentVerifyTicket");
}
@Test
public void testSetComponentVerifyTicket() {
this.wxOpenConfigStorage.setComponentVerifyTicket("new ComponentVerifyTicket");
String componentVerifyTicket = this.wxOpenConfigStorage.getComponentVerifyTicket();
Assert.assertEquals(componentVerifyTicket, "new ComponentVerifyTicket");
}
@Test
public void testIsComponentAccessTokenExpired() {
String responseContent = "{\"component_access_token\": \"new componentAccessToken\", \"expires_in\": 10000}";
WxOpenComponentAccessToken componentAccessToken = WxOpenComponentAccessToken.fromJson(responseContent);
this.wxOpenConfigStorage.updateComponentAccessToken(componentAccessToken);
boolean expired = this.wxOpenConfigStorage.isComponentAccessTokenExpired();
Assert.assertEquals(expired, false);
this.wxOpenConfigStorage.expireComponentAccessToken();
expired = this.wxOpenConfigStorage.isComponentAccessTokenExpired();
Assert.assertEquals(expired, true);
}
@Test
public void testGetAuthorizerRefreshToken() {
String appid = "appid1";
this.wxOpenConfigStorage.setAuthorizerRefreshToken(appid, "AuthorizerRefreshToken 1");
String authorizerAccessToken = this.wxOpenConfigStorage.getAuthorizerRefreshToken(appid);
Assert.assertEquals(authorizerAccessToken, "AuthorizerRefreshToken 1");
this.wxOpenConfigStorage.setAuthorizerRefreshToken(appid, "AuthorizerRefreshToken 2");
authorizerAccessToken = this.wxOpenConfigStorage.getAuthorizerRefreshToken(appid);
Assert.assertEquals(authorizerAccessToken, "AuthorizerRefreshToken 2");
}
@Test
public void testGetAuthorizerAccessToken() {
String appid = "appid1";
String responseContent = "{\"authorizer_access_token\": \"new authorizer_access_token\",\"expires_in\": 100000}";
WxOpenAuthorizerAccessToken wxOpenAuthorizerAccessToken = WxOpenAuthorizerAccessToken.fromJson(responseContent);
this.wxOpenConfigStorage.updateAuthorizerAccessToken(appid, wxOpenAuthorizerAccessToken);
String authorizerAccessToken = this.wxOpenConfigStorage.getAuthorizerAccessToken(appid);
Assert.assertEquals(authorizerAccessToken, "new authorizer_access_token");
}
@Test
public void testIsAuthorizerAccessTokenExpired() {
String appid = "appid1";
String responseContent = "{\"authorizer_access_token\": \"new authorizer_access_token\",\"expires_in\": 100000}";
WxOpenAuthorizerAccessToken wxOpenAuthorizerAccessToken = WxOpenAuthorizerAccessToken.fromJson(responseContent);
this.wxOpenConfigStorage.updateAuthorizerAccessToken(appid, wxOpenAuthorizerAccessToken);
String authorizerAccessToken = this.wxOpenConfigStorage.getAuthorizerAccessToken(appid);
Assert.assertEquals(authorizerAccessToken, "new authorizer_access_token");
boolean expired = this.wxOpenConfigStorage.isAuthorizerAccessTokenExpired(appid);
Assert.assertEquals(expired, false);
this.wxOpenConfigStorage.expireAuthorizerAccessToken(appid);
expired = this.wxOpenConfigStorage.isAuthorizerAccessTokenExpired(appid);
Assert.assertEquals(expired, true);
}
@Test
public void testGetJsapiTicket() {
String appid = "appid1";
this.wxOpenConfigStorage.updateJsapiTicket(appid, "jsapiTicket", 100000);
String jsapiTicket = this.wxOpenConfigStorage.getJsapiTicket(appid);
Assert.assertEquals(jsapiTicket, "jsapiTicket");
boolean expired = this.wxOpenConfigStorage.isJsapiTicketExpired(appid);
Assert.assertEquals(expired, false);
this.wxOpenConfigStorage.expireJsapiTicket(appid);
jsapiTicket = this.wxOpenConfigStorage.getJsapiTicket(appid);
Assert.assertEquals(jsapiTicket, null);
expired = this.wxOpenConfigStorage.isJsapiTicketExpired(appid);
Assert.assertEquals(expired, true);
}
@Test
public void testGetCardApiTicket() {
String appid = "appid1";
this.wxOpenConfigStorage.updateCardApiTicket(appid, "new CardApiTicket", 10000);
String cardApiTicket = this.wxOpenConfigStorage.getCardApiTicket(appid);
Assert.assertEquals(cardApiTicket, "new CardApiTicket");
boolean expired = this.wxOpenConfigStorage.isCardApiTicketExpired(appid);
Assert.assertEquals(expired, false);
this.wxOpenConfigStorage.expireCardApiTicket(appid);
expired = this.wxOpenConfigStorage.isCardApiTicketExpired(appid);
Assert.assertEquals(expired, true);
}
}