优化OauthApi授权 #64
This commit is contained in:
@@ -23,7 +23,7 @@ import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||
|
||||
/**
|
||||
* 微信第三方应用接口实现
|
||||
*
|
||||
*
|
||||
* @className WeixinSuiteProxy
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年6月22日
|
||||
@@ -37,7 +37,7 @@ public class WeixinSuiteProxy {
|
||||
|
||||
/**
|
||||
* 每个套件授权不一样 suiteId - suiteApi
|
||||
*/
|
||||
*/
|
||||
private Map<String, SuiteApi> suiteMap;
|
||||
/**
|
||||
* 供应商API:如登陆URL
|
||||
@@ -53,7 +53,7 @@ public class WeixinSuiteProxy {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param suiteSettings
|
||||
* 套件信息配置
|
||||
*/
|
||||
@@ -61,27 +61,32 @@ public class WeixinSuiteProxy {
|
||||
this.suiteSettings = suiteSettings;
|
||||
if (suiteSettings.getWeixinAccount().getSuiteAccounts() != null) {
|
||||
this.suiteMap = new HashMap<String, SuiteApi>();
|
||||
for (WeixinAccount suite : suiteSettings.getWeixinAccount().getSuiteAccounts()) {
|
||||
for (WeixinAccount suite : suiteSettings.getWeixinAccount()
|
||||
.getSuiteAccounts()) {
|
||||
this.suiteMap.put(suite.getId(), new SuiteApi(
|
||||
new SuiteTicketHolder(suite.getId(), suite.getSecret(), suiteSettings.getTokenStorager0())));
|
||||
this.suiteMap.put(null,
|
||||
suiteMap.get(suiteSettings.getWeixinAccount().getSuiteAccounts().get(0).getId()));
|
||||
new SuiteTicketHolder(suite.getId(), suite.getSecret(),
|
||||
suiteSettings.getTokenStorager0())));
|
||||
this.suiteMap.put(
|
||||
null,
|
||||
suiteMap.get(suiteSettings.getWeixinAccount()
|
||||
.getSuiteAccounts().get(0).getId()));
|
||||
}
|
||||
}
|
||||
if (StringUtil.isNotBlank(suiteSettings.getWeixinAccount().getId())
|
||||
&& StringUtil.isNotBlank(suiteSettings.getWeixinAccount().getProviderSecret())) {
|
||||
this.providerApi = new ProviderApi(
|
||||
new TokenHolder(
|
||||
new WeixinProviderTokenCreator(suiteSettings.getWeixinAccount().getId(),
|
||||
suiteSettings.getWeixinAccount().getProviderSecret()),
|
||||
suiteSettings.getTokenStorager0()),
|
||||
&& StringUtil.isNotBlank(suiteSettings.getWeixinAccount()
|
||||
.getProviderSecret())) {
|
||||
this.providerApi = new ProviderApi(new TokenHolder(
|
||||
new WeixinProviderTokenCreator(suiteSettings
|
||||
.getWeixinAccount().getId(), suiteSettings
|
||||
.getWeixinAccount().getProviderSecret()),
|
||||
suiteSettings.getTokenStorager0()),
|
||||
suiteSettings.getTokenStorager0());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业号信息
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public WeixinQyAccount getWeixinAccount() {
|
||||
@@ -90,7 +95,7 @@ public class WeixinSuiteProxy {
|
||||
|
||||
/**
|
||||
* 获取套件接口对象(只关注第一个套件
|
||||
*
|
||||
*
|
||||
* @see com.foxinmy.weixin4j.qy.api.SuiteApi
|
||||
* @return API实例
|
||||
*/
|
||||
@@ -100,7 +105,7 @@ public class WeixinSuiteProxy {
|
||||
|
||||
/**
|
||||
* 获取套件接口对象(多个套件
|
||||
*
|
||||
*
|
||||
* @see com.foxinmy.weixin4j.qy.api.SuiteApi
|
||||
* @param suiteId
|
||||
* 套件ID
|
||||
@@ -112,7 +117,7 @@ public class WeixinSuiteProxy {
|
||||
|
||||
/**
|
||||
* 缓存套件ticket(多个套件
|
||||
*
|
||||
*
|
||||
* @param suiteId
|
||||
* 套件ID
|
||||
* @param suiteTicket
|
||||
@@ -122,13 +127,14 @@ public class WeixinSuiteProxy {
|
||||
* 推送suite_ticket协议</a>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public void cacheTicket(String suiteId, String suiteTicket) throws WeixinException {
|
||||
public void cacheTicket(String suiteId, String suiteTicket)
|
||||
throws WeixinException {
|
||||
suite(suiteId).getTicketHolder().cachingTicket(suiteTicket);
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用套件授权 <font color="red">需先缓存ticket</font>
|
||||
*
|
||||
*
|
||||
* @see {@link #getSuiteAuthorizeURL(String, String,String)}
|
||||
* @param suiteId
|
||||
* 套件ID
|
||||
@@ -137,13 +143,14 @@ public class WeixinSuiteProxy {
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String getSuiteAuthorizeURL(String suiteId) throws WeixinException {
|
||||
String redirectUri = Weixin4jConfigUtil.getValue("suite.oauth.redirect.uri");
|
||||
String redirectUri = Weixin4jConfigUtil
|
||||
.getValue("suite.oauth.redirect.uri");
|
||||
return getSuiteAuthorizeURL(suiteId, redirectUri, "state");
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用套件授权 <font color="red">需先缓存ticket</font>
|
||||
*
|
||||
*
|
||||
* @param suiteId
|
||||
* 套件ID
|
||||
* @param redirectUri
|
||||
@@ -157,9 +164,11 @@ public class WeixinSuiteProxy {
|
||||
* @return 请求授权的URL
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String getSuiteAuthorizeURL(String suiteId, String redirectUri, String state) throws WeixinException {
|
||||
public String getSuiteAuthorizeURL(String suiteId, String redirectUri,
|
||||
String state) throws WeixinException {
|
||||
try {
|
||||
return String.format(URLConsts.SUITE_OAUTH_URL, suiteId, suite(suiteId).getTicketHolder().getTicket(),
|
||||
return String.format(URLConsts.SUITE_OAUTH_URL, suiteId,
|
||||
suite(suiteId).getTicketHolder().getTicket(),
|
||||
URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
;
|
||||
@@ -169,9 +178,10 @@ public class WeixinSuiteProxy {
|
||||
|
||||
/**
|
||||
* 第三方套件获取企业号管理员登录信息
|
||||
*
|
||||
*
|
||||
* @param authCode
|
||||
* oauth2.0授权企业号管理员登录产生的code
|
||||
* oauth2.0授权企业号管理员登录产生的code:通过成员授权获取到的code,每次成员授权带上的code将不一样,
|
||||
* code只能使用一次,5分钟未被使用自动过期
|
||||
* @return 登陆信息
|
||||
* @see com.foxinmy.weixin4j.qy.api.ProviderApi
|
||||
* @see <a href=
|
||||
@@ -186,7 +196,7 @@ public class WeixinSuiteProxy {
|
||||
|
||||
/**
|
||||
* 获取登录企业号官网的url
|
||||
*
|
||||
*
|
||||
* @param corpId
|
||||
* <font color="red">oauth授权的corpid</font>
|
||||
* @param targetType
|
||||
@@ -200,13 +210,14 @@ public class WeixinSuiteProxy {
|
||||
* 获取登录企业号官网的url</a>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String getLoginUrl(String corpId, LoginTargetType targetType, int agentId) throws WeixinException {
|
||||
public String getLoginUrl(String corpId, LoginTargetType targetType,
|
||||
int agentId) throws WeixinException {
|
||||
return providerApi.getLoginUrl(corpId, targetType, agentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建WeixinProxy对象
|
||||
*
|
||||
*
|
||||
* @param suiteId
|
||||
* 套件ID
|
||||
* @param authCorpId
|
||||
@@ -215,7 +226,8 @@ public class WeixinSuiteProxy {
|
||||
* @return
|
||||
*/
|
||||
public WeixinProxy getWeixinProxy(String suiteId, String authCorpId) {
|
||||
return new WeixinProxy(suite(suiteId).getPerCodeHolder(authCorpId), suite(suiteId).getSuiteTokenHolder());
|
||||
return new WeixinProxy(suite(suiteId).getPerCodeHolder(authCorpId),
|
||||
suite(suiteId).getSuiteTokenHolder());
|
||||
}
|
||||
|
||||
public final static String VERSION = "1.6.9";
|
||||
|
||||
@@ -9,7 +9,7 @@ import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||
|
||||
/**
|
||||
* 企业号oauth授权
|
||||
*
|
||||
*
|
||||
* @className OauthApi
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年6月11日
|
||||
@@ -27,45 +27,54 @@ import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||
public class OauthApi extends QyApi {
|
||||
private final WeixinAccount account;
|
||||
|
||||
/**
|
||||
* 默认使用weixin4j.properties里面的corpid、corpsecret信息
|
||||
*/
|
||||
public OauthApi() {
|
||||
this(Weixin4jConfigUtil.getWeixinAccount());
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入corpid、appsecret信息
|
||||
*
|
||||
* @param account
|
||||
*/
|
||||
public OauthApi(WeixinAccount account) {
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业号用户身份授权
|
||||
*
|
||||
* @see {@link #getUserAuthorizeURL(String, String,String)}
|
||||
*
|
||||
* 企业号用户身份授权:重定向URL使用weixin4j.properties#user.oauth.redirect.uri
|
||||
*
|
||||
* @see {@link #getUserAuthorizeURL(String,String)}
|
||||
*
|
||||
* @return 请求授权的URL
|
||||
*/
|
||||
public String getUserAuthorizeURL() {
|
||||
String corpId = account.getId();
|
||||
String redirectUri = Weixin4jConfigUtil.getValue("user.oauth.redirect.uri");
|
||||
return getUserAuthorizeURL(corpId, redirectUri, "state");
|
||||
String redirectUri = Weixin4jConfigUtil
|
||||
.getValue("user.oauth.redirect.uri");
|
||||
return getUserAuthorizeURL(redirectUri, "state");
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业号用户身份授权
|
||||
*
|
||||
* @param corpId
|
||||
* 企业号的corpid
|
||||
*
|
||||
* @param redirectUri
|
||||
* 重定向地址
|
||||
* @param state
|
||||
* 用于保持请求和回调的状态
|
||||
* @return 请求授权的URL
|
||||
* @see UserApi
|
||||
* @see {@link com.foxinmy.weixin4j.qy.WeixinProxy#getUserByCode(String)}
|
||||
* @see <a href=
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E7%99%BB%E5%BD%95%E6%8E%88%E6%9D%83">
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E8%BA%AB%E4%BB%BD%E9%AA%8C%E8%AF%81">
|
||||
* 企业号用户身份授权</a>
|
||||
*/
|
||||
public String getUserAuthorizeURL(String corpId, String redirectUri, String state) {
|
||||
public String getUserAuthorizeURL(String redirectUri, String state) {
|
||||
String oauth_uri = getRequestUri("user_oauth_uri");
|
||||
try {
|
||||
return String.format(oauth_uri, corpId, URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state);
|
||||
return String.format(oauth_uri, account.getId(),
|
||||
URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
;
|
||||
}
|
||||
@@ -73,21 +82,21 @@ public class OauthApi extends QyApi {
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业号第三方提供商授权
|
||||
*
|
||||
* @see {@link #getThirdAuthorizeURL(String, String,String)}
|
||||
*
|
||||
* 企业号第三方提供商授权:重定向URL使用weixin4j.properties#third.oauth.redirect.uri
|
||||
*
|
||||
* @see {@link #getThirdAuthorizeURL(String,String)}
|
||||
*
|
||||
* @return 请求授权的URL
|
||||
*/
|
||||
public String getThirdAuthorizeURL() {
|
||||
String corpId = account.getId();
|
||||
String redirectUri = Weixin4jConfigUtil.getValue("third.oauth.redirect.uri");
|
||||
return getThirdAuthorizeURL(corpId, redirectUri, "state");
|
||||
String redirectUri = Weixin4jConfigUtil
|
||||
.getValue("third.oauth.redirect.uri");
|
||||
return getThirdAuthorizeURL(redirectUri, "state");
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业号登陆授权
|
||||
*
|
||||
*
|
||||
* @param corpId
|
||||
* 企业号(提供商)的corpid
|
||||
* @param redirectUri
|
||||
@@ -98,13 +107,14 @@ public class OauthApi extends QyApi {
|
||||
* @see ProviderApi
|
||||
* @see {@link com.foxinmy.weixin4j.qy.WeixinSuiteProxy#getOUserInfoByCode(String)}
|
||||
* @see <a href=
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E7%99%BB%E5%BD%95%E6%8E%88%E6%9D%83">
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E6%88%90%E5%91%98%E7%99%BB%E5%BD%95%E6%8E%88%E6%9D%83">
|
||||
* 企业号第三方提供商授权</a>
|
||||
*/
|
||||
public String getThirdAuthorizeURL(String corpId, String redirectUri, String state) {
|
||||
public String getThirdAuthorizeURL(String redirectUri, String state) {
|
||||
String oauth_uri = getRequestUri("provider_oauth_uri");
|
||||
try {
|
||||
return String.format(oauth_uri, corpId, URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state);
|
||||
return String.format(oauth_uri, account.getId(),
|
||||
URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user