From c1b4f5b16a72d2a1a94feae38f5c42dd84c8df53 Mon Sep 17 00:00:00 2001 From: feidian108 <75881401@qq.com> Date: Fri, 24 Mar 2023 18:02:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0=E6=94=B6=E5=88=B0=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=9C=8D=E5=8A=A1=E5=95=86=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E6=B6=88=E6=81=AF=E8=A7=A3=E5=AF=86=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cp/bean/message/WxCpTpXmlMessage.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpTpXmlMessage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpTpXmlMessage.java index a3b68c234e..7fe4dacef4 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpTpXmlMessage.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpTpXmlMessage.java @@ -9,6 +9,8 @@ import me.chanjar.weixin.common.util.xml.IntegerArrayConverter; import me.chanjar.weixin.common.util.xml.StringArrayConverter; import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; +import me.chanjar.weixin.cp.config.WxCpTpConfigStorage; +import me.chanjar.weixin.cp.util.crypto.WxCpTpCryptUtil; import me.chanjar.weixin.cp.util.xml.XStreamTransformer; import java.io.Serializable; @@ -774,4 +776,20 @@ public static WxCpTpXmlMessage fromXml(String xml) { return xmlPackage; } + /** + * + * @param encryptedXml the encrypted xml + * @param wxCpTpConfigStorage the wx cp config storage + * @param timestamp the timestamp + * @param nonce the nonce + * @param msgSignature the msg signature + * @return the wx cp tp xml message + */ + public static WxCpTpXmlMessage fromEncryptedXml(String encryptedXml, WxCpTpConfigStorage wxCpTpConfigStorage, + String timestamp, String nonce, String msgSignature) { + WxCpTpCryptUtil cryptUtil = new WxCpTpCryptUtil(wxCpTpConfigStorage); + String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml); + log.debug("解密后的原始xml消息内容:{}", plainText); + return fromXml(plainText); + } } From 8730f5fbd404d29f33a4df5f6596e9d5c4a9ec50 Mon Sep 17 00:00:00 2001 From: feidian108 <75881401@qq.com> Date: Fri, 24 Mar 2023 18:10:39 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0=E6=9E=84=E9=80=A0=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9=E5=BA=94=E7=94=A8oauth2=E9=93=BE=E6=8E=A5=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cp/tp/service/WxCpTpOAuth2Service.java | 34 ++++++++++++++++++ .../weixin/cp/tp/service/WxCpTpService.java | 7 ++++ .../service/impl/BaseWxCpTpServiceImpl.java | 10 +++++- .../service/impl/WxCpTpOAuth2ServiceImpl.java | 35 +++++++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpOAuth2Service.java create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpOAuth2ServiceImpl.java diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpOAuth2Service.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpOAuth2Service.java new file mode 100644 index 0000000000..0746feea2b --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpOAuth2Service.java @@ -0,0 +1,34 @@ +package me.chanjar.weixin.cp.tp.service; + +/** + *
+ *   构造第三方应用oauth2链接
+ *   Created by feidian108 on 2023/3/24.
+ * 
+ *

+ * 企业微信服务商文档 + */ +public interface WxCpTpOAuth2Service { + + /** + *

+   *   构造第三方应用oauth2链接(静默授权)
+   * 
+ * @param redirectUri 授权后重定向的回调链接地址 + * @param state 重定向后state参数 + * @return url string + */ + String buildAuthorizeUrl(String redirectUri, String state); + + + /** + *
+   *   构造第三方应用oauth2链接
+   * 
+ * @param redirectUri 授权后重定向的回调链接地址 + * @param state 重定向后state参数 + * @param scope 应用授权作用域,snsapi_base:静默授权,snsapi_privateinfo:手动授权 + * @return url string + */ + String buildAuthorizeUrl(String redirectUri, String state, String scope); +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java index f86426876d..5dbcd8b297 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java @@ -631,4 +631,11 @@ public interface WxCpTpService { void setWxCpTpIdConverService(WxCpTpIdConvertService wxCpTpIdConvertService); + /** + * 构造第三方应用oauth2链接 + */ + WxCpTpOAuth2Service getWxCpTpOAuth2Service(); + + void setWxCpTpOAuth2Service(WxCpTpOAuth2Service wxCpTpOAuth2Service); + } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java index 06f919aa6b..68ac59bb24 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java @@ -56,7 +56,7 @@ public abstract class BaseWxCpTpServiceImpl implements WxCpTpService, Requ private WxCpTpEditionService wxCpTpEditionService = new WxCpTpEditionServiceImpl(this); private WxCpTpLicenseService wxCpTpLicenseService = new WxCpTpLicenseServiceImpl(this); private WxCpTpIdConvertService wxCpTpIdConvertService = new WxCpTpIdConvertServiceImpl(this); - + private WxCpTpOAuth2Service wxCpTpOAuth2Service = new WxCpTpOAuth2ServiceImpl(this); /** * 全局的是否正在刷新access token的锁. */ @@ -753,5 +753,13 @@ public void setWxCpTpIdConverService(WxCpTpIdConvertService wxCpTpIdConvertServi } + @Override + public WxCpTpOAuth2Service getWxCpTpOAuth2Service() { + return wxCpTpOAuth2Service; + } + @Override + public void setWxCpTpOAuth2Service(WxCpTpOAuth2Service wxCpTpOAuth2Service) { + this.wxCpTpOAuth2Service = wxCpTpOAuth2Service; + } } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpOAuth2ServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpOAuth2ServiceImpl.java new file mode 100644 index 0000000000..4b2e492463 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpOAuth2ServiceImpl.java @@ -0,0 +1,35 @@ +package me.chanjar.weixin.cp.tp.service.impl; + +import lombok.RequiredArgsConstructor; +import me.chanjar.weixin.common.util.http.URIUtil; +import me.chanjar.weixin.cp.tp.service.WxCpTpOAuth2Service; +import me.chanjar.weixin.cp.tp.service.WxCpTpService; + +import static me.chanjar.weixin.common.api.WxConsts.OAuth2Scope.SNSAPI_BASE; +import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.OAuth2.URL_OAUTH2_AUTHORIZE; + +@RequiredArgsConstructor +public class WxCpTpOAuth2ServiceImpl implements WxCpTpOAuth2Service { + + private final WxCpTpService mainService; + + + @Override + public String buildAuthorizeUrl(String redirectUri, String state) { + return this.buildAuthorizeUrl(redirectUri, state, SNSAPI_BASE); + } + + @Override + public String buildAuthorizeUrl(String redirectUri, String state, String scope) { + StringBuilder url = new StringBuilder(URL_OAUTH2_AUTHORIZE); + url.append("?appid=").append(this.mainService.getWxCpTpConfigStorage().getSuiteId()); + url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectUri)); + url.append("&response_type=code"); + url.append("&scope=").append(scope); + if (state != null) { + url.append("&state=").append(state); + } + url.append("#wechat_redirect"); + return url.toString(); + } +}