🎨 【小程序】修复 getUserEncryptKey 签名计算问题
This commit is contained in:
@@ -9,7 +9,6 @@ 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.apache.commons.codec.binary.Base64;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
@@ -27,7 +26,7 @@ public class WxMaInternetServiceImpl implements WxMaInternetService {
|
||||
|
||||
private String sha256(String data, String sessionKey) throws Exception {
|
||||
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
|
||||
SecretKeySpec secret_key = new SecretKeySpec(Base64.decodeBase64(sessionKey), "HmacSHA256");
|
||||
SecretKeySpec secret_key = new SecretKeySpec(sessionKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
||||
sha256_HMAC.init(secret_key);
|
||||
byte[] array = sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package cn.binarywang.wx.miniapp.api.impl;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 服务端网络相关接口签名测试.
|
||||
*/
|
||||
public class WxMaInternetServiceImplSignatureTest {
|
||||
@Test
|
||||
public void testGetUserEncryptKeySignatureUsesRawSessionKey() throws Exception {
|
||||
WxMaService wxMaService = mock(WxMaService.class);
|
||||
when(wxMaService.post(anyString(), eq(""))).thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}");
|
||||
|
||||
String openid = "ogu-84hVFTbTt-myGisQESoDJ6BM";
|
||||
String sessionKey = "9ny8n3t0KULoi0deF7T9pw==";
|
||||
WxMaInternetServiceImpl service = new WxMaInternetServiceImpl(wxMaService);
|
||||
|
||||
service.getUserEncryptKey(openid, sessionKey);
|
||||
|
||||
ArgumentCaptor<String> urlCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(wxMaService).post(urlCaptor.capture(), eq(""));
|
||||
assertThat(urlCaptor.getValue())
|
||||
.contains("openid=" + openid)
|
||||
.contains("signature=A6782009C273FDF75CAD421C9D3EAF6CA8715C1B15CB1E130EF40FE7901A9772");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user