@@ -1,26 +1,35 @@ | |||
# 使用说明 | |||
1. 在自己的Spring Boot项目里,引入maven依赖 | |||
```xml | |||
# wx-java-miniapp-spring-boot-starter | |||
## 快速开始 | |||
1. 引入依赖 | |||
```xml | |||
<dependency> | |||
<groupId>com.github.binarywang</groupId> | |||
<artifactId>wx-java-miniapp-spring-boot-starter</artifactId> | |||
<version>${version}</version> | |||
</dependency> | |||
``` | |||
2. 添加配置(application.yml) | |||
```yml | |||
wx: | |||
miniapp: | |||
appid: 111 | |||
secret: 111 | |||
token: 111 | |||
aesKey: 111 | |||
msgDataFormat: JSON | |||
``` | |||
``` | |||
2. 添加配置(application.properties) | |||
```properties | |||
# 公众号配置(必填) | |||
wx.miniapp.appid = appId | |||
wx.miniapp.secret = @secret | |||
wx.miniapp.token = @token | |||
wx.miniapp.aesKey = @aesKey | |||
wx.miniapp.msgDataFormat = @msgDataFormat # 消息格式,XML或者JSON. | |||
# 存储配置redis(可选) | |||
# 注意: 指定redis.host值后不会使用容器注入的redis连接(JedisPool) | |||
wx.miniapp.config-storage.type = jedis # 配置类型: memory(默认), jedis, redistemplate | |||
wx.miniapp.config-storage.key-prefix = wa # 相关redis前缀配置: wa(默认) | |||
wx.miniapp.config-storage.redis.host = 127.0.0.1 | |||
wx.miniapp.config-storage.redis.port = 6379 | |||
# http客户端配置 | |||
wx.miniapp.config-storage.http-client-type=httpclient # http客户端类型: httpclient(默认) | |||
wx.miniapp.config-storage.http-proxy-host= | |||
wx.miniapp.config-storage.http-proxy-port= | |||
wx.miniapp.config-storage.http-proxy-username= | |||
wx.miniapp.config-storage.http-proxy-password= | |||
``` | |||
3. 自动注入的类型 | |||
- `WxMaService` | |||
- `WxMaConfig` | |||
@@ -1,4 +1,4 @@ | |||
# wx-java-mp-starter | |||
# wx-java-mp-spring-boot-starter | |||
## 快速开始 | |||
1. 引入依赖 | |||
```xml | |||
@@ -27,9 +27,9 @@ | |||
wx.mp.config-storage.http-proxy-username= | |||
wx.mp.config-storage.http-proxy-password= | |||
``` | |||
3. 支持自动注入的类型 | |||
`WxMpService`以及~~相关的服务类, 比如: `wxMpService.getXxxService`。~~ | |||
3. 自动注入的类型 | |||
- `WxMpService`以及~~相关的服务类, 比如: `wxMpService.getXxxService`。~~ | |||
- `WxMpConfigStorage` | |||
@@ -9,26 +9,27 @@ | |||
</dependency> | |||
``` | |||
2. 添加配置(application.properties) | |||
``` | |||
# 开放平台配置(必填) | |||
wx.open.appId = @appId | |||
wx.open.secret = @secret | |||
wx.open.token = @token | |||
wx.open.aesKey = @aesKey | |||
# 存储配置redis(可选), 优先使用(wx.open.config-storage.redis)配置的redis, 支持自定注入的JedisPool | |||
wx.open.config-storage.type = redis # 可选值, memory(默认), redis | |||
wx.open.config-storage.redis.host = 127.0.0.1 | |||
wx.open.config-storage.redis.port = 6379 | |||
```properties | |||
# 公众号配置(必填) | |||
wx.open.appId = appId | |||
wx.open.secret = @secret | |||
wx.open.token = @token | |||
wx.open.aesKey = @aesKey | |||
# 存储配置redis(可选) | |||
# 优先注入容器的(JedisPool, RedissonClient), 当配置了wx.open.config-storage.redis.host, 不会使用容器注入redis连接配置 | |||
wx.open.config-storage.type = redis # 配置类型: memory(默认), redis(jedis), jedis, redisson, redistemplate | |||
wx.open.config-storage.key-prefix = wx # 相关redis前缀配置: wx(默认) | |||
wx.open.config-storage.redis.host = 127.0.0.1 | |||
wx.open.config-storage.redis.port = 6379 | |||
# http客户端配置 | |||
wx.open.config-storage.http-client-type=httpclient # http客户端类型: httpclient(默认) | |||
wx.open.config-storage.http-proxy-host= | |||
wx.open.config-storage.http-proxy-port= | |||
wx.open.config-storage.http-proxy-username= | |||
wx.open.config-storage.http-proxy-password= | |||
``` | |||
3. 支持自动注入的类型: `WxOpenService, WxOpenMessageRouter, WxOpenComponentService` | |||
4. 覆盖自动配置: 自定义注入的bean会覆盖自动注入的 | |||
- WxOpenConfigStorage | |||
- WxOpenService | |||
@@ -22,12 +22,18 @@ | |||
<dependency> | |||
<groupId>redis.clients</groupId> | |||
<artifactId>jedis</artifactId> | |||
<scope>compile</scope> | |||
<scope>provided</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.redisson</groupId> | |||
<artifactId>redisson</artifactId> | |||
<scope>compile</scope> | |||
<scope>provided</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.springframework.data</groupId> | |||
<artifactId>spring-data-redis</artifactId> | |||
<version>${spring.boot.version}</version> | |||
<scope>provided</scope> | |||
</dependency> | |||
</dependencies> | |||
@@ -3,20 +3,23 @@ package com.binarywang.spring.starter.wxjava.open.config; | |||
import com.binarywang.spring.starter.wxjava.open.properties.RedisProperties; | |||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties; | |||
import lombok.RequiredArgsConstructor; | |||
import me.chanjar.weixin.common.redis.JedisWxRedisOps; | |||
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps; | |||
import me.chanjar.weixin.common.redis.RedissonWxRedisOps; | |||
import me.chanjar.weixin.common.redis.WxRedisOps; | |||
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; | |||
import org.springframework.context.ApplicationContext; | |||
import org.springframework.context.annotation.Bean; | |||
import org.springframework.context.annotation.Configuration; | |||
import org.springframework.data.redis.core.StringRedisTemplate; | |||
import redis.clients.jedis.JedisPool; | |||
import redis.clients.jedis.JedisPoolConfig; | |||
@@ -29,15 +32,7 @@ import redis.clients.jedis.JedisPoolConfig; | |||
@RequiredArgsConstructor | |||
public class WxOpenStorageAutoConfiguration { | |||
private final WxOpenProperties properties; | |||
@Autowired(required = false) | |||
private JedisPool jedisPool; | |||
@Autowired(required = false) | |||
private RedissonClient redissonClient; | |||
@Value("${wx.open.config-storage.redis.host:}") | |||
private String redisHost; | |||
private final ApplicationContext applicationContext; | |||
@Bean | |||
@ConditionalOnMissingBean(WxOpenConfigStorage.class) | |||
@@ -45,43 +40,63 @@ public class WxOpenStorageAutoConfiguration { | |||
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage(); | |||
WxOpenProperties.StorageType type = storage.getType(); | |||
if (type == WxOpenProperties.StorageType.redis) { | |||
return getWxOpenInRedisConfigStorage(); | |||
WxOpenInMemoryConfigStorage config; | |||
if (type == WxOpenProperties.StorageType.redis || type == WxOpenProperties.StorageType.jedis) { | |||
config = getWxOpenInRedisConfigStorage(); | |||
} else if (type == WxOpenProperties.StorageType.redisson) { | |||
config = getWxOpenInRedissonConfigStorage(); | |||
} else if (type == WxOpenProperties.StorageType.redistemplate) { | |||
config = getWxOpenInRedisTemplateConfigStorage(); | |||
} else { | |||
config = getWxOpenInMemoryConfigStorage(); | |||
} | |||
if (type == WxOpenProperties.StorageType.jedis) { | |||
return getWxOpenInRedisConfigStorage(); | |||
} | |||
if (type == WxOpenProperties.StorageType.redisson) { | |||
return getWxOpenInRedissonConfigStorage(); | |||
WxOpenProperties.ConfigStorage configStorageProperties = properties.getConfigStorage(); | |||
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey()); | |||
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost()); | |||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername()); | |||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword()); | |||
if (configStorageProperties.getHttpProxyPort() != null) { | |||
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort()); | |||
} | |||
return getWxOpenInMemoryConfigStorage(); | |||
return config; | |||
} | |||
private WxOpenInMemoryConfigStorage getWxOpenInMemoryConfigStorage() { | |||
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage(); | |||
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey()); | |||
return config; | |||
} | |||
private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() { | |||
JedisPool poolToUse = jedisPool; | |||
if (jedisPool == null || StringUtils.isNotEmpty(redisHost)) { | |||
poolToUse = getJedisPool(); | |||
RedisProperties redisProperties = properties.getConfigStorage().getRedis(); | |||
JedisPool jedisPool; | |||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) { | |||
jedisPool = getJedisPool(); | |||
} else { | |||
jedisPool = applicationContext.getBean(JedisPool.class); | |||
} | |||
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(poolToUse, properties.getConfigStorage().getKeyPrefix()); | |||
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey()); | |||
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool); | |||
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix()); | |||
return config; | |||
} | |||
private WxOpenInRedissonConfigStorage getWxOpenInRedissonConfigStorage() { | |||
RedissonClient redissonClientToUse = this.redissonClient; | |||
if (redissonClient == null) { | |||
redissonClientToUse = getRedissonClient(); | |||
private WxOpenInRedisConfigStorage getWxOpenInRedissonConfigStorage() { | |||
RedisProperties redisProperties = properties.getConfigStorage().getRedis(); | |||
RedissonClient redissonClient; | |||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) { | |||
redissonClient = getRedissonClient(); | |||
} else { | |||
redissonClient = applicationContext.getBean(RedissonClient.class); | |||
} | |||
WxOpenInRedissonConfigStorage config = new WxOpenInRedissonConfigStorage(redissonClientToUse, properties.getConfigStorage().getKeyPrefix()); | |||
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey()); | |||
WxRedisOps redisOps = new RedissonWxRedisOps(redissonClient); | |||
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix()); | |||
return config; | |||
} | |||
private WxOpenInRedisConfigStorage getWxOpenInRedisTemplateConfigStorage() { | |||
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class); | |||
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate); | |||
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix()); | |||
return config; | |||
} | |||
@@ -16,7 +16,7 @@ public class RedisProperties implements Serializable { | |||
/** | |||
* 主机地址. | |||
*/ | |||
private String host = "127.0.0.1"; | |||
private String host; | |||
/** | |||
* 端口号. | |||
@@ -40,7 +40,7 @@ public class WxOpenProperties { | |||
private String aesKey; | |||
/** | |||
* 存储策略, memory, redis. | |||
* 存储策略. | |||
*/ | |||
private ConfigStorage configStorage = new ConfigStorage(); | |||
@@ -49,11 +49,45 @@ public class WxOpenProperties { | |||
public static class ConfigStorage implements Serializable { | |||
private static final long serialVersionUID = 4815731027000065434L; | |||
/** | |||
* 存储类型. | |||
*/ | |||
private StorageType type = memory; | |||
/** | |||
* 指定key前缀. | |||
*/ | |||
private String keyPrefix = "wx"; | |||
/** | |||
* redis连接配置. | |||
*/ | |||
private RedisProperties redis = new RedisProperties(); | |||
private String keyPrefix; | |||
/** | |||
* http客户端类型. | |||
*/ | |||
private HttpClientType httpClientType = HttpClientType.httpclient; | |||
/** | |||
* http代理主机. | |||
*/ | |||
private String httpProxyHost; | |||
/** | |||
* http代理端口. | |||
*/ | |||
private Integer httpProxyPort; | |||
/** | |||
* http代理用户名. | |||
*/ | |||
private String httpProxyUsername; | |||
/** | |||
* http代理密码. | |||
*/ | |||
private String httpProxyPassword; | |||
} | |||
@@ -73,6 +107,17 @@ public class WxOpenProperties { | |||
/** | |||
* redisson. | |||
*/ | |||
redisson | |||
redisson, | |||
/** | |||
* redistemplate | |||
*/ | |||
redistemplate | |||
} | |||
public enum HttpClientType { | |||
/** | |||
* HttpClient. | |||
*/ | |||
httpclient | |||
} | |||
} |
@@ -3,7 +3,7 @@ package me.chanjar.weixin.common.redis; | |||
import lombok.RequiredArgsConstructor; | |||
import me.chanjar.weixin.common.util.locks.JedisDistributedLock; | |||
import redis.clients.jedis.Jedis; | |||
import redis.clients.jedis.JedisPool; | |||
import redis.clients.util.Pool; | |||
import java.util.concurrent.TimeUnit; | |||
import java.util.concurrent.locks.Lock; | |||
@@ -11,7 +11,7 @@ import java.util.concurrent.locks.Lock; | |||
@RequiredArgsConstructor | |||
public class JedisWxRedisOps implements WxRedisOps { | |||
private final JedisPool jedisPool; | |||
private final Pool<Jedis> jedisPool; | |||
@Override | |||
public String getValue(String key) { | |||
@@ -1,10 +1,12 @@ | |||
package me.chanjar.weixin.open.api.impl; | |||
import me.chanjar.weixin.common.util.locks.JedisDistributedLock; | |||
import lombok.NonNull; | |||
import me.chanjar.weixin.common.redis.JedisWxRedisOps; | |||
import me.chanjar.weixin.common.redis.WxRedisOps; | |||
import redis.clients.jedis.Jedis; | |||
import redis.clients.jedis.JedisPool; | |||
import redis.clients.util.Pool; | |||
import java.util.concurrent.TimeUnit; | |||
import java.util.concurrent.locks.Lock; | |||
/** | |||
@@ -12,163 +14,127 @@ import java.util.concurrent.locks.Lock; | |||
*/ | |||
public class WxOpenInRedisConfigStorage extends AbstractWxOpenInRedisConfigStorage { | |||
protected final Pool<Jedis> jedisPool; | |||
private final WxRedisOps redisOps; | |||
public WxOpenInRedisConfigStorage(Pool<Jedis> jedisPool) { | |||
this.jedisPool = jedisPool; | |||
this(jedisPool, null); | |||
} | |||
public WxOpenInRedisConfigStorage(Pool<Jedis> jedisPool, String keyPrefix) { | |||
this.jedisPool = jedisPool; | |||
this.keyPrefix = keyPrefix; | |||
public WxOpenInRedisConfigStorage(@NonNull Pool<Jedis> jedisPool, String keyPrefix) { | |||
this(new JedisWxRedisOps(jedisPool), keyPrefix); | |||
} | |||
public WxOpenInRedisConfigStorage(JedisPool jedisPool) { | |||
this.jedisPool = jedisPool; | |||
public WxOpenInRedisConfigStorage(@NonNull WxRedisOps redisOps, String keyPrefix) { | |||
this.redisOps = redisOps; | |||
this.keyPrefix = keyPrefix; | |||
} | |||
@Override | |||
public String getComponentVerifyTicket() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.get(this.componentVerifyTicketKey); | |||
} | |||
return redisOps.getValue(this.componentVerifyTicketKey); | |||
} | |||
@Override | |||
public void setComponentVerifyTicket(String componentVerifyTicket) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.set(this.componentVerifyTicketKey, componentVerifyTicket); | |||
} | |||
redisOps.setValue(this.componentVerifyTicketKey, componentVerifyTicket, Integer.MAX_VALUE, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public String getComponentAccessToken() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.get(this.componentAccessTokenKey); | |||
} | |||
return redisOps.getValue(this.componentAccessTokenKey); | |||
} | |||
@Override | |||
public boolean isComponentAccessTokenExpired() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.ttl(this.componentAccessTokenKey) < 2; | |||
} | |||
Long expire = redisOps.getExpire(this.componentAccessTokenKey); | |||
return expire == null || expire < 2; | |||
} | |||
@Override | |||
public void expireComponentAccessToken() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.expire(this.componentAccessTokenKey, 0); | |||
} | |||
redisOps.expire(this.componentAccessTokenKey, 0, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public void updateComponentAccessToken(String componentAccessToken, int expiresInSeconds) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.setex(this.componentAccessTokenKey, expiresInSeconds - 200, componentAccessToken); | |||
} | |||
redisOps.setValue(this.componentAccessTokenKey, componentAccessToken, expiresInSeconds - 200, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public String getAuthorizerRefreshToken(String appId) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.get(this.getKey(this.authorizerRefreshTokenKey, appId)); | |||
} | |||
return redisOps.getValue(this.getKey(this.authorizerRefreshTokenKey, appId)); | |||
} | |||
@Override | |||
public void setAuthorizerRefreshToken(String appId, String authorizerRefreshToken) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.set(this.getKey(this.authorizerRefreshTokenKey, appId), authorizerRefreshToken); | |||
} | |||
redisOps.setValue(this.getKey(this.authorizerRefreshTokenKey, appId), authorizerRefreshToken, 0, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public String getAuthorizerAccessToken(String appId) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.get(this.getKey(this.authorizerAccessTokenKey, appId)); | |||
} | |||
return redisOps.getValue(this.getKey(this.authorizerAccessTokenKey, appId)); | |||
} | |||
@Override | |||
public boolean isAuthorizerAccessTokenExpired(String appId) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.ttl(this.getKey(this.authorizerAccessTokenKey, appId)) < 2; | |||
} | |||
Long expire = redisOps.getExpire(this.getKey(this.authorizerAccessTokenKey, appId)); | |||
return expire == null || expire < 2; | |||
} | |||
@Override | |||
public void expireAuthorizerAccessToken(String appId) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.expire(this.getKey(this.authorizerAccessTokenKey, appId), 0); | |||
} | |||
redisOps.expire(this.getKey(this.authorizerAccessTokenKey, appId), 0, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public void updateAuthorizerAccessToken(String appId, String authorizerAccessToken, int expiresInSeconds) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.setex(this.getKey(this.authorizerAccessTokenKey, appId), expiresInSeconds - 200, authorizerAccessToken); | |||
} | |||
redisOps.setValue(this.getKey(this.authorizerAccessTokenKey, appId), authorizerAccessToken, expiresInSeconds - 200, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public String getJsapiTicket(String appId) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.get(this.getKey(this.jsapiTicketKey, appId)); | |||
} | |||
return redisOps.getValue(this.getKey(this.jsapiTicketKey, appId)); | |||
} | |||
@Override | |||
public boolean isJsapiTicketExpired(String appId) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.ttl(this.getKey(this.jsapiTicketKey, appId)) < 2; | |||
} | |||
Long expire = redisOps.getExpire(this.getKey(this.jsapiTicketKey, appId)); | |||
return expire == null || expire < 2; | |||
} | |||
@Override | |||
public void expireJsapiTicket(String appId) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.expire(this.getKey(this.jsapiTicketKey, appId), 0); | |||
} | |||
redisOps.expire(this.getKey(this.jsapiTicketKey, appId), 0, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public void updateJsapiTicket(String appId, String jsapiTicket, int expiresInSeconds) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.setex(this.getKey(this.jsapiTicketKey, appId), expiresInSeconds - 200, jsapiTicket); | |||
} | |||
redisOps.setValue(this.getKey(this.jsapiTicketKey, appId), jsapiTicket, expiresInSeconds - 200, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public String getCardApiTicket(String appId) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.get(this.getKey(this.cardApiTicket, appId)); | |||
} | |||
return redisOps.getValue(this.getKey(this.cardApiTicket, appId)); | |||
} | |||
@Override | |||
public boolean isCardApiTicketExpired(String appId) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.ttl(this.getKey(this.cardApiTicket, appId)) < 2; | |||
} | |||
Long expire = redisOps.getExpire(this.getKey(this.cardApiTicket, appId)); | |||
return expire == null || expire < 2; | |||
} | |||
@Override | |||
public void expireCardApiTicket(String appId) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.expire(this.getKey(this.cardApiTicket, appId), 0); | |||
} | |||
redisOps.expire(this.getKey(this.cardApiTicket, appId), 0, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.setex(this.getKey(this.cardApiTicket, appId), expiresInSeconds - 200, cardApiTicket); | |||
} | |||
redisOps.setValue(this.getKey(this.cardApiTicket, appId), cardApiTicket, expiresInSeconds - 200, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public Lock getLockByKey(String key) { | |||
return new JedisDistributedLock(jedisPool, getKey(lockKey, key)); | |||
return redisOps.getLock(key); | |||
} | |||
} |
@@ -1,128 +1,140 @@ | |||
package me.chanjar.weixin.open.api.impl; | |||
import java.util.concurrent.TimeUnit; | |||
import lombok.NonNull; | |||
import me.chanjar.weixin.common.redis.RedissonWxRedisOps; | |||
import me.chanjar.weixin.common.redis.WxRedisOps; | |||
import org.redisson.api.RedissonClient; | |||
import java.util.concurrent.TimeUnit; | |||
import java.util.concurrent.locks.Lock; | |||
/** | |||
* @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); | |||
} | |||
public class WxOpenInRedissonConfigStorage extends AbstractWxOpenInRedisConfigStorage { | |||
private final WxRedisOps redisOps; | |||
public WxOpenInRedissonConfigStorage(@NonNull RedissonClient redissonClient, String keyPrefix) { | |||
this(new RedissonWxRedisOps(redissonClient), keyPrefix); | |||
} | |||
public WxOpenInRedissonConfigStorage(@NonNull RedissonClient redissonClient) { | |||
this(redissonClient, null); | |||
} | |||
private WxOpenInRedissonConfigStorage(@NonNull WxRedisOps redisOps, String keyPrefix) { | |||
this.redisOps = redisOps; | |||
this.keyPrefix = keyPrefix; | |||
} | |||
@Override | |||
public String getComponentVerifyTicket() { | |||
return redisOps.getValue(this.componentVerifyTicketKey); | |||
} | |||
@Override | |||
public void setComponentVerifyTicket(String componentVerifyTicket) { | |||
redisOps.setValue(this.componentVerifyTicketKey, componentVerifyTicket, Integer.MAX_VALUE, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public String getComponentAccessToken() { | |||
return redisOps.getValue(this.componentAccessTokenKey); | |||
} | |||
@Override | |||
public boolean isComponentAccessTokenExpired() { | |||
Long expire = redisOps.getExpire(this.componentAccessTokenKey); | |||
return expire == null || expire < 2; | |||
} | |||
@Override | |||
public void expireComponentAccessToken() { | |||
redisOps.expire(this.componentAccessTokenKey, 0, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public void updateComponentAccessToken(String componentAccessToken, int expiresInSeconds) { | |||
redisOps.setValue(this.componentAccessTokenKey, componentAccessToken, expiresInSeconds - 200, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public String getAuthorizerRefreshToken(String appId) { | |||
return redisOps.getValue(this.getKey(this.authorizerRefreshTokenKey, appId)); | |||
} | |||
@Override | |||
public void setAuthorizerRefreshToken(String appId, String authorizerRefreshToken) { | |||
redisOps.setValue(this.getKey(this.authorizerRefreshTokenKey, appId), authorizerRefreshToken, 0, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public String getAuthorizerAccessToken(String appId) { | |||
return redisOps.getValue(this.getKey(this.authorizerAccessTokenKey, appId)); | |||
} | |||
@Override | |||
public boolean isAuthorizerAccessTokenExpired(String appId) { | |||
Long expire = redisOps.getExpire(this.getKey(this.authorizerAccessTokenKey, appId)); | |||
return expire == null || expire < 2; | |||
} | |||
@Override | |||
public void expireAuthorizerAccessToken(String appId) { | |||
redisOps.expire(this.getKey(this.authorizerAccessTokenKey, appId), 0, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public void updateAuthorizerAccessToken(String appId, String authorizerAccessToken, int expiresInSeconds) { | |||
redisOps.setValue(this.getKey(this.authorizerAccessTokenKey, appId), authorizerAccessToken, expiresInSeconds - 200, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public String getJsapiTicket(String appId) { | |||
return redisOps.getValue(this.getKey(this.jsapiTicketKey, appId)); | |||
} | |||
@Override | |||
public boolean isJsapiTicketExpired(String appId) { | |||
Long expire = redisOps.getExpire(this.getKey(this.jsapiTicketKey, appId)); | |||
return expire == null || expire < 2; | |||
} | |||
@Override | |||
public void expireJsapiTicket(String appId) { | |||
redisOps.expire(this.getKey(this.jsapiTicketKey, appId), 0, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public void updateJsapiTicket(String appId, String jsapiTicket, int expiresInSeconds) { | |||
redisOps.setValue(this.getKey(this.jsapiTicketKey, appId), jsapiTicket, expiresInSeconds - 200, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public String getCardApiTicket(String appId) { | |||
return redisOps.getValue(this.getKey(this.cardApiTicket, appId)); | |||
} | |||
@Override | |||
public boolean isCardApiTicketExpired(String appId) { | |||
Long expire = redisOps.getExpire(this.getKey(this.cardApiTicket, appId)); | |||
return expire == null || expire < 2; | |||
} | |||
@Override | |||
public void expireCardApiTicket(String appId) { | |||
redisOps.expire(this.getKey(this.cardApiTicket, appId), 0, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds) { | |||
redisOps.setValue(this.getKey(this.cardApiTicket, appId), cardApiTicket, expiresInSeconds - 200, TimeUnit.SECONDS); | |||
} | |||
@Override | |||
public Lock getLockByKey(String key) { | |||
return redisOps.getLock(key); | |||
} | |||
} |