From 1dc58f00212c8ebd78e84b56b81dda0c980f87ba Mon Sep 17 00:00:00 2001 From: huazai Date: Mon, 17 Jan 2022 13:49:23 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20#2506=20=E3=80=90=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E3=80=91=E8=A1=A5=E5=85=85=E5=AE=8C=E5=96=84=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=94=A8=E6=88=B7encryptKey=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=9A=84=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wx/miniapp/api/WxMaInternetService.java | 22 +++++++++-- .../api/impl/WxMaInternetServiceImpl.java | 39 +++++++++++++++++-- .../api/impl/WxMaInternetServiceImplTest.java | 36 ++++++++++++++--- .../api/impl/WxMaUserServiceImplTest.java | 5 +++ 4 files changed, 91 insertions(+), 11 deletions(-) diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaInternetService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaInternetService.java index 8e6a3e2d8..4d055ba2d 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaInternetService.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaInternetService.java @@ -8,20 +8,36 @@ import me.chanjar.weixin.common.error.WxErrorException; * 【小程序-服务端-网络】网络相关接口. * 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html * + * * @author chutian0124 */ public interface WxMaInternetService { /** - * - * *
    * 获取用户encryptKey。 会获取用户最近3次的key,每个key的存活时间为3600s。
    * 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
    * 接口地址:POST https://api.weixin.qq.com/wxa/business/getuserencryptkey?access_token=ACCESS_TOKEN&openid=OPENID&signature=SIGNATURE&sig_method=hmac_sha256
+   * @param openid 用户的openid
+   * @param signature 用sessionkey对空字符串签名得到的结果
+   * @param sigMethod 签名方法,只支持 hmac_sha256
    * 
* * @return {@link WxMaInternetResponse} * @throws WxErrorException */ - WxMaInternetResponse getUserEncryptKey() throws WxErrorException; + WxMaInternetResponse getUserEncryptKey(String openid, String signature, String sigMethod) throws WxErrorException; + + /** + *
+   * 获取用户encryptKey。 会获取用户最近3次的key,每个key的存活时间为3600s。
+   * 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
+   * 接口地址:POST https://api.weixin.qq.com/wxa/business/getuserencryptkey?access_token=ACCESS_TOKEN&openid=OPENID&signature=SIGNATURE&sig_method=hmac_sha256
+   * @param openid 用户的openid
+   * @param sessionKey 用户的sessionKey
+   * 
+ * + * @return {@link WxMaInternetResponse} + * @throws WxErrorException + */ + WxMaInternetResponse getUserEncryptKey(String openid, String sessionKey) throws WxErrorException; } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaInternetServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaInternetServiceImpl.java index 8ee023baf..c72382836 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaInternetServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaInternetServiceImpl.java @@ -9,9 +9,12 @@ import lombok.RequiredArgsConstructor; import me.chanjar.weixin.common.enums.WxType; import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxErrorException; +import org.jetbrains.annotations.NotNull; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; /** - * * 服务端网络相关接口 * * @author chutian0124 @@ -21,9 +24,39 @@ import me.chanjar.weixin.common.error.WxErrorException; public class WxMaInternetServiceImpl implements WxMaInternetService { private final WxMaService wxMaService; + private String sha256(String data, String sessionKey) throws Exception { + Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); + SecretKeySpec secret_key = new SecretKeySpec(sessionKey.getBytes("UTF-8"), "HmacSHA256"); + sha256_HMAC.init(secret_key); + byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8")); + StringBuilder sb = new StringBuilder(); + for (byte item : array) { + sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); + } + return sb.toString().toUpperCase(); + } + @Override - public WxMaInternetResponse getUserEncryptKey() throws WxErrorException { - String responseContent = this.wxMaService.post(WxMaApiUrlConstants.Internet.GET_USER_ENCRYPT_KEY, ""); + public WxMaInternetResponse getUserEncryptKey(String openid, String signature, String sigMethod) throws WxErrorException { + String url = WxMaApiUrlConstants.Internet.GET_USER_ENCRYPT_KEY + "?openid=" + openid + "&signature=" + signature + "&sig_method=" + sigMethod; + return getWxMaInternetResponse(url); + } + + @Override + public WxMaInternetResponse getUserEncryptKey(String openid, String sessionKey) throws WxErrorException { + String signature = null; + try { + signature = sha256("", sessionKey); + } catch (Exception e) { + throw new WxErrorException("签名错误"); + } + String url = WxMaApiUrlConstants.Internet.GET_USER_ENCRYPT_KEY + "?sig_method=hmac_sha256&openid=" + openid + "&signature=" + signature; + return getWxMaInternetResponse(url); + } + + @NotNull + private WxMaInternetResponse getWxMaInternetResponse(String url) throws WxErrorException { + String responseContent = this.wxMaService.post(url, ""); WxMaInternetResponse response = WxMaGsonBuilder.create().fromJson(responseContent, WxMaInternetResponse.class); if (response.getErrcode() == -1) { throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); diff --git a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaInternetServiceImplTest.java b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaInternetServiceImplTest.java index ccc2f9c93..9b1ffa167 100644 --- a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaInternetServiceImplTest.java +++ b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaInternetServiceImplTest.java @@ -4,12 +4,15 @@ import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.bean.internet.WxMaInternetResponse; import cn.binarywang.wx.miniapp.test.ApiTestModule; import com.google.inject.Inject; -import me.chanjar.weixin.common.error.WxErrorException; import org.testng.annotations.Guice; import org.testng.annotations.Test; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; + +import static org.assertj.core.api.Assertions.assertThat; + /** - * * 服务端网络相关接口测试 * * @author chutian0124 @@ -21,9 +24,32 @@ public class WxMaInternetServiceImplTest { @Inject private WxMaService wxService; + private static String HMACSHA256(String data, String key) throws Exception { + Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); + SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256"); + sha256_HMAC.init(secret_key); + byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8")); + StringBuilder sb = new StringBuilder(); + for (byte item : array) { + sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); + } + return sb.toString().toUpperCase(); + } + @Test - public void testGetUserEncryptKey() throws WxErrorException { - WxMaInternetResponse response = this.wxService.getInternetService().getUserEncryptKey(); - System.out.println(response); + public void testGetUserEncryptKey() throws Exception { + String openid = "ogu-84hVFTbTt-myGisQESoDJ6BM"; + String signature = HMACSHA256("", "9ny8n3t0KULoi0deF7T9pw=="); + String sigMethod = "hmac_sha256"; + WxMaInternetResponse response = this.wxService.getInternetService().getUserEncryptKey(openid, signature, sigMethod); + assertThat(response).isNotNull(); + } + + @Test + public void testGetUserEncryptKey2() throws Exception { + String openid = "ogu-84hVFTbTt-myGisQESoDJ6BM"; + String sessionKey = "9ny8n3t0KULoi0deF7T9pw=="; + WxMaInternetResponse response = this.wxService.getInternetService().getUserEncryptKey(openid, sessionKey); + assertThat(response).isNotNull(); } } diff --git a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImplTest.java b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImplTest.java index dbee3f9c9..5b04d05f8 100644 --- a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImplTest.java +++ b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImplTest.java @@ -76,4 +76,9 @@ public class WxMaUserServiceImplTest { public void testGetNewPhoneNoInfo() throws Exception{ assertNotNull(wxService.getUserService().getNewPhoneNoInfo("test")); } + + @Test + public void testGetAccessToken() throws Exception{ + assertNotNull(wxService.getAccessToken(true)); + } }