修缮token实现机制
This commit is contained in:
parent
76ae9d58e3
commit
dea8ab6252
@ -325,4 +325,8 @@
|
|||||||
|
|
||||||
+ weixin.properties重命名为weixin4j.properties
|
+ weixin.properties重命名为weixin4j.properties
|
||||||
|
|
||||||
+ released 1.5.0!
|
+ released 1.5.0!
|
||||||
|
|
||||||
|
* 2015-06-12
|
||||||
|
|
||||||
|
+ 修缮token实现机制
|
||||||
@ -1,60 +0,0 @@
|
|||||||
package com.foxinmy.weixin4j.token;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Calendar;
|
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
|
||||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
|
||||||
import com.foxinmy.weixin4j.xml.XmlStream;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用FILE保存TOKEN
|
|
||||||
*
|
|
||||||
* @className FileTokenHolder
|
|
||||||
* @author jy
|
|
||||||
* @date 2015年1月9日
|
|
||||||
* @since JDK 1.7
|
|
||||||
* @see com.foxinmy.weixin4j.token.TokenCreator
|
|
||||||
*/
|
|
||||||
public class FileTokenHolder implements TokenHolder {
|
|
||||||
private final String tokenPath;
|
|
||||||
private final TokenCreator tokenCreator;
|
|
||||||
|
|
||||||
public FileTokenHolder(TokenCreator tokenCreator) {
|
|
||||||
this(ConfigUtil.getValue("token_path"), tokenCreator);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileTokenHolder(String tokenPath, TokenCreator tokenCreator) {
|
|
||||||
this.tokenPath = tokenPath;
|
|
||||||
this.tokenCreator = tokenCreator;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Token getToken() throws WeixinException {
|
|
||||||
File token_file = new File(String.format("%s/%s.xml", tokenPath,
|
|
||||||
tokenCreator.getCacheKey()));
|
|
||||||
Token token = null;
|
|
||||||
Calendar ca = Calendar.getInstance();
|
|
||||||
long now_time = ca.getTimeInMillis();
|
|
||||||
try {
|
|
||||||
if (token_file.exists()) {
|
|
||||||
token = XmlStream.fromXML(new FileInputStream(token_file),
|
|
||||||
Token.class);
|
|
||||||
long expire_time = token.getTime()
|
|
||||||
+ (token.getExpiresIn() * 1000) - 2;
|
|
||||||
if (expire_time > now_time) {
|
|
||||||
return token;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
token = tokenCreator.createToken();
|
|
||||||
XmlStream.toXML(token, new FileOutputStream(token_file));
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new WeixinException(e.getMessage());
|
|
||||||
}
|
|
||||||
return token;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
package com.foxinmy.weixin4j.token;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
|
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||||
|
import com.foxinmy.weixin4j.xml.XmlStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用FILE保存TOKEN
|
||||||
|
*
|
||||||
|
* @className FileTokenStorager
|
||||||
|
* @author jy
|
||||||
|
* @date 2015年1月9日
|
||||||
|
* @since JDK 1.7
|
||||||
|
*/
|
||||||
|
public class FileTokenStorager implements TokenStorager {
|
||||||
|
|
||||||
|
private final String tokenPath;
|
||||||
|
|
||||||
|
public FileTokenStorager() {
|
||||||
|
this(ConfigUtil.getValue("token_path"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public FileTokenStorager(String tokenPath) {
|
||||||
|
this.tokenPath = tokenPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Token lookupToken(String cacheKey) throws WeixinException {
|
||||||
|
File token_file = new File(String.format("%s/%s.xml", tokenPath,
|
||||||
|
cacheKey));
|
||||||
|
try {
|
||||||
|
if (token_file.exists()) {
|
||||||
|
Token token = XmlStream.fromXML(
|
||||||
|
new FileInputStream(token_file), Token.class);
|
||||||
|
long expire_time = token.getTime()
|
||||||
|
+ (token.getExpiresIn() * 1000) - 2;
|
||||||
|
if (expire_time > System.currentTimeMillis()) {
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new WeixinException(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cachingToken(Token token, String cacheKey)
|
||||||
|
throws WeixinException {
|
||||||
|
try {
|
||||||
|
XmlStream.toXML(
|
||||||
|
token,
|
||||||
|
new FileOutputStream(new File(String.format("%s/%s.xml",
|
||||||
|
tokenPath, cacheKey))));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new WeixinException(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,11 @@
|
|||||||
### TOKEN的实现
|
### TOKEN的实现
|
||||||
|
|
||||||
TokenHolder.java token持有者接口<br>
|
* TokenCreator 负责创建新的token
|
||||||
FileTokenHolder.java 基于文件保存的TokenHolder实现<br>
|
|
||||||
RedisTokenHolder.java 基于redis保存的TokenHolder实现(推荐)<br>
|
* TokenStorager 负责查找已缓存的token或者缓存新的token
|
||||||
TokenCreator.java token创建者接口
|
|
||||||
|
* TokenHolder 负责获取token(屏蔽了获取方式)
|
||||||
|
|
||||||
|
* FileTokenStorager 是系统默认的token存储策略实现
|
||||||
|
|
||||||
|
* RedisTokenStorager 如果服务器支持redis,推荐使用(需要自己添加jar包和[java类](https://github.com/foxinmy/weixin4j/wiki/%E7%94%A8redis%E4%BF%9D%E5%AD%98token))
|
||||||
|
|||||||
@ -1,19 +1,35 @@
|
|||||||
package com.foxinmy.weixin4j.token;
|
package com.foxinmy.weixin4j.token;
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* token持有者
|
* 对token的缓存获取
|
||||||
*
|
*
|
||||||
* @className TokenHolder
|
* @className TokenHolder
|
||||||
* @author jy.hu
|
* @author jy
|
||||||
* @date 2014年9月27日
|
* @date 2015年6月12日
|
||||||
* @since JDK 1.7
|
* @since JDK 1.7
|
||||||
* @see com.foxinmy.weixin4j.model.Token
|
* @see TokenCreator
|
||||||
* @see com.foxinmy.weixin4j.token.FileTokenHolder
|
* @see TokenStorager
|
||||||
* @see com.foxinmy.weixin4j.token.RedisTokenHolder
|
*/
|
||||||
*/
|
public final class TokenHolder {
|
||||||
public interface TokenHolder {
|
|
||||||
public Token getToken() throws WeixinException;
|
private final TokenCreator tokenCreator;
|
||||||
}
|
private final TokenStorager tokenStorager;
|
||||||
|
|
||||||
|
public TokenHolder(TokenCreator tokenCreator, TokenStorager tokenStorager) {
|
||||||
|
this.tokenCreator = tokenCreator;
|
||||||
|
this.tokenStorager = tokenStorager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Token getToken() throws WeixinException {
|
||||||
|
String cacheKey = tokenCreator.getCacheKey();
|
||||||
|
Token token = tokenStorager.lookupToken(cacheKey);
|
||||||
|
if (token == null) {
|
||||||
|
token = tokenCreator.createToken();
|
||||||
|
tokenStorager.cachingToken(token, cacheKey);
|
||||||
|
}
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -0,0 +1,39 @@
|
|||||||
|
package com.foxinmy.weixin4j.token;
|
||||||
|
|
||||||
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* token的存储
|
||||||
|
*
|
||||||
|
* @className TokenStorager
|
||||||
|
* @author jy.hu
|
||||||
|
* @date 2014年9月27日
|
||||||
|
* @since JDK 1.7
|
||||||
|
* @see com.foxinmy.weixin4j.model.Token
|
||||||
|
* @see com.foxinmy.weixin4j.token.FileTokenStorager
|
||||||
|
* @see com.foxinmy.weixin4j.token.RedisTokenStorager
|
||||||
|
*/
|
||||||
|
public interface TokenStorager {
|
||||||
|
/**
|
||||||
|
* 查找缓存的token
|
||||||
|
*
|
||||||
|
* @param cacheKey
|
||||||
|
* 缓存的名称
|
||||||
|
* @return 查找结果
|
||||||
|
* @throws WeixinException
|
||||||
|
*/
|
||||||
|
public Token lookupToken(String cacheKey) throws WeixinException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存新的token
|
||||||
|
*
|
||||||
|
* @param token
|
||||||
|
* 新产生的token
|
||||||
|
* @param cacheKey
|
||||||
|
* 缓存的名称
|
||||||
|
* @throws WeixinException
|
||||||
|
*/
|
||||||
|
public void cachingToken(Token token, String cacheKey)
|
||||||
|
throws WeixinException;
|
||||||
|
}
|
||||||
@ -55,7 +55,7 @@ weixin4j.properties说明
|
|||||||
| 属性名 | 说明 |
|
| 属性名 | 说明 |
|
||||||
| :---------- | :-------------- |
|
| :---------- | :-------------- |
|
||||||
| account | 微信公众号信息 `json格式` |
|
| account | 微信公众号信息 `json格式` |
|
||||||
| token_path | 使用FileTokenHolder时token保存的物理路径 |
|
| token_path | 使用FileTokenStorager时token保存的物理路径 |
|
||||||
| qr_path | 调用二维码接口时保存二维码图片的物理路径 |
|
| qr_path | 调用二维码接口时保存二维码图片的物理路径 |
|
||||||
| media_path | 调用媒体接口时保存媒体文件的物理路径 |
|
| media_path | 调用媒体接口时保存媒体文件的物理路径 |
|
||||||
| bill_path | 调用下载对账单接口保存excel文件的物理路径 |
|
| bill_path | 调用下载对账单接口保存excel文件的物理路径 |
|
||||||
@ -81,7 +81,7 @@ weixin4j.properties说明
|
|||||||
ca_file=/tmp/weixin4j/xxxxx.p12
|
ca_file=/tmp/weixin4j/xxxxx.p12
|
||||||
#classpath路径下:ca_file=classpath:xxxxx.p12
|
#classpath路径下:ca_file=classpath:xxxxx.p12
|
||||||
|
|
||||||
#微信登陆授权的重定向路径(使用OauthApi时需要填写)
|
#公众号登陆授权的重定向路径(使用OauthApi时需要填写)
|
||||||
redirect_uri=http://xxx
|
redirect_uri=http://xxx
|
||||||
|
|
||||||
2.实例化一个`WeixinProxy`对象,调用API,需要强调的是如果只传入appid,appsecret两个参数将无法调用支付相关接口
|
2.实例化一个`WeixinProxy`对象,调用API,需要强调的是如果只传入appid,appsecret两个参数将无法调用支付相关接口
|
||||||
@ -91,11 +91,11 @@ weixin4j.properties说明
|
|||||||
// weixinProxy = new WeixinProxy(weixinAccount);
|
// weixinProxy = new WeixinProxy(weixinAccount);
|
||||||
weixinProxy.getUser(openId);
|
weixinProxy.getUser(openId);
|
||||||
|
|
||||||
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenHolder`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用[RedisTokenHolder](https://github.com/foxinmy/weixin4j/wiki/%E7%94%A8redis%E4%BF%9D%E5%AD%98token).
|
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenStorager`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用[RedisTokenStorager](https://github.com/foxinmy/weixin4j/wiki/%E7%94%A8redis%E4%BF%9D%E5%AD%98token).
|
||||||
>
|
>
|
||||||
> WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenHolder());
|
> WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenStorager());
|
||||||
|
|
||||||
> // weixinProxy = new WeixinProxy(new RedisTokenHolder(weixinAccount));
|
> // weixinProxy = new WeixinProxy(new RedisTokenStorager(weixinAccount));
|
||||||
|
|
||||||
[更新LOG](./CHANGE.md)
|
[更新LOG](./CHANGE.md)
|
||||||
----------------------
|
----------------------
|
||||||
@ -28,8 +28,9 @@ import com.foxinmy.weixin4j.mp.type.CurrencyType;
|
|||||||
import com.foxinmy.weixin4j.mp.type.IdQuery;
|
import com.foxinmy.weixin4j.mp.type.IdQuery;
|
||||||
import com.foxinmy.weixin4j.mp.type.IdType;
|
import com.foxinmy.weixin4j.mp.type.IdType;
|
||||||
import com.foxinmy.weixin4j.mp.type.RefundType;
|
import com.foxinmy.weixin4j.mp.type.RefundType;
|
||||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
import com.foxinmy.weixin4j.token.FileTokenStorager;
|
||||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||||
|
import com.foxinmy.weixin4j.token.TokenStorager;
|
||||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,19 +52,32 @@ public class WeixinPayProxy {
|
|||||||
private final CouponApi couponApi;
|
private final CouponApi couponApi;
|
||||||
private final CashApi cashApi;
|
private final CashApi cashApi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认使用文件保存token、使用weixin4j.properties配置的账号信息
|
||||||
|
*/
|
||||||
public WeixinPayProxy() {
|
public WeixinPayProxy() {
|
||||||
this(JSON.parseObject(ConfigUtil.getValue("account"),
|
this(new FileTokenStorager());
|
||||||
WeixinMpAccount.class), new FileTokenHolder(
|
|
||||||
new WeixinTokenCreator()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* WeixinAccount对象
|
* 使用weixin4j.properties配置的账号信息
|
||||||
*
|
|
||||||
* @param weixinAccount
|
|
||||||
* 微信账户
|
|
||||||
*/
|
*/
|
||||||
public WeixinPayProxy(WeixinMpAccount weixinAccount, TokenHolder tokenHolder) {
|
public WeixinPayProxy(TokenStorager tokenStorager) {
|
||||||
|
this(tokenStorager, JSON.parseObject(ConfigUtil.getValue("account"),
|
||||||
|
WeixinMpAccount.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param tokenStorager
|
||||||
|
* token的存储策略
|
||||||
|
* @param weixinAccount
|
||||||
|
* 公众号账号信息
|
||||||
|
*/
|
||||||
|
public WeixinPayProxy(TokenStorager tokenStorager,
|
||||||
|
WeixinMpAccount weixinAccount) {
|
||||||
|
TokenHolder tokenHolder = new TokenHolder(new WeixinTokenCreator(
|
||||||
|
weixinAccount), tokenStorager);
|
||||||
this.pay2Api = new Pay2Api(weixinAccount, tokenHolder);
|
this.pay2Api = new Pay2Api(weixinAccount, tokenHolder);
|
||||||
this.pay3Api = new Pay3Api(weixinAccount, tokenHolder);
|
this.pay3Api = new Pay3Api(weixinAccount, tokenHolder);
|
||||||
int version = weixinAccount.getVersion();
|
int version = weixinAccount.getVersion();
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import java.util.List;
|
|||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.weixin.JsonResult;
|
import com.foxinmy.weixin4j.http.weixin.JsonResult;
|
||||||
import com.foxinmy.weixin4j.model.Button;
|
import com.foxinmy.weixin4j.model.Button;
|
||||||
|
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||||
import com.foxinmy.weixin4j.mp.api.CustomApi;
|
import com.foxinmy.weixin4j.mp.api.CustomApi;
|
||||||
import com.foxinmy.weixin4j.mp.api.DataApi;
|
import com.foxinmy.weixin4j.mp.api.DataApi;
|
||||||
import com.foxinmy.weixin4j.mp.api.GroupApi;
|
import com.foxinmy.weixin4j.mp.api.GroupApi;
|
||||||
@ -39,13 +40,15 @@ import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
|
|||||||
import com.foxinmy.weixin4j.mp.type.DatacubeType;
|
import com.foxinmy.weixin4j.mp.type.DatacubeType;
|
||||||
import com.foxinmy.weixin4j.mp.type.IndustryType;
|
import com.foxinmy.weixin4j.mp.type.IndustryType;
|
||||||
import com.foxinmy.weixin4j.mp.type.Lang;
|
import com.foxinmy.weixin4j.mp.type.Lang;
|
||||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
import com.foxinmy.weixin4j.token.FileTokenStorager;
|
||||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||||
|
import com.foxinmy.weixin4j.token.TokenStorager;
|
||||||
import com.foxinmy.weixin4j.tuple.MassTuple;
|
import com.foxinmy.weixin4j.tuple.MassTuple;
|
||||||
import com.foxinmy.weixin4j.tuple.MpArticle;
|
import com.foxinmy.weixin4j.tuple.MpArticle;
|
||||||
import com.foxinmy.weixin4j.tuple.Tuple;
|
import com.foxinmy.weixin4j.tuple.Tuple;
|
||||||
import com.foxinmy.weixin4j.tuple.Video;
|
import com.foxinmy.weixin4j.tuple.Video;
|
||||||
import com.foxinmy.weixin4j.type.MediaType;
|
import com.foxinmy.weixin4j.type.MediaType;
|
||||||
|
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信公众平台接口实现
|
* 微信公众平台接口实现
|
||||||
@ -71,10 +74,18 @@ public class WeixinProxy {
|
|||||||
private final DataApi dataApi;
|
private final DataApi dataApi;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认采用文件存放Token信息
|
* 默认使用文件方式保存token、使用weixin4j.properties配置的账号信息
|
||||||
*/
|
*/
|
||||||
public WeixinProxy() {
|
public WeixinProxy() {
|
||||||
this(new FileTokenHolder(new WeixinTokenCreator()));
|
this(new FileTokenStorager());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认使用weixin4j.properties配置的账号信息
|
||||||
|
* @param tokenStorager
|
||||||
|
*/
|
||||||
|
public WeixinProxy(TokenStorager tokenStorager) {
|
||||||
|
this(tokenStorager, ConfigUtil.getWeixinAccount());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,16 +94,19 @@ public class WeixinProxy {
|
|||||||
* @param appsecret
|
* @param appsecret
|
||||||
*/
|
*/
|
||||||
public WeixinProxy(String appid, String appsecret) {
|
public WeixinProxy(String appid, String appsecret) {
|
||||||
this(new FileTokenHolder(new WeixinTokenCreator(appid, appsecret)));
|
this(new FileTokenStorager(), new WeixinAccount(appid, appsecret));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TokenHolder对象
|
|
||||||
*
|
*
|
||||||
* @see com.foxinmy.weixin4j.token.TokenHolder
|
* @param tokenStorager
|
||||||
* @param tokenHolder
|
* token存储策略
|
||||||
|
* @param weixinAccount
|
||||||
|
* 公众号账号信息
|
||||||
*/
|
*/
|
||||||
public WeixinProxy(TokenHolder tokenHolder) {
|
public WeixinProxy(TokenStorager tokenStorager, WeixinAccount weixinAccount) {
|
||||||
|
TokenHolder tokenHolder = new TokenHolder(new WeixinTokenCreator(
|
||||||
|
weixinAccount), tokenStorager);
|
||||||
this.mediaApi = new MediaApi(tokenHolder);
|
this.mediaApi = new MediaApi(tokenHolder);
|
||||||
this.notifyApi = new NotifyApi(tokenHolder);
|
this.notifyApi = new NotifyApi(tokenHolder);
|
||||||
this.customApi = new CustomApi(tokenHolder);
|
this.customApi = new CustomApi(tokenHolder);
|
||||||
|
|||||||
@ -13,8 +13,7 @@ import com.foxinmy.weixin4j.mp.model.WeixinMpAccount;
|
|||||||
import com.foxinmy.weixin4j.mp.payment.coupon.CouponDetail;
|
import com.foxinmy.weixin4j.mp.payment.coupon.CouponDetail;
|
||||||
import com.foxinmy.weixin4j.mp.payment.coupon.CouponResult;
|
import com.foxinmy.weixin4j.mp.payment.coupon.CouponResult;
|
||||||
import com.foxinmy.weixin4j.mp.payment.coupon.CouponStock;
|
import com.foxinmy.weixin4j.mp.payment.coupon.CouponStock;
|
||||||
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
|
import com.foxinmy.weixin4j.token.FileTokenStorager;
|
||||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
|
||||||
import com.foxinmy.weixin4j.util.DateUtil;
|
import com.foxinmy.weixin4j.util.DateUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,8 +32,7 @@ public class CouponTest {
|
|||||||
ACCOUNT = new WeixinMpAccount("appid",
|
ACCOUNT = new WeixinMpAccount("appid",
|
||||||
"appsecret",
|
"appsecret",
|
||||||
"paysign", "mchid");
|
"paysign", "mchid");
|
||||||
WEIXINPAY = new WeixinPayProxy(ACCOUNT, new FileTokenHolder(
|
WEIXINPAY = new WeixinPayProxy(new FileTokenStorager(), ACCOUNT);
|
||||||
new WeixinTokenCreator(ACCOUNT.getId(), ACCOUNT.getSecret())));
|
|
||||||
}
|
}
|
||||||
protected final File caFile = new File("证书文件路径(*.p12)");
|
protected final File caFile = new File("证书文件路径(*.p12)");
|
||||||
|
|
||||||
|
|||||||
@ -17,11 +17,10 @@ import com.foxinmy.weixin4j.mp.payment.v3.ApiResult;
|
|||||||
import com.foxinmy.weixin4j.mp.payment.v3.Order;
|
import com.foxinmy.weixin4j.mp.payment.v3.Order;
|
||||||
import com.foxinmy.weixin4j.mp.payment.v3.PayPackageV3;
|
import com.foxinmy.weixin4j.mp.payment.v3.PayPackageV3;
|
||||||
import com.foxinmy.weixin4j.mp.payment.v3.PrePay;
|
import com.foxinmy.weixin4j.mp.payment.v3.PrePay;
|
||||||
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
|
|
||||||
import com.foxinmy.weixin4j.mp.type.IdQuery;
|
import com.foxinmy.weixin4j.mp.type.IdQuery;
|
||||||
import com.foxinmy.weixin4j.mp.type.IdType;
|
import com.foxinmy.weixin4j.mp.type.IdType;
|
||||||
import com.foxinmy.weixin4j.mp.type.TradeType;
|
import com.foxinmy.weixin4j.mp.type.TradeType;
|
||||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
import com.foxinmy.weixin4j.token.FileTokenStorager;
|
||||||
|
|
||||||
public class PayTest {
|
public class PayTest {
|
||||||
private final static WeixinPayProxy PAY2;
|
private final static WeixinPayProxy PAY2;
|
||||||
@ -31,12 +30,10 @@ public class PayTest {
|
|||||||
static {
|
static {
|
||||||
ACCOUNT2 = new WeixinMpAccount("请填入v2版本的appid", "请填入v2版本的appSecret",
|
ACCOUNT2 = new WeixinMpAccount("请填入v2版本的appid", "请填入v2版本的appSecret",
|
||||||
"请填入v3版本的paysignkey", "请填入v2版本的partnerId", "请填入v2版本的partnerKey");
|
"请填入v3版本的paysignkey", "请填入v2版本的partnerId", "请填入v2版本的partnerKey");
|
||||||
PAY2 = new WeixinPayProxy(ACCOUNT2, new FileTokenHolder(
|
PAY2 = new WeixinPayProxy(new FileTokenStorager(), ACCOUNT2);
|
||||||
new WeixinTokenCreator(ACCOUNT2.getId(), ACCOUNT2.getSecret())));
|
|
||||||
ACCOUNT3 = new WeixinMpAccount("请填入v3版本的appid", "请填入v3版本的appSecret",
|
ACCOUNT3 = new WeixinMpAccount("请填入v3版本的appid", "请填入v3版本的appSecret",
|
||||||
"请填入v3版本的paysignkey", "请填入v3版本的mchid");
|
"请填入v3版本的paysignkey", "请填入v3版本的mchid");
|
||||||
PAY3 = new WeixinPayProxy(ACCOUNT3, new FileTokenHolder(
|
PAY3 = new WeixinPayProxy(new FileTokenStorager(), ACCOUNT3);
|
||||||
new WeixinTokenCreator(ACCOUNT3.getId(), ACCOUNT3.getSecret())));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
|
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
|
||||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
import com.foxinmy.weixin4j.token.FileTokenStorager;
|
||||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,7 +23,8 @@ public class TokenTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
tokenHolder = new FileTokenHolder(new WeixinTokenCreator());
|
tokenHolder = new TokenHolder(new WeixinTokenCreator(),
|
||||||
|
new FileTokenStorager());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@ -39,7 +39,7 @@ weixin4j.properties说明
|
|||||||
| 属性名 | 说明 |
|
| 属性名 | 说明 |
|
||||||
| :---------- | :-------------- |
|
| :---------- | :-------------- |
|
||||||
| account | 微信企业号信息 `json格式` |
|
| account | 微信企业号信息 `json格式` |
|
||||||
| token_path | 使用FileTokenHolder时token保存的物理路径 |
|
| token_path | 使用FileTokenStorager时token保存的物理路径 |
|
||||||
| media_path | 调用媒体接口时保存媒体文件的物理路径 |
|
| media_path | 调用媒体接口时保存媒体文件的物理路径 |
|
||||||
| redirect_uri | 调用OauthApi接口时需要填写的重定向路径 |
|
| redirect_uri | 调用OauthApi接口时需要填写的重定向路径 |
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ weixin4j.properties说明
|
|||||||
token_path=/tmp/weixin4j/token
|
token_path=/tmp/weixin4j/token
|
||||||
media_path=/tmp/weixin4j/media
|
media_path=/tmp/weixin4j/media
|
||||||
|
|
||||||
#微信登陆授权的重定向路径(使用OauthApi时需要填写)
|
#企业号登陆授权的重定向路径(使用OauthApi时需要填写)
|
||||||
redirect_uri=http://xxx
|
redirect_uri=http://xxx
|
||||||
|
|
||||||
2.实例化一个`WeixinProxy`对象,调用API
|
2.实例化一个`WeixinProxy`对象,调用API
|
||||||
@ -63,11 +63,11 @@ weixin4j.properties说明
|
|||||||
// weixinProxy = new WeixinProxy(weixinAccount);
|
// weixinProxy = new WeixinProxy(weixinAccount);
|
||||||
weixinProxy.getUser(userid);
|
weixinProxy.getUser(userid);
|
||||||
|
|
||||||
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenHolder`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用[RedisTokenHolder](https://github.com/foxinmy/weixin4j/wiki/%E7%94%A8redis%E4%BF%9D%E5%AD%98token).
|
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenStorager`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用[RedisTokenStorager](https://github.com/foxinmy/weixin4j/wiki/%E7%94%A8redis%E4%BF%9D%E5%AD%98token).
|
||||||
|
|
||||||
> WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenHolder());
|
> WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenStorager());
|
||||||
|
|
||||||
> // weixinProxy = new WeixinProxy(new RedisTokenHolder(weixinAccount));
|
> // weixinProxy = new WeixinProxy(new RedisTokenStorager(weixinAccount));
|
||||||
|
|
||||||
[更新LOG](./CHANGE.md)
|
[更新LOG](./CHANGE.md)
|
||||||
----------------------
|
----------------------
|
||||||
@ -8,6 +8,7 @@ import com.alibaba.fastjson.JSONObject;
|
|||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.weixin.JsonResult;
|
import com.foxinmy.weixin4j.http.weixin.JsonResult;
|
||||||
import com.foxinmy.weixin4j.model.Button;
|
import com.foxinmy.weixin4j.model.Button;
|
||||||
|
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||||
import com.foxinmy.weixin4j.qy.api.AgentApi;
|
import com.foxinmy.weixin4j.qy.api.AgentApi;
|
||||||
import com.foxinmy.weixin4j.qy.api.BatchApi;
|
import com.foxinmy.weixin4j.qy.api.BatchApi;
|
||||||
import com.foxinmy.weixin4j.qy.api.HelperApi;
|
import com.foxinmy.weixin4j.qy.api.HelperApi;
|
||||||
@ -30,9 +31,11 @@ import com.foxinmy.weixin4j.qy.model.User;
|
|||||||
import com.foxinmy.weixin4j.qy.token.WeixinTokenCreator;
|
import com.foxinmy.weixin4j.qy.token.WeixinTokenCreator;
|
||||||
import com.foxinmy.weixin4j.qy.type.InviteType;
|
import com.foxinmy.weixin4j.qy.type.InviteType;
|
||||||
import com.foxinmy.weixin4j.qy.type.UserStatus;
|
import com.foxinmy.weixin4j.qy.type.UserStatus;
|
||||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
import com.foxinmy.weixin4j.token.FileTokenStorager;
|
||||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||||
|
import com.foxinmy.weixin4j.token.TokenStorager;
|
||||||
import com.foxinmy.weixin4j.type.MediaType;
|
import com.foxinmy.weixin4j.type.MediaType;
|
||||||
|
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信企业号接口实现
|
* 微信企业号接口实现
|
||||||
@ -55,10 +58,19 @@ public class WeixinProxy {
|
|||||||
private final BatchApi batchApi;
|
private final BatchApi batchApi;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认采用文件存放Token信息
|
* 默认使用文件方式保存token、使用weixin4j.properties配置的账号信息
|
||||||
*/
|
*/
|
||||||
public WeixinProxy() {
|
public WeixinProxy() {
|
||||||
this(new FileTokenHolder(new WeixinTokenCreator()));
|
this(new FileTokenStorager());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认使用weixin4j.properties配置的账号信息
|
||||||
|
*
|
||||||
|
* @param tokenStorager
|
||||||
|
*/
|
||||||
|
public WeixinProxy(TokenStorager tokenStorager) {
|
||||||
|
this(tokenStorager, ConfigUtil.getWeixinAccount());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,16 +80,19 @@ public class WeixinProxy {
|
|||||||
* @param corpsecret
|
* @param corpsecret
|
||||||
*/
|
*/
|
||||||
public WeixinProxy(String corpid, String corpsecret) {
|
public WeixinProxy(String corpid, String corpsecret) {
|
||||||
this(new FileTokenHolder(new WeixinTokenCreator(corpid, corpsecret)));
|
this(new FileTokenStorager(), new WeixinAccount(corpid, corpsecret));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TokenHolder对象
|
|
||||||
*
|
*
|
||||||
* @see com.foxinmy.weixin4j.token.TokenHolder
|
* @param tokenStorager
|
||||||
* @param tokenHolder
|
* token存储策略
|
||||||
|
* @param weixinAccount
|
||||||
|
* 企业号账号信息
|
||||||
*/
|
*/
|
||||||
public WeixinProxy(TokenHolder tokenHolder) {
|
public WeixinProxy(TokenStorager tokenStorager, WeixinAccount weixinAccount) {
|
||||||
|
TokenHolder tokenHolder = new TokenHolder(new WeixinTokenCreator(
|
||||||
|
weixinAccount), tokenStorager);
|
||||||
this.partyApi = new PartyApi(tokenHolder);
|
this.partyApi = new PartyApi(tokenHolder);
|
||||||
this.userApi = new UserApi(tokenHolder);
|
this.userApi = new UserApi(tokenHolder);
|
||||||
this.tagApi = new TagApi(tokenHolder);
|
this.tagApi = new TagApi(tokenHolder);
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.qy.token.WeixinTokenCreator;
|
import com.foxinmy.weixin4j.qy.token.WeixinTokenCreator;
|
||||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
import com.foxinmy.weixin4j.token.FileTokenStorager;
|
||||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,8 +23,8 @@ public class TokenTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
tokenHolder = new FileTokenHolder(
|
tokenHolder = new TokenHolder(new WeixinTokenCreator(),
|
||||||
new WeixinTokenCreator());
|
new FileTokenStorager());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user