🎨 #4124 【小程序】修复小程序登录态校验接口地址和签名生成问题
This commit is contained in:
@@ -109,7 +109,7 @@ public class WxMaUserServiceImpl implements WxMaUserService {
|
||||
|
||||
@Override
|
||||
public boolean checkSessionKey(String openid, String sessionKey) throws WxErrorException {
|
||||
String signature = SignUtils.createHmacSha256Sign(openid, sessionKey);
|
||||
String signature = SignUtils.createHmacSha256Sign("", sessionKey).toLowerCase();
|
||||
String url = String.format(CHECK_SESSION_KEY_URL, openid, signature);
|
||||
this.service.get(url, null);
|
||||
return true;
|
||||
|
||||
@@ -387,7 +387,7 @@ public class WxMaApiUrlConstants {
|
||||
String CODE_2_VERIFY_INFO_URL = "https://api.weixin.qq.com/wxa/sec/checkcode2verifyinfo";
|
||||
/** 检查登录态接口 */
|
||||
String CHECK_SESSION_KEY_URL =
|
||||
"https://api.weixin.qq.com/wxa/checksessionkey?openid=%s&signature=%s&sig_method=hmac_sha256";
|
||||
"https://api.weixin.qq.com/wxa/checksession?openid=%s&signature=%s&sig_method=hmac_sha256";
|
||||
}
|
||||
|
||||
public interface Ocr {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package cn.binarywang.wx.miniapp.api.impl;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.SignUtils;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* {@link WxMaUserServiceImpl} 检查登录态接口的单元测试。
|
||||
*/
|
||||
public class WxMaUserServiceImplCheckSessionKeyTest {
|
||||
|
||||
@Test
|
||||
public void shouldUseCheckSessionUrlAndSignEmptyStringWhenCheckingSessionKey() throws WxErrorException {
|
||||
WxMaService wxMaService = mock(WxMaService.class);
|
||||
when(wxMaService.get(anyString(), isNull())).thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}");
|
||||
|
||||
new WxMaUserServiceImpl(wxMaService).checkSessionKey("user-openid", "session-key");
|
||||
|
||||
ArgumentCaptor<String> url = ArgumentCaptor.forClass(String.class);
|
||||
verify(wxMaService).get(url.capture(), isNull());
|
||||
assertEquals(url.getValue(),
|
||||
"https://api.weixin.qq.com/wxa/checksession?openid=user-openid&signature="
|
||||
+ SignUtils.createHmacSha256Sign("", "session-key").toLowerCase()
|
||||
+ "&sig_method=hmac_sha256");
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
<test name="Unit_Test">
|
||||
<classes>
|
||||
<class name="cn.binarywang.wx.miniapp.api.impl.WxMaSubscribeServiceImplUrlTest"/>
|
||||
<class name="cn.binarywang.wx.miniapp.api.impl.WxMaUserServiceImplCheckSessionKeyTest"/>
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
||||
|
||||
Reference in New Issue
Block a user