1
1
package me .chanjar .weixin .mp .api ;
2
2
3
- import me .chanjar .weixin .common .bean .WxAccessToken ;
4
- import me .chanjar .weixin .common .util .ToStringUtils ;
5
- import me .chanjar .weixin .common .util .http .ApacheHttpClientBuilder ;
6
3
import redis .clients .jedis .Jedis ;
7
4
8
- import javax .net .ssl .SSLContext ;
9
- import java .io .File ;
10
- import java .util .concurrent .locks .Lock ;
11
- import java .util .concurrent .locks .ReentrantLock ;
12
-
13
5
/**
14
6
* 基于Redis的微信配置provider
15
7
*
16
8
* @author lly835
17
9
*/
18
- public class WxMpInRedisConfigStorage implements WxMpConfigStorage {
10
+ @ SuppressWarnings ("hiding" )
11
+ public class WxMpInRedisConfigStorage extends WxMpInMemoryConfigStorage {
19
12
20
13
private final static String ACCESS_TOKEN_KEY = "wechat_access_token_" ;
21
14
22
15
private final static String JSAPI_TICKET_KEY = "wechat_jsapi_ticket_" ;
23
16
24
17
private final static String CARDAPI_TICKET_KEY = "wechat_cardapi_ticket_" ;
25
18
26
- protected volatile String appId ;
27
- protected volatile String secret ;
28
- protected volatile String partnerId ;
29
- protected volatile String partnerKey ;
30
- protected volatile String notifyURL ;
31
- protected volatile String tradeType ;
32
- protected volatile String token ;
33
- protected volatile String accessToken ;
34
- protected volatile String aesKey ;
35
- protected volatile long expiresTime ;
36
-
37
- protected volatile String oauth2redirectUri ;
38
-
39
- protected volatile String httpProxyHost ;
40
- protected volatile int httpProxyPort ;
41
- protected volatile String httpProxyUsername ;
42
- protected volatile String httpProxyPassword ;
43
-
44
- protected volatile String jsapiTicket ;
45
- protected volatile long jsapiTicketExpiresTime ;
46
-
47
- protected volatile String cardApiTicket ;
48
- protected volatile long cardApiTicketExpiresTime ;
49
-
50
- protected Lock accessTokenLock = new ReentrantLock ();
51
- protected Lock jsapiTicketLock = new ReentrantLock ();
52
- protected Lock cardApiTicketLock = new ReentrantLock ();
53
-
54
- /**
55
- * 临时文件目录
56
- */
57
- protected volatile File tmpDirFile ;
58
-
59
- protected volatile SSLContext sslContext ;
60
-
61
- protected volatile ApacheHttpClientBuilder apacheHttpClientBuilder ;
62
-
63
19
protected Jedis jedis ;
64
20
65
21
@ Override
66
22
public String getAccessToken () {
67
23
return jedis .get (ACCESS_TOKEN_KEY .concat (appId ));
68
24
}
69
25
70
- @ Override
71
- public Lock getAccessTokenLock () {
72
- return this .accessTokenLock ;
73
- }
74
-
75
26
@ Override
76
27
public boolean isAccessTokenExpired () {
77
28
return getAccessToken () == null ? true : false ;
78
29
}
79
30
80
- @ Override
81
- public synchronized void updateAccessToken (WxAccessToken accessToken ) {
82
- updateAccessToken (accessToken .getAccessToken (), accessToken .getExpiresIn ());
83
- }
84
-
85
- @ Override
31
+ @ Override
86
32
public synchronized void updateAccessToken (String accessToken , int expiresInSeconds ) {
87
- this .accessToken = accessToken ;
88
- this .expiresTime = System .currentTimeMillis () + (expiresInSeconds - 200 ) * 1000L ;
89
33
jedis .set (ACCESS_TOKEN_KEY .concat (appId ), accessToken );
90
34
jedis .expire (ACCESS_TOKEN_KEY .concat (appId ), expiresInSeconds - 200 );
91
35
}
92
36
93
37
@ Override
94
38
public void expireAccessToken () {
95
- this . expiresTime = 0 ;
39
+ jedis . expire ( ACCESS_TOKEN_KEY . concat ( appId ), 0 ) ;
96
40
}
97
41
98
42
@ Override
99
43
public String getJsapiTicket () {
100
- return this .jsapiTicket ;
101
- }
102
-
103
- public void setJsapiTicket (String jsapiTicket ) {
104
- this .jsapiTicket = jsapiTicket ;
105
- }
106
-
107
- @ Override
108
- public Lock getJsapiTicketLock () {
109
- return this .jsapiTicketLock ;
44
+ return jedis .get (JSAPI_TICKET_KEY .concat (appId ));
110
45
}
111
46
112
47
@ Override
@@ -116,194 +51,40 @@ public boolean isJsapiTicketExpired() {
116
51
117
52
@ Override
118
53
public synchronized void updateJsapiTicket (String jsapiTicket , int expiresInSeconds ) {
119
- this .jsapiTicket = jsapiTicket ;
120
- // 预留200秒的时间
121
- this .jsapiTicketExpiresTime = System .currentTimeMillis () + (expiresInSeconds - 200 ) * 1000L ;
122
- jedis .set (JSAPI_TICKET_KEY .concat (appId ), accessToken );
54
+ jedis .set (JSAPI_TICKET_KEY .concat (appId ), jsapiTicket );
123
55
jedis .expire (JSAPI_TICKET_KEY .concat (appId ), expiresInSeconds - 200 );
124
56
}
125
57
126
58
@ Override
127
59
public void expireJsapiTicket () {
128
- this . jsapiTicketExpiresTime = 0 ;
60
+ jedis . expire ( JSAPI_TICKET_KEY . concat ( appId ), 0 ) ;
129
61
}
130
62
131
63
/**
132
64
* 卡券api_ticket
133
65
*/
134
66
@ Override
135
67
public String getCardApiTicket () {
136
- return this .cardApiTicket ;
137
- }
138
-
139
- @ Override
140
- public Lock getCardApiTicketLock () {
141
- return this .cardApiTicketLock ;
68
+ return jedis .get (CARDAPI_TICKET_KEY .concat (appId ));
142
69
}
143
70
144
71
@ Override
145
72
public boolean isCardApiTicketExpired () {
146
- return System . currentTimeMillis () > this . cardApiTicketExpiresTime ;
73
+ return getCardApiTicket () == null ? true : false ;
147
74
}
148
75
149
76
@ Override
150
77
public synchronized void updateCardApiTicket (String cardApiTicket , int expiresInSeconds ) {
151
- this .cardApiTicket = cardApiTicket ;
152
- // 预留200秒的时间
153
- this .cardApiTicketExpiresTime = System .currentTimeMillis () + (expiresInSeconds - 200 ) * 1000L ;
154
- jedis .set (CARDAPI_TICKET_KEY .concat (appId ), accessToken );
78
+ jedis .set (CARDAPI_TICKET_KEY .concat (appId ), cardApiTicket );
155
79
jedis .expire (CARDAPI_TICKET_KEY .concat (appId ), expiresInSeconds - 200 );
156
80
}
157
81
158
82
@ Override
159
83
public void expireCardApiTicket () {
160
- this .cardApiTicketExpiresTime = 0 ;
161
- }
162
-
163
- @ Override
164
- public String getAppId () {
165
- return this .appId ;
166
- }
167
-
168
- public void setAppId (String appId ) {
169
- this .appId = appId ;
170
- }
171
-
172
- @ Override
173
- public String getSecret () {
174
- return this .secret ;
175
- }
176
-
177
- public void setSecret (String secret ) {
178
- this .secret = secret ;
179
- }
180
-
181
- @ Override
182
- public String getToken () {
183
- return this .token ;
184
- }
185
-
186
- public void setToken (String token ) {
187
- this .token = token ;
188
- }
189
-
190
- @ Override
191
- public long getExpiresTime () {
192
- return this .expiresTime ;
193
- }
194
-
195
- public void setExpiresTime (long expiresTime ) {
196
- this .expiresTime = expiresTime ;
197
- }
198
-
199
- @ Override
200
- public String getAesKey () {
201
- return this .aesKey ;
202
- }
203
-
204
- public void setAesKey (String aesKey ) {
205
- this .aesKey = aesKey ;
206
- }
207
-
208
- @ Override
209
- public String getOauth2redirectUri () {
210
- return this .oauth2redirectUri ;
211
- }
212
-
213
- public void setOauth2redirectUri (String oauth2redirectUri ) {
214
- this .oauth2redirectUri = oauth2redirectUri ;
215
- }
216
-
217
- @ Override
218
- public String getHttpProxyHost () {
219
- return this .httpProxyHost ;
220
- }
221
-
222
- public void setHttpProxyHost (String httpProxyHost ) {
223
- this .httpProxyHost = httpProxyHost ;
224
- }
225
-
226
- @ Override
227
- public int getHttpProxyPort () {
228
- return this .httpProxyPort ;
229
- }
230
-
231
- public void setHttpProxyPort (int httpProxyPort ) {
232
- this .httpProxyPort = httpProxyPort ;
233
- }
234
-
235
- @ Override
236
- public String getHttpProxyUsername () {
237
- return this .httpProxyUsername ;
238
- }
239
-
240
- public void setHttpProxyUsername (String httpProxyUsername ) {
241
- this .httpProxyUsername = httpProxyUsername ;
242
- }
243
-
244
- @ Override
245
- public String getHttpProxyPassword () {
246
- return this .httpProxyPassword ;
247
- }
248
-
249
- public void setHttpProxyPassword (String httpProxyPassword ) {
250
- this .httpProxyPassword = httpProxyPassword ;
251
- }
252
-
253
- @ Override
254
- public String toString () {
255
- return ToStringUtils .toSimpleString (this );
256
- }
257
-
258
- public void setPartnerId (String partnerId ) {
259
- this .partnerId = partnerId ;
260
- }
261
-
262
- public void setPartnerKey (String partnerKey ) {
263
- this .partnerKey = partnerKey ;
264
- }
265
-
266
-
267
- public String getNotifyURL () {
268
- return notifyURL ;
269
- }
270
-
271
- public void setNotifyURL (String notifyURL ) {
272
- this .notifyURL = notifyURL ;
273
- }
274
-
275
- public String getTradeType () {
276
- return tradeType ;
277
- }
278
-
279
- public void setTradeType (String tradeType ) {
280
- this .tradeType = tradeType ;
281
- }
282
-
283
- @ Override
284
- public File getTmpDirFile () {
285
- return this .tmpDirFile ;
286
- }
287
-
288
- public void setTmpDirFile (File tmpDirFile ) {
289
- this .tmpDirFile = tmpDirFile ;
290
- }
291
-
292
- @ Override
293
- public ApacheHttpClientBuilder getApacheHttpClientBuilder () {
294
- return this .apacheHttpClientBuilder ;
295
- }
296
-
297
- public void setApacheHttpClientBuilder (ApacheHttpClientBuilder apacheHttpClientBuilder ) {
298
- this .apacheHttpClientBuilder = apacheHttpClientBuilder ;
299
- }
300
-
301
- @ Override
302
- public boolean autoRefreshToken () {
303
- return true ;
84
+ jedis .expire (CARDAPI_TICKET_KEY .concat (appId ), 0 );
304
85
}
305
86
306
87
public void setJedis (Jedis jedis ) {
307
- this .jedis = jedis ;
308
- }
309
- }
88
+ this .jedis = jedis ;
89
+ }
90
+ }
0 commit comments