优化TokenCreator相关类
This commit is contained in:
parent
d1ede56860
commit
76ae9d58e3
@ -36,6 +36,10 @@ public final class Consts {
|
||||
* 企业号获取token的url
|
||||
*/
|
||||
public static final String QY_ASSESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
|
||||
/**
|
||||
* 企业号提供商获取token的url
|
||||
*/
|
||||
public static final String QY_PROVIDER_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/service/get_provider_token";
|
||||
/**
|
||||
* 公众平台jssdk获取token的url
|
||||
*/
|
||||
|
||||
@ -8,7 +8,7 @@ import com.alibaba.fastjson.JSON;
|
||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||
|
||||
/**
|
||||
* 公众号配置
|
||||
* 公众号配置信息 class路径下weixin4j.properties文件
|
||||
*
|
||||
* @className ConfigUtil
|
||||
* @author jy
|
||||
@ -40,7 +40,7 @@ public class ConfigUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取weixin.properties文件中的key值
|
||||
* 获取weixin4j.properties文件中的key值
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
|
||||
@ -11,6 +11,7 @@ import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||
import com.foxinmy.weixin4j.mp.model.OauthToken;
|
||||
import com.foxinmy.weixin4j.mp.model.User;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* oauth授权
|
||||
@ -26,21 +27,12 @@ public class OauthApi extends MpApi {
|
||||
/**
|
||||
* @see {@link com.foxinmy.weixin4j.mp.api.OauthApi#getAuthorizeURL(String, String,String)}
|
||||
*
|
||||
* @return
|
||||
* @return 请求授权的URL
|
||||
*/
|
||||
public String getAuthorizeURL() {
|
||||
return getAuthorizeURL("state");
|
||||
}
|
||||
|
||||
/**
|
||||
* @see {@link com.foxinmy.weixin4j.mp.api.OauthApi#getAuthorizeURL(String, String,String)}
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getAuthorizeURL(String state) {
|
||||
String appId = ConfigUtil.getWeixinAccount().getId();
|
||||
String redirectUri = ConfigUtil.getValue("redirect_uri");
|
||||
return getAuthorizeURL(appId, redirectUri, state);
|
||||
return getAuthorizeURL(appId, redirectUri, "state", "snsapi_login");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -52,14 +44,15 @@ public class OauthApi extends MpApi {
|
||||
* 重定向地址
|
||||
* @param state
|
||||
* 用于保持请求和回调的状态,授权请求后原样带回给第三方
|
||||
* @return 请求的URL
|
||||
* @return 请求授权的URL
|
||||
*/
|
||||
public String getAuthorizeURL(String appId, String redirectUri, String state) {
|
||||
public String getAuthorizeURL(String appId, String redirectUri,
|
||||
String state, String... scopes) {
|
||||
String sns_user_auth_uri = getRequestUri("sns_user_auth_uri");
|
||||
try {
|
||||
return String.format(sns_user_auth_uri, appId,
|
||||
URLEncoder.encode(redirectUri, Consts.UTF_8.name()),
|
||||
"snsapi_login", state);
|
||||
StringUtil.join(scopes, ','), state);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
;
|
||||
}
|
||||
@ -92,8 +85,8 @@ public class OauthApi extends MpApi {
|
||||
public OauthToken getOauthToken(String code, String appid, String appsecret)
|
||||
throws WeixinException {
|
||||
String user_token_uri = getRequestUri("sns_user_token_uri");
|
||||
WeixinResponse response = weixinClient.get(String.format(user_token_uri, appid,
|
||||
appsecret, code));
|
||||
WeixinResponse response = weixinClient.get(String.format(
|
||||
user_token_uri, appid, appsecret, code));
|
||||
|
||||
return response.getAsObject(new TypeReference<OauthToken>() {
|
||||
});
|
||||
@ -122,8 +115,8 @@ public class OauthApi extends MpApi {
|
||||
public OauthToken refreshToken(String appId, String refreshToken)
|
||||
throws WeixinException {
|
||||
String sns_token_refresh_uri = getRequestUri("sns_token_refresh_uri");
|
||||
WeixinResponse response = weixinClient.get(String.format(sns_token_refresh_uri,
|
||||
appId, refreshToken));
|
||||
WeixinResponse response = weixinClient.get(String.format(
|
||||
sns_token_refresh_uri, appId, refreshToken));
|
||||
|
||||
return response.getAsObject(new TypeReference<OauthToken>() {
|
||||
});
|
||||
@ -141,7 +134,8 @@ public class OauthApi extends MpApi {
|
||||
public boolean authAccessToken(String accessToken, String openId) {
|
||||
String sns_auth_token_uri = getRequestUri("sns_auth_token_uri");
|
||||
try {
|
||||
weixinClient.get(String.format(sns_auth_token_uri, accessToken, openId));
|
||||
weixinClient.get(String.format(sns_auth_token_uri, accessToken,
|
||||
openId));
|
||||
return true;
|
||||
} catch (WeixinException e) {
|
||||
;
|
||||
|
||||
@ -8,6 +8,7 @@ import com.foxinmy.weixin4j.model.Consts;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
|
||||
/**
|
||||
* 微信公众平台JSTICKET创建者
|
||||
@ -26,6 +27,16 @@ public class WeixinJSTicketCreator implements TokenCreator {
|
||||
private final TokenHolder weixinTokenHolder;
|
||||
private final WeixinHttpClient httpClient;
|
||||
|
||||
/**
|
||||
* jssdk
|
||||
*
|
||||
* @param weixinTokenHolder
|
||||
* <font color="red">公众平台的access_token</font>
|
||||
*/
|
||||
public WeixinJSTicketCreator(TokenHolder weixinTokenHolder) {
|
||||
this(ConfigUtil.getWeixinAccount().getId(), weixinTokenHolder);
|
||||
}
|
||||
|
||||
/**
|
||||
* jssdk
|
||||
*
|
||||
@ -47,8 +58,9 @@ public class WeixinJSTicketCreator implements TokenCreator {
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
WeixinResponse response = httpClient.get(String.format(Consts.MP_JS_TICKET_URL,
|
||||
weixinTokenHolder.getToken().getAccessToken()));
|
||||
WeixinResponse response = httpClient.get(String.format(
|
||||
Consts.MP_JS_TICKET_URL, weixinTokenHolder.getToken()
|
||||
.getAccessToken()));
|
||||
JSONObject result = response.getAsJson();
|
||||
Token token = new Token(result.getString("ticket"));
|
||||
token.setExpiresIn(result.getIntValue("expires_in"));
|
||||
|
||||
@ -28,10 +28,11 @@ public class WeixinTokenCreator implements TokenCreator {
|
||||
private final String secret;
|
||||
|
||||
public WeixinTokenCreator() {
|
||||
WeixinAccount weixinAccount = ConfigUtil.getWeixinAccount();
|
||||
this.appid = weixinAccount.getId();
|
||||
this.secret = weixinAccount.getSecret();
|
||||
this.httpClient = new WeixinHttpClient();
|
||||
this(ConfigUtil.getWeixinAccount());
|
||||
}
|
||||
|
||||
public WeixinTokenCreator(WeixinAccount weixinAccount) {
|
||||
this(weixinAccount.getId(), weixinAccount.getSecret());
|
||||
}
|
||||
|
||||
public WeixinTokenCreator(String appid, String secret) {
|
||||
|
||||
@ -20,4 +20,7 @@ bill_path=/tmp/weixin4j/bill
|
||||
# ca\u8bc1\u4e66\u5b58\u653e\u7684\u5b8c\u6574\u8def\u5f84 (V2\u7248\u672c\u540e\u7f00\u4e3a*.pfx,V3\u7248\u672c\u540e\u7f00\u4e3a*.p12)
|
||||
ca_file=/tmp/weixin4j/xxxxx.p12
|
||||
# classpath\u8def\u5f84\u4e0b\u53ef\u4ee5\u8fd9\u4e48\u5199
|
||||
# ca_file=classpath:xxxxx.pfx
|
||||
# ca_file=classpath:xxxxx.pfx
|
||||
|
||||
# oauth\u6388\u6743\u540e\u91cd\u5b9a\u5411\u7684url
|
||||
redirect_uri=
|
||||
@ -41,15 +41,20 @@ weixin4j.properties说明
|
||||
| account | 微信企业号信息 `json格式` |
|
||||
| token_path | 使用FileTokenHolder时token保存的物理路径 |
|
||||
| media_path | 调用媒体接口时保存媒体文件的物理路径 |
|
||||
| redirect_uri | 调用OauthApi接口时需要填写的重定向路径 |
|
||||
|
||||
示例(properties中换行用右斜杆\\)
|
||||
|
||||
account={"id":"corpid","secret":"corpsecret",\
|
||||
"token":"企业号中应用在回调模式下的token",\
|
||||
"encodingAesKey":"企业号中应用在回调模式下AES加密密钥"}
|
||||
"encodingAesKey":"企业号中应用在回调模式下AES加密密钥",\
|
||||
"providerSecret:"提供商的secret"}
|
||||
|
||||
token_path=/tmp/weixin4j/token
|
||||
media_path=/tmp/weixin4j/media
|
||||
|
||||
#微信登陆授权的重定向路径(使用OauthApi时需要填写)
|
||||
redirect_uri=http://xxx
|
||||
|
||||
2.实例化一个`WeixinProxy`对象,调用API
|
||||
|
||||
|
||||
@ -62,7 +62,7 @@ public class WeixinProxy {
|
||||
}
|
||||
|
||||
/**
|
||||
* appid,appsecret
|
||||
* corpid,corpsecret
|
||||
*
|
||||
* @param corpid
|
||||
* @param corpsecret
|
||||
|
||||
@ -78,4 +78,9 @@ batch_replaceuser_uri={api_base_url}/batch/replaceuser?access_token=%s
|
||||
batch_replaceparty_uri={api_base_url}/batch/replaceparty?access_token=%s
|
||||
batch_replaceparty.cvs={"header":"\u90e8\u95e8\u540d\u79f0,\u90e8\u95e8ID,\u7236\u90e8\u95e8ID,\u6392\u5e8f","column":["name","id","parentid","order"]}
|
||||
# \u83b7\u53d6\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u7ed3\u679c
|
||||
batch_getresult_uri={api_base_url}/batch/getresult?access_token=%s&jobid=%s
|
||||
batch_getresult_uri={api_base_url}/batch/getresult?access_token=%s&jobid=%s
|
||||
|
||||
# \u63d0\u4f9b\u5546oauth\u6388\u6743
|
||||
provider_oauth_uri=https://qy.weixin.qq.com/cgi-bin/loginpage?corp_id=%s&redirect_uri=%s&state=%s
|
||||
# \u4f01\u4e1a\u53f7\u7ba1\u7406\u5458\u767b\u5f55\u4fe1\u606f
|
||||
oauth_logininfo_uri={api_base_url}/service/get_login_info?provider_access_token=%s
|
||||
@ -30,8 +30,22 @@ public class WeixinQyAccount extends WeixinAccount {
|
||||
super(corpid, corpsecret);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供商的secret
|
||||
*/
|
||||
private String providerSecret;
|
||||
|
||||
public String getProviderSecret() {
|
||||
return providerSecret;
|
||||
}
|
||||
|
||||
public void setProviderSecret(String providerSecret) {
|
||||
this.providerSecret = providerSecret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WeixinQyAccount [" + super.toString() + "]";
|
||||
return "WeixinQyAccount [" + super.toString() + ", providerSecret="
|
||||
+ providerSecret + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,9 +8,10 @@ import com.foxinmy.weixin4j.model.Consts;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
|
||||
/**
|
||||
* 微信企业号JSTICKET创建者
|
||||
* 微信企业号JSTICKET创建
|
||||
*
|
||||
* @className WeixinJSTicketCreator
|
||||
* @author jy
|
||||
@ -22,30 +23,41 @@ import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
*/
|
||||
public class WeixinJSTicketCreator implements TokenCreator {
|
||||
|
||||
private final String appid;
|
||||
private final String corpid;
|
||||
private final TokenHolder weixinTokenHolder;
|
||||
private final WeixinHttpClient httpClient;
|
||||
|
||||
/**
|
||||
* jssdk
|
||||
*
|
||||
* @param weixinTokenHolder
|
||||
* <font color="red">公众平台的access_token</font>
|
||||
*/
|
||||
public WeixinJSTicketCreator(TokenHolder weixinTokenHolder) {
|
||||
this(ConfigUtil.getWeixinAccount().getId(), weixinTokenHolder);
|
||||
}
|
||||
|
||||
/**
|
||||
* <font color="red">企业号的的access_token</font>
|
||||
*
|
||||
* @param weixinTokenHolder
|
||||
*/
|
||||
public WeixinJSTicketCreator(String appid, TokenHolder weixinTokenHolder) {
|
||||
this.appid = appid;
|
||||
public WeixinJSTicketCreator(String corpid, TokenHolder weixinTokenHolder) {
|
||||
this.corpid = corpid;
|
||||
this.weixinTokenHolder = weixinTokenHolder;
|
||||
this.httpClient = new WeixinHttpClient();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey() {
|
||||
return String.format("qy_jsticket_%s", appid);
|
||||
return String.format("qy_jsticket_%s", corpid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
WeixinResponse response = httpClient.get(String.format(Consts.QY_JS_TICKET_URL,
|
||||
weixinTokenHolder.getToken().getAccessToken()));
|
||||
WeixinResponse response = httpClient.get(String.format(
|
||||
Consts.QY_JS_TICKET_URL, weixinTokenHolder.getToken()
|
||||
.getAccessToken()));
|
||||
JSONObject result = response.getAsJson();
|
||||
Token token = new Token(result.getString("ticket"));
|
||||
token.setExpiresIn(result.getIntValue("expires_in"));
|
||||
|
||||
@ -11,7 +11,7 @@ import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
|
||||
/**
|
||||
* 微信企业号TOKEN创建者
|
||||
* 微信企业号TOKEN创建
|
||||
*
|
||||
* @className WeixinTokenCreator
|
||||
* @author jy
|
||||
@ -28,10 +28,11 @@ public class WeixinTokenCreator implements TokenCreator {
|
||||
private final String corpsecret;
|
||||
|
||||
public WeixinTokenCreator() {
|
||||
WeixinAccount weixinAccount = ConfigUtil.getWeixinAccount();
|
||||
this.corpid = weixinAccount.getId();
|
||||
this.corpsecret = weixinAccount.getSecret();
|
||||
this.httpClient = new WeixinHttpClient();
|
||||
this(ConfigUtil.getWeixinAccount());
|
||||
}
|
||||
|
||||
public WeixinTokenCreator(WeixinAccount weixinAccount) {
|
||||
this(weixinAccount.getId(), weixinAccount.getSecret());
|
||||
}
|
||||
|
||||
public WeixinTokenCreator(String corpid, String corpsecret) {
|
||||
|
||||
@ -2,9 +2,13 @@
|
||||
# \u4f01\u4e1a\u53f7\u4fe1\u606f
|
||||
account={"id":"wxf10bce209c91d0e2","secret":"cW0OtP7-7YJ7jHKFZaJW2skJHE9bLHadxOswBdVdI2walRBSPfTSA6QqD_QXw8JZ",\
|
||||
"token":"gp2eGT5mIpngr",\
|
||||
"encodingAesKey":"BRYfV4zPFUJb3v3MySNBg1ERKE3vyyMRoScu76vFySv"}
|
||||
"encodingAesKey":"BRYfV4zPFUJb3v3MySNBg1ERKE3vyyMRoScu76vFySv",\
|
||||
"providerSecret":"\u63d0\u4f9b\u5546\u7684secret"}
|
||||
|
||||
# \u4f7f\u7528FileTokenHolder\u65f6token\u7684\u5b58\u653e\u8def\u5f84
|
||||
token_path=/tmp/weixin4j/token
|
||||
# \u5a92\u4f53\u6587\u4ef6\u4fdd\u5b58\u8def\u5f84
|
||||
media_path=/tmp/weixin4j/media
|
||||
media_path=/tmp/weixin4j/media
|
||||
|
||||
# oauth\u6388\u6743\u540e\u91cd\u5b9a\u5411\u7684url
|
||||
redirect_uri=
|
||||
@ -2,6 +2,8 @@ package com.foxinmy.weixin4j.server.test;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.handler.BlankMessageHandler;
|
||||
import com.foxinmy.weixin4j.handler.DebugMessageHandler;
|
||||
@ -112,6 +114,9 @@ public class MessageServerStartup {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new MessageServerStartup().test1();
|
||||
|
||||
System.err.println(new BigDecimal(new Long(14212345l)).divide(
|
||||
new BigDecimal("100000"))
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user