优化TokenCreator相关类
This commit is contained in:
@@ -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=
|
||||
Reference in New Issue
Block a user