添加可选[RedisTokenStorager&添加缓存token时的前缀wx
This commit is contained in:
parent
dd61596a38
commit
ef5fb006f0
@ -533,3 +533,10 @@
|
|||||||
+ weixin4j-base:调整Pay3Api退款方法名为 refundApply
|
+ weixin4j-base:调整Pay3Api退款方法名为 refundApply
|
||||||
|
|
||||||
+ weixin4j-base:调整Pay3Api#refundApply参数个数
|
+ weixin4j-base:调整Pay3Api#refundApply参数个数
|
||||||
|
|
||||||
|
|
||||||
|
* 2015-12-10
|
||||||
|
|
||||||
|
+ 添加可选[RedisTokenStorager](weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/RedisTokenStorager.java)
|
||||||
|
|
||||||
|
+ 添加缓存token时的前缀`wx`
|
||||||
|
|||||||
@ -35,5 +35,11 @@
|
|||||||
<version>4.0.30.Final</version>
|
<version>4.0.30.Final</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>redis.clients</groupId>
|
||||||
|
<artifactId>jedis</artifactId>
|
||||||
|
<version>2.6.0</version>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
@ -8,4 +8,4 @@
|
|||||||
|
|
||||||
* FileTokenStorager 是系统默认的token存储策略实现
|
* FileTokenStorager 是系统默认的token存储策略实现
|
||||||
|
|
||||||
* RedisTokenStorager 如果服务器支持redis,推荐使用(需要自己添加jar包和[java类](https://github.com/foxinmy/weixin4j/wiki/%E7%94%A8redis%E4%BF%9D%E5%AD%98token))
|
* RedisTokenStorager 如果服务器支持redis,推荐使用(需要自己添加jedis包)
|
||||||
|
|||||||
@ -0,0 +1,86 @@
|
|||||||
|
package com.foxinmy.weixin4j.token;
|
||||||
|
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
|
import redis.clients.jedis.JedisPool;
|
||||||
|
import redis.clients.jedis.JedisPoolConfig;
|
||||||
|
|
||||||
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
|
import com.foxinmy.weixin4j.util.StringUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用REDIS保存TOKEN
|
||||||
|
*
|
||||||
|
* @className RedisTokenStorager
|
||||||
|
* @author jy
|
||||||
|
* @date 2015年1月9日
|
||||||
|
* @since JDK 1.6
|
||||||
|
*/
|
||||||
|
public class RedisTokenStorager implements TokenStorager {
|
||||||
|
|
||||||
|
private JedisPool jedisPool;
|
||||||
|
|
||||||
|
public final static int MAX_TOTAL = 50;
|
||||||
|
public final static int MAX_IDLE = 5;
|
||||||
|
public final static int MAX_WAIT_MILLIS = 2000;
|
||||||
|
public final static boolean TEST_ON_BORROW = false;
|
||||||
|
public final static boolean TEST_ON_RETURN = true;
|
||||||
|
|
||||||
|
public RedisTokenStorager() {
|
||||||
|
this("localhost", 6379);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RedisTokenStorager(String host, int port) {
|
||||||
|
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
|
||||||
|
jedisPoolConfig.setMaxTotal(MAX_TOTAL);
|
||||||
|
jedisPoolConfig.setMaxIdle(MAX_IDLE);
|
||||||
|
jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS);
|
||||||
|
jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW);
|
||||||
|
jedisPoolConfig.setTestOnReturn(TEST_ON_RETURN);
|
||||||
|
this.jedisPool = new JedisPool(jedisPoolConfig, host, port);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RedisTokenStorager(String host, int port,
|
||||||
|
JedisPoolConfig jedisPoolConfig) {
|
||||||
|
this(new JedisPool(jedisPoolConfig, host, port));
|
||||||
|
}
|
||||||
|
|
||||||
|
public RedisTokenStorager(JedisPool jedisPool) {
|
||||||
|
this.jedisPool = jedisPool;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Token lookup(String cacheKey) throws WeixinException {
|
||||||
|
Jedis jedis = null;
|
||||||
|
try {
|
||||||
|
jedis = jedisPool.getResource();
|
||||||
|
String accessToken = jedis.get(cacheKey);
|
||||||
|
if (!StringUtil.isBlank(accessToken)) {
|
||||||
|
return new Token(accessToken);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (jedis != null) {
|
||||||
|
jedis.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void caching(String cacheKey, Token token) throws WeixinException {
|
||||||
|
Jedis jedis = null;
|
||||||
|
try {
|
||||||
|
jedis = jedisPool.getResource();
|
||||||
|
if (token.getExpiresIn() > 0) {
|
||||||
|
jedis.setex(cacheKey, (int) token.getExpiresIn(),
|
||||||
|
token.getAccessToken());
|
||||||
|
} else {
|
||||||
|
jedis.set(cacheKey, token.getAccessToken());
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (jedis != null) {
|
||||||
|
jedis.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -24,7 +24,7 @@ public interface TokenCreator {
|
|||||||
* 创建token
|
* 创建token
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* @throws MeetException
|
* @throws WeixinException
|
||||||
*/
|
*/
|
||||||
public Token createToken() throws WeixinException;
|
public Token createToken() throws WeixinException;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -96,7 +96,7 @@ weixin4j.properties说明
|
|||||||
// weixinPayProxy = new WeixinPayProxy(weixinAccount);
|
// weixinPayProxy = new WeixinPayProxy(weixinAccount);
|
||||||
weixinPayProxy.orderQuery(idQuery);
|
weixinPayProxy.orderQuery(idQuery);
|
||||||
|
|
||||||
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenStorager`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用[RedisTokenStorager](https://github.com/foxinmy/weixin4j/wiki/%E7%94%A8redis%E4%BF%9D%E5%AD%98token).
|
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenStorager`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用[RedisTokenStorager](../weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/RedisTokenStorager.java).
|
||||||
>
|
>
|
||||||
> WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenStorager());
|
> WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenStorager());
|
||||||
|
|
||||||
|
|||||||
@ -42,7 +42,7 @@ public class WeixinJSTicketCreator implements TokenCreator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCacheKey() {
|
public String getCacheKey() {
|
||||||
return String.format("mp_jsticket_%s", appid);
|
return String.format("wx_mp_jsticket_%s", appid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -40,7 +40,7 @@ public class WeixinTokenCreator implements TokenCreator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCacheKey() {
|
public String getCacheKey() {
|
||||||
return String.format("mp_token_%s", appid);
|
return String.format("wx_mp_token_%s", appid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -55,7 +55,7 @@ public class MediaTest extends TokenTest {
|
|||||||
@Test
|
@Test
|
||||||
public void download1() throws WeixinException, IOException {
|
public void download1() throws WeixinException, IOException {
|
||||||
MediaDownloadResult content = mediaApi.downloadMedia(
|
MediaDownloadResult content = mediaApi.downloadMedia(
|
||||||
"V4GvAetzQQjj_ECx1Siqvccgm80a32tk1gRusOBYMk8Uouybl-CwSkGk8w7wKgvI", false);
|
"nD05mhmkW-SHVS8NfWSxdzJi-6VnVF5YSP5hmMWnKJUxLK5czAWtN4NhVmgEfVoe", false);
|
||||||
Assert.assertTrue(content != null);
|
Assert.assertTrue(content != null);
|
||||||
System.err.println(content);
|
System.err.println(content);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -100,7 +100,7 @@ weixin4j.properties说明
|
|||||||
//weixinSuiteProxy = new WeixinSuiteProxy(suiteId,suiteSecret);
|
//weixinSuiteProxy = new WeixinSuiteProxy(suiteId,suiteSecret);
|
||||||
weixinSuiteProxy.api().getOAuthInfo(authCorpid);
|
weixinSuiteProxy.api().getOAuthInfo(authCorpid);
|
||||||
|
|
||||||
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenStorager`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用[RedisTokenStorager](https://github.com/foxinmy/weixin4j/wiki/%E7%94%A8redis%E4%BF%9D%E5%AD%98token).
|
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenStorager`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用[RedisTokenStorager](../weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/RedisTokenStorager.java).
|
||||||
|
|
||||||
> WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenStorager());
|
> WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenStorager());
|
||||||
|
|
||||||
|
|||||||
@ -42,7 +42,7 @@ public class WeixinSuitePreCodeCreator implements TokenCreator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCacheKey() {
|
public String getCacheKey() {
|
||||||
return String.format("qy_suite_precode_%s", suiteId);
|
return String.format("wx_qy_suite_precode_%s", suiteId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -36,7 +36,7 @@ public class WeixinSuiteTokenCreator implements TokenCreator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCacheKey() {
|
public String getCacheKey() {
|
||||||
return String.format("qy_suite_token_%s", ticketHolder.getSuiteId());
|
return String.format("wx_qy_suite_token_%s", ticketHolder.getSuiteId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -42,7 +42,7 @@ public class WeixinTokenSuiteCreator implements TokenCreator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCacheKey() {
|
public String getCacheKey() {
|
||||||
return String.format("qy_token_suite_%s:%s",
|
return String.format("wx_qy_token_suite_%s:%s",
|
||||||
perCodeHolder.getSuiteId(), perCodeHolder.getAuthCorpId()
|
perCodeHolder.getSuiteId(), perCodeHolder.getAuthCorpId()
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|||||||
@ -41,7 +41,7 @@ public class WeixinJSTicketCreator implements TokenCreator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCacheKey() {
|
public String getCacheKey() {
|
||||||
return String.format("qy_jsticket_%s", corpid);
|
return String.format("wx_qy_jsticket_%s", corpid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -40,7 +40,7 @@ public class WeixinProviderTokenCreator implements TokenCreator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCacheKey() {
|
public String getCacheKey() {
|
||||||
return String.format("qy_provider_token_%s", corpid);
|
return String.format("wx_qy_provider_token_%s", corpid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -40,7 +40,7 @@ public class WeixinTokenCreator implements TokenCreator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCacheKey() {
|
public String getCacheKey() {
|
||||||
return String.format("qy_token_%s", corpid);
|
return String.format("wx_qy_token_%s", corpid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user