Skip to content

抽象AbstractWxOpenInRedisConfigStorage方便扩展,并提供redisson实现 #1377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
5 changes: 5 additions & 0 deletions spring-boot-starters/wx-java-mp-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand All @@ -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() {
Expand Down
5 changes: 5 additions & 0 deletions spring-boot-starters/wx-java-open-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
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;
Expand All @@ -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;

Expand All @@ -40,12 +48,20 @@ public WxOpenConfigStorage wxOpenConfigStorage() {
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;
}

Expand All @@ -55,17 +71,21 @@ private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() {
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();
Expand All @@ -90,4 +110,16 @@ private JedisPool getJedisPool() {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public enum StorageType {
/**
* redis.
*/
redis
redis,
/**
* jedis.
*/
jedis,
/**
* redisson.
*/
redisson
}
}
4 changes: 4 additions & 0 deletions weixin-java-open/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


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;
Expand All @@ -23,6 +24,7 @@
*
* @author <a href="https://github.com/007gzs">007</a>
*/
@Data
public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
private String componentAppId;
private String componentAppSecret;
Expand All @@ -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() {
Expand All @@ -113,51 +62,6 @@ public void updateComponentAccessToken(WxOpenComponentAccessToken componentAcces
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);
Expand All @@ -174,6 +78,14 @@ public void updateComponentAccessToken(String componentAccessToken, int expiresI
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;
Expand Down
Loading