mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-24 16:18:51 +08:00
🐛 #1473 修复多个小程序获取redis里的access_token冲突问题
This commit is contained in:
parent
faf07b3d27
commit
9bad0ff280
@ -14,6 +14,10 @@ public class WxMaRedisConfigImpl extends AbstractWxMaRedisConfig {
|
|||||||
|
|
||||||
private JedisPool jedisPool;
|
private JedisPool jedisPool;
|
||||||
|
|
||||||
|
private static final String ACCESS_TOKEN_KEY = "wa:access_token:";
|
||||||
|
|
||||||
|
private String accessTokenKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JedisPool 在此配置类是必须项,使用 WxMaRedisConfigImpl(JedisPool) 构造方法来构造实例
|
* JedisPool 在此配置类是必须项,使用 WxMaRedisConfigImpl(JedisPool) 构造方法来构造实例
|
||||||
*/
|
*/
|
||||||
@ -37,4 +41,41 @@ public class WxMaRedisConfigImpl extends AbstractWxMaRedisConfig {
|
|||||||
protected Jedis getJedis() {
|
protected Jedis getJedis() {
|
||||||
return jedisPool.getResource();
|
return jedisPool.getResource();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每个公众号生成独有的存储key.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setAppid(String appId) {
|
||||||
|
super.setAppid(appId);
|
||||||
|
this.accessTokenKey = ACCESS_TOKEN_KEY.concat(appId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAccessToken() {
|
||||||
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
|
return jedis.get(this.accessTokenKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAccessTokenExpired() {
|
||||||
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
|
return jedis.ttl(accessTokenKey) < 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
|
||||||
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
|
jedis.setex(this.accessTokenKey, expiresInSeconds - 200, accessToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void expireAccessToken() {
|
||||||
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
|
jedis.expire(this.accessTokenKey, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user