-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
新增微信小程序服务端检查登录态(checkSessionKey)接口 #3951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| import java.util.Map; | ||
|
|
||
| import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.User.CHECK_SESSION_KEY_URL; | ||
| import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.User.CODE_2_VERIFY_INFO_URL; | ||
| import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.User.GET_PHONE_NUMBER_URL; | ||
| import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.User.SET_USER_STORAGE; | ||
|
|
@@ -97,4 +98,12 @@ public WxMaCode2VerifyInfoResult getCode2VerifyInfo(String code, String checkcod | |
| return WxMaCode2VerifyInfoResult.fromJson(responseContent); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean checkSessionKey(String openid, String sessionKey) throws WxErrorException { | ||
| String signature = SignUtils.createHmacSha256Sign(openid, sessionKey); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImpl.java:103: According to the official Severity: high Other Locations
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage. |
||
| String url = String.format(CHECK_SESSION_KEY_URL, openid, signature); | ||
| this.service.get(url, null); | ||
| return true; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -81,4 +81,10 @@ public void testSetUserStorage() throws WxErrorException { | |||||
| public void testGetAccessToken() throws Exception{ | ||||||
| assertNotNull(wxService.getAccessToken(true)); | ||||||
| } | ||||||
|
|
||||||
| @Test(expectedExceptions = WxErrorException.class) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImplTest.java:85: This test only asserts an exception for obviously-invalid inputs, so it can pass even if the request URL/signature computation is incorrect. It also doesn’t validate the success path (returning Severity: medium 🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage. |
||||||
| public void testCheckSessionKey() throws WxErrorException { | ||||||
| // 使用无效的 openid 和 sessionKey,预期微信服务端返回错误(如 errcode=87009)并抛出 WxErrorException | ||||||
|
||||||
| // 使用无效的 openid 和 sessionKey,预期微信服务端返回错误(如 errcode=87009)并抛出 WxErrorException | |
| // 使用无效的 openid 和 sessionKey,预期微信服务端返回错误并抛出 WxErrorException |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
checkSessionKeysignature is computed asHMAC_SHA256(openid, sessionKey), but this API expects the signature generated by signing an empty string withsession_key(hmac_sha256(session_key, "")). With the current implementation, even a validsession_keywill consistently produce an invalid signature error (87009), so the method cannot successfully validate a real login session.Useful? React with 👍 / 👎.