Skip to content

Commit 7542114

Browse files
yydzxzbinarywang
authored andcommitted
🎨 #1377 开放平台redis存储类进行抽象重构,方便扩展,并提供redisson的实现
1 parent 9f33b34 commit 7542114

File tree

14 files changed

+527
-142
lines changed

14 files changed

+527
-142
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@
242242
<version>2.9.0</version>
243243
<scope>provided</scope>
244244
</dependency>
245+
<dependency>
246+
<groupId>org.redisson</groupId>
247+
<artifactId>redisson</artifactId>
248+
<version>3.12.0</version>
249+
<scope>provided</scope>
250+
</dependency>
245251
<dependency>
246252
<groupId>org.projectlombok</groupId>
247253
<artifactId>lombok</artifactId>

spring-boot-starters/wx-java-mp-spring-boot-starter/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
<artifactId>jedis</artifactId>
2525
<scope>compile</scope>
2626
</dependency>
27+
<dependency>
28+
<groupId>org.redisson</groupId>
29+
<artifactId>redisson</artifactId>
30+
<scope>compile</scope>
31+
</dependency>
2732
</dependencies>
2833

2934
<build>

spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
77
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
88
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
9+
import org.redisson.api.RedissonClient;
910
import org.springframework.beans.factory.annotation.Autowired;
1011
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1112
import org.springframework.context.annotation.Bean;
@@ -26,6 +27,9 @@ public class WxMpStorageAutoConfiguration {
2627
@Autowired(required = false)
2728
private JedisPool jedisPool;
2829

30+
@Autowired(required = false)
31+
private RedissonClient redissonClient;
32+
2933
@Bean
3034
@ConditionalOnMissingBean(WxMpConfigStorage.class)
3135
public WxMpConfigStorage wxMpInMemoryConfigStorage() {

spring-boot-starters/wx-java-open-spring-boot-starter/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
<artifactId>jedis</artifactId>
2525
<scope>compile</scope>
2626
</dependency>
27+
<dependency>
28+
<groupId>org.redisson</groupId>
29+
<artifactId>redisson</artifactId>
30+
<scope>compile</scope>
31+
</dependency>
2732
</dependencies>
2833

2934
<build>

spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/WxOpenStorageAutoConfiguration.java

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
77
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
88
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
9+
import me.chanjar.weixin.open.api.impl.WxOpenInRedissonConfigStorage;
910
import org.apache.commons.lang3.StringUtils;
11+
import org.redisson.Redisson;
12+
import org.redisson.api.RedissonClient;
13+
import org.redisson.config.Config;
14+
import org.redisson.config.TransportMode;
1015
import org.springframework.beans.factory.annotation.Autowired;
1116
import org.springframework.beans.factory.annotation.Value;
1217
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -28,6 +33,9 @@ public class WxOpenStorageAutoConfiguration {
2833
@Autowired(required = false)
2934
private JedisPool jedisPool;
3035

36+
@Autowired(required = false)
37+
private RedissonClient redissonClient;
38+
3139
@Value("${wx.open.config-storage.redis.host:}")
3240
private String redisHost;
3341

@@ -40,12 +48,20 @@ public WxOpenConfigStorage wxOpenConfigStorage() {
4048
if (type == WxOpenProperties.StorageType.redis) {
4149
return getWxOpenInRedisConfigStorage();
4250
}
51+
52+
if (type == WxOpenProperties.StorageType.jedis){
53+
return getWxOpenInRedisConfigStorage();
54+
}
55+
56+
if (type == WxOpenProperties.StorageType.redisson){
57+
return getWxOpenInRedissonConfigStorage();
58+
}
4359
return getWxOpenInMemoryConfigStorage();
4460
}
4561

4662
private WxOpenInMemoryConfigStorage getWxOpenInMemoryConfigStorage() {
4763
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage();
48-
setWxOpenInfo(config);
64+
config.setWxOpenInfo(properties.getAppId(),properties.getSecret(), properties.getToken(), properties.getAesKey());
4965
return config;
5066
}
5167

@@ -55,17 +71,21 @@ private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() {
5571
poolToUse = getJedisPool();
5672
}
5773
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(poolToUse);
58-
setWxOpenInfo(config);
74+
config.setWxOpenInfo(properties.getAppId(),properties.getSecret(), properties.getToken(), properties.getAesKey());
5975
return config;
6076
}
6177

62-
private void setWxOpenInfo(WxOpenConfigStorage config) {
63-
config.setComponentAppId(properties.getAppId());
64-
config.setComponentAppSecret(properties.getSecret());
65-
config.setComponentToken(properties.getToken());
66-
config.setComponentAesKey(properties.getAesKey());
78+
private WxOpenInRedissonConfigStorage getWxOpenInRedissonConfigStorage(){
79+
RedissonClient redissonClientToUse = this.redissonClient;
80+
if(redissonClient == null){
81+
redissonClientToUse = getRedissonClient();
82+
}
83+
WxOpenInRedissonConfigStorage config = new WxOpenInRedissonConfigStorage(redissonClientToUse);
84+
config.setWxOpenInfo(properties.getAppId(),properties.getSecret(), properties.getToken(), properties.getAesKey());
85+
return config;
6786
}
6887

88+
6989
private JedisPool getJedisPool() {
7090
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
7191
RedisProperties redis = storage.getRedis();
@@ -90,4 +110,16 @@ private JedisPool getJedisPool() {
90110
redis.getTimeout(), redis.getPassword(), redis.getDatabase());
91111
return pool;
92112
}
113+
114+
private RedissonClient getRedissonClient(){
115+
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
116+
RedisProperties redis = storage.getRedis();
117+
118+
Config config = new Config();
119+
config.useSingleServer()
120+
.setAddress("redis://" + redis.getHost() + ":" + redis.getPort())
121+
.setPassword(redis.getPassword());
122+
config.setTransportMode(TransportMode.NIO);
123+
return Redisson.create(config);
124+
}
93125
}

spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/WxOpenProperties.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public enum StorageType {
6363
/**
6464
* redis.
6565
*/
66-
redis
66+
redis,
67+
/**
68+
* jedis.
69+
*/
70+
jedis,
71+
/**
72+
* redisson.
73+
*/
74+
redisson
6775
}
6876
}

weixin-java-open/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
<groupId>org.projectlombok</groupId>
8888
<artifactId>lombok</artifactId>
8989
</dependency>
90+
<dependency>
91+
<groupId>org.redisson</groupId>
92+
<artifactId>redisson</artifactId>
93+
</dependency>
9094
</dependencies>
9195

9296
<build>

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenConfigStorage.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,13 @@ public interface WxOpenConfigStorage {
128128
* @param expiresInSeconds 过期时间,以秒为单位
129129
*/
130130
void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds);
131+
132+
/**
133+
* 设置第三方平台基础信息
134+
* @param componentAppId 第三方平台 appid
135+
* @param componentAppSecret 第三方平台 appsecret
136+
* @param componentToken 消息校验Token
137+
* @param componentAesKey 消息加解密Key
138+
*/
139+
void setWxOpenInfo(String componentAppId, String componentAppSecret, String componentToken, String componentAesKey);
131140
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package me.chanjar.weixin.open.api.impl;
2+
3+
4+
import org.apache.commons.lang3.StringUtils;
5+
6+
/**
7+
* @author yangyidian
8+
* @date 2020/01/09
9+
**/
10+
public abstract class AbstractWxOpenInRedisConfigStorage extends WxOpenInMemoryConfigStorage {
11+
protected final static String COMPONENT_VERIFY_TICKET_KEY = "wechat_component_verify_ticket:";
12+
protected final static String COMPONENT_ACCESS_TOKEN_KEY = "wechat_component_access_token:";
13+
14+
protected final static String AUTHORIZER_REFRESH_TOKEN_KEY = "wechat_authorizer_refresh_token:";
15+
protected final static String AUTHORIZER_ACCESS_TOKEN_KEY = "wechat_authorizer_access_token:";
16+
protected final static String JSAPI_TICKET_KEY = "wechat_jsapi_ticket:";
17+
protected final static String CARD_API_TICKET_KEY = "wechat_card_api_ticket:";
18+
19+
/**
20+
* redis 存储的 key 的前缀,可为空
21+
*/
22+
protected String keyPrefix;
23+
protected String componentVerifyTicketKey;
24+
protected String componentAccessTokenKey;
25+
protected String authorizerRefreshTokenKey;
26+
protected String authorizerAccessTokenKey;
27+
protected String jsapiTicketKey;
28+
protected String cardApiTicket;
29+
30+
@Override
31+
public void setComponentAppId(String componentAppId) {
32+
super.setComponentAppId(componentAppId);
33+
String prefix = StringUtils.isBlank(keyPrefix) ? "" :
34+
(StringUtils.endsWith(keyPrefix, ":") ? keyPrefix : (keyPrefix + ":"));
35+
componentVerifyTicketKey = prefix + COMPONENT_VERIFY_TICKET_KEY.concat(componentAppId);
36+
componentAccessTokenKey = prefix + COMPONENT_ACCESS_TOKEN_KEY.concat(componentAppId);
37+
authorizerRefreshTokenKey = prefix + AUTHORIZER_REFRESH_TOKEN_KEY.concat(componentAppId);
38+
authorizerAccessTokenKey = prefix + AUTHORIZER_ACCESS_TOKEN_KEY.concat(componentAppId);
39+
this.jsapiTicketKey = JSAPI_TICKET_KEY.concat(componentAppId);
40+
this.cardApiTicket = CARD_API_TICKET_KEY.concat(componentAppId);
41+
}
42+
43+
protected String getKey(String prefix, String appId) {
44+
return prefix.endsWith(":") ? prefix.concat(appId) : prefix.concat(":").concat(appId);
45+
}
46+
47+
}

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInMemoryConfigStorage.java

Lines changed: 10 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import cn.binarywang.wx.miniapp.config.WxMaConfig;
5+
import lombok.Data;
56
import me.chanjar.weixin.common.bean.WxAccessToken;
67
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
78
import me.chanjar.weixin.mp.bean.WxMpHostConfig;
@@ -23,6 +24,7 @@
2324
*
2425
* @author <a href="https://github.com/007gzs">007</a>
2526
*/
27+
@Data
2628
public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
2729
private String componentAppId;
2830
private String componentAppSecret;
@@ -43,60 +45,7 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
4345
private Map<String, Token> jsapiTickets = new ConcurrentHashMap<>();
4446
private Map<String, Token> cardApiTickets = new ConcurrentHashMap<>();
4547

46-
@Override
47-
public String getComponentAppId() {
48-
return componentAppId;
49-
}
50-
51-
@Override
52-
public void setComponentAppId(String componentAppId) {
53-
this.componentAppId = componentAppId;
54-
}
55-
56-
@Override
57-
public String getComponentAppSecret() {
58-
return componentAppSecret;
59-
}
60-
61-
@Override
62-
public void setComponentAppSecret(String componentAppSecret) {
63-
this.componentAppSecret = componentAppSecret;
64-
}
65-
66-
@Override
67-
public String getComponentToken() {
68-
return componentToken;
69-
}
70-
71-
@Override
72-
public void setComponentToken(String componentToken) {
73-
this.componentToken = componentToken;
74-
}
75-
76-
@Override
77-
public String getComponentAesKey() {
78-
return componentAesKey;
79-
}
8048

81-
@Override
82-
public void setComponentAesKey(String componentAesKey) {
83-
this.componentAesKey = componentAesKey;
84-
}
85-
86-
@Override
87-
public String getComponentVerifyTicket() {
88-
return componentVerifyTicket;
89-
}
90-
91-
@Override
92-
public void setComponentVerifyTicket(String componentVerifyTicket) {
93-
this.componentVerifyTicket = componentVerifyTicket;
94-
}
95-
96-
@Override
97-
public String getComponentAccessToken() {
98-
return componentAccessToken;
99-
}
10049

10150
@Override
10251
public boolean isComponentAccessTokenExpired() {
@@ -113,51 +62,6 @@ public void updateComponentAccessToken(WxOpenComponentAccessToken componentAcces
11362
updateComponentAccessToken(componentAccessToken.getComponentAccessToken(), componentAccessToken.getExpiresIn());
11463
}
11564

116-
@Override
117-
public String getHttpProxyHost() {
118-
return httpProxyHost;
119-
}
120-
121-
public void setHttpProxyHost(String httpProxyHost) {
122-
this.httpProxyHost = httpProxyHost;
123-
}
124-
125-
@Override
126-
public int getHttpProxyPort() {
127-
return httpProxyPort;
128-
}
129-
130-
public void setHttpProxyPort(int httpProxyPort) {
131-
this.httpProxyPort = httpProxyPort;
132-
}
133-
134-
@Override
135-
public String getHttpProxyUsername() {
136-
return httpProxyUsername;
137-
}
138-
139-
public void setHttpProxyUsername(String httpProxyUsername) {
140-
this.httpProxyUsername = httpProxyUsername;
141-
}
142-
143-
@Override
144-
public String getHttpProxyPassword() {
145-
return httpProxyPassword;
146-
}
147-
148-
public void setHttpProxyPassword(String httpProxyPassword) {
149-
this.httpProxyPassword = httpProxyPassword;
150-
}
151-
152-
@Override
153-
public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
154-
return apacheHttpClientBuilder;
155-
}
156-
157-
public ApacheHttpClientBuilder setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
158-
return this.apacheHttpClientBuilder = apacheHttpClientBuilder;
159-
}
160-
16165
@Override
16266
public WxMpConfigStorage getWxMpConfigStorage(String appId) {
16367
return new WxOpenInnerConfigStorage(this, appId);
@@ -174,6 +78,14 @@ public void updateComponentAccessToken(String componentAccessToken, int expiresI
17478
this.componentExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L;
17579
}
17680

81+
@Override
82+
public void setWxOpenInfo(String componentAppId, String componentAppSecret, String componentToken, String componentAesKey) {
83+
setComponentAppId(componentAppId);
84+
setComponentAppSecret(componentAppSecret);
85+
setComponentToken(componentToken);
86+
setComponentAesKey(componentAesKey);
87+
}
88+
17789
@Override
17890
public boolean autoRefreshToken() {
17991
return true;

0 commit comments

Comments
 (0)