Skip to content

Commit 36028b8

Browse files
authored
🆕 #2962 【企业微信】 增加构造第三方应用oauth2链接的方法
1 parent bae84e1 commit 36028b8

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpTpXmlMessage.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import me.chanjar.weixin.common.util.xml.IntegerArrayConverter;
1010
import me.chanjar.weixin.common.util.xml.StringArrayConverter;
1111
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
12+
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
13+
import me.chanjar.weixin.cp.util.crypto.WxCpTpCryptUtil;
1214
import me.chanjar.weixin.cp.util.xml.XStreamTransformer;
1315

1416
import java.io.Serializable;
@@ -774,4 +776,20 @@ public static WxCpTpXmlMessage fromXml(String xml) {
774776
return xmlPackage;
775777
}
776778

779+
/**
780+
*
781+
* @param encryptedXml the encrypted xml
782+
* @param wxCpTpConfigStorage the wx cp config storage
783+
* @param timestamp the timestamp
784+
* @param nonce the nonce
785+
* @param msgSignature the msg signature
786+
* @return the wx cp tp xml message
787+
*/
788+
public static WxCpTpXmlMessage fromEncryptedXml(String encryptedXml, WxCpTpConfigStorage wxCpTpConfigStorage,
789+
String timestamp, String nonce, String msgSignature) {
790+
WxCpTpCryptUtil cryptUtil = new WxCpTpCryptUtil(wxCpTpConfigStorage);
791+
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
792+
log.debug("解密后的原始xml消息内容:{}", plainText);
793+
return fromXml(plainText);
794+
}
777795
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package me.chanjar.weixin.cp.tp.service;
2+
3+
/**
4+
* <pre>
5+
* 构造第三方应用oauth2链接
6+
* Created by feidian108 on 2023/3/24.
7+
* </pre>
8+
* <p>
9+
* <a href="https://developer.work.weixin.qq.com/document/path/91120">企业微信服务商文档</a>
10+
*/
11+
public interface WxCpTpOAuth2Service {
12+
13+
/**
14+
* <pre>
15+
* 构造第三方应用oauth2链接(静默授权)
16+
* </pre>
17+
* @param redirectUri 授权后重定向的回调链接地址
18+
* @param state 重定向后state参数
19+
* @return url string
20+
*/
21+
String buildAuthorizeUrl(String redirectUri, String state);
22+
23+
24+
/**
25+
* <pre>
26+
* 构造第三方应用oauth2链接
27+
* </pre>
28+
* @param redirectUri 授权后重定向的回调链接地址
29+
* @param state 重定向后state参数
30+
* @param scope 应用授权作用域,snsapi_base:静默授权,snsapi_privateinfo:手动授权
31+
* @return url string
32+
*/
33+
String buildAuthorizeUrl(String redirectUri, String state, String scope);
34+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,4 +631,11 @@ public interface WxCpTpService {
631631

632632
void setWxCpTpIdConverService(WxCpTpIdConvertService wxCpTpIdConvertService);
633633

634+
/**
635+
* 构造第三方应用oauth2链接
636+
*/
637+
WxCpTpOAuth2Service getWxCpTpOAuth2Service();
638+
639+
void setWxCpTpOAuth2Service(WxCpTpOAuth2Service wxCpTpOAuth2Service);
640+
634641
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public abstract class BaseWxCpTpServiceImpl<H, P> implements WxCpTpService, Requ
5656
private WxCpTpEditionService wxCpTpEditionService = new WxCpTpEditionServiceImpl(this);
5757
private WxCpTpLicenseService wxCpTpLicenseService = new WxCpTpLicenseServiceImpl(this);
5858
private WxCpTpIdConvertService wxCpTpIdConvertService = new WxCpTpIdConvertServiceImpl(this);
59-
59+
private WxCpTpOAuth2Service wxCpTpOAuth2Service = new WxCpTpOAuth2ServiceImpl(this);
6060
/**
6161
* 全局的是否正在刷新access token的锁.
6262
*/
@@ -753,5 +753,13 @@ public void setWxCpTpIdConverService(WxCpTpIdConvertService wxCpTpIdConvertServi
753753
}
754754

755755

756+
@Override
757+
public WxCpTpOAuth2Service getWxCpTpOAuth2Service() {
758+
return wxCpTpOAuth2Service;
759+
}
756760

761+
@Override
762+
public void setWxCpTpOAuth2Service(WxCpTpOAuth2Service wxCpTpOAuth2Service) {
763+
this.wxCpTpOAuth2Service = wxCpTpOAuth2Service;
764+
}
757765
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package me.chanjar.weixin.cp.tp.service.impl;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import me.chanjar.weixin.common.util.http.URIUtil;
5+
import me.chanjar.weixin.cp.tp.service.WxCpTpOAuth2Service;
6+
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
7+
8+
import static me.chanjar.weixin.common.api.WxConsts.OAuth2Scope.SNSAPI_BASE;
9+
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.OAuth2.URL_OAUTH2_AUTHORIZE;
10+
11+
@RequiredArgsConstructor
12+
public class WxCpTpOAuth2ServiceImpl implements WxCpTpOAuth2Service {
13+
14+
private final WxCpTpService mainService;
15+
16+
17+
@Override
18+
public String buildAuthorizeUrl(String redirectUri, String state) {
19+
return this.buildAuthorizeUrl(redirectUri, state, SNSAPI_BASE);
20+
}
21+
22+
@Override
23+
public String buildAuthorizeUrl(String redirectUri, String state, String scope) {
24+
StringBuilder url = new StringBuilder(URL_OAUTH2_AUTHORIZE);
25+
url.append("?appid=").append(this.mainService.getWxCpTpConfigStorage().getSuiteId());
26+
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectUri));
27+
url.append("&response_type=code");
28+
url.append("&scope=").append(scope);
29+
if (state != null) {
30+
url.append("&state=").append(state);
31+
}
32+
url.append("#wechat_redirect");
33+
return url.toString();
34+
}
35+
}

0 commit comments

Comments
 (0)