Skip to content

新增微信小程序服务端检查登录态(checkSessionKey)接口#3951

Open
Copilot wants to merge 2 commits intodevelopfrom
copilot/add-check-login-status-interface
Open

新增微信小程序服务端检查登录态(checkSessionKey)接口#3951
Copilot wants to merge 2 commits intodevelopfrom
copilot/add-check-login-status-interface

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 7, 2026

微信小程序服务端缺少检查登录态(api_checksessionkey)接口,该接口在虚拟支付等场景下构建用户签名前必须调用以验证 session_key 有效性。

变更内容

  • WxMaApiUrlConstants.User:新增 CHECK_SESSION_KEY_URL 常量(https://api.weixin.qq.com/wxa/checksessionkey
  • WxMaUserService:新增 checkSessionKey(String openid, String sessionKey) 方法接口,登录态有效返回 true,失效时抛出 WxErrorException(errcode 87009)
  • WxMaUserServiceImpl:实现方法,使用 HMAC_SHA256(openid, sessionKey) 构建签名并发起 GET 请求

使用示例

// 在虚拟支付构建用户签名前验证登录态
try {
    boolean valid = wxMaService.getUserService().checkSessionKey(openid, sessionKey);
    // valid == true,登录态有效,可继续构建签名
} catch (WxErrorException e) {
    // errcode=87009,session_key 已失效,需要重新登录
}

参考官方文档:api_checksessionkey

Copilot AI changed the title [WIP] Add check login status interface for WeChat mini program 新增微信小程序服务端检查登录态(checkSessionKey)接口 Apr 7, 2026
Copilot AI requested a review from binarywang April 7, 2026 03:38
@binarywang binarywang marked this pull request as ready for review April 7, 2026 09:10
Copilot AI review requested due to automatic review settings April 7, 2026 09:10
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 47add3a19b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


@Override
public boolean checkSessionKey(String openid, String sessionKey) throws WxErrorException {
String signature = SignUtils.createHmacSha256Sign(openid, sessionKey);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Sign empty payload when calling checkSessionKey

The checkSessionKey signature is computed as HMAC_SHA256(openid, sessionKey), but this API expects the signature generated by signing an empty string with session_key (hmac_sha256(session_key, "")). With the current implementation, even a valid session_key will consistently produce an invalid signature error (87009), so the method cannot successfully validate a real login session.

Useful? React with 👍 / 👎.

@augmentcode
Copy link
Copy Markdown

augmentcode bot commented Apr 7, 2026

🤖 Augment PR Summary

Summary: Adds a Mini Program server-side API wrapper to validate whether a stored login session (session_key) is still valid before sensitive operations (e.g., virtual payments).

Changes:

  • Introduced WxMaApiUrlConstants.User.CHECK_SESSION_KEY_URL for the check-session endpoint.
  • Added checkSessionKey(String openid, String sessionKey) to WxMaUserService.
  • Implemented the method in WxMaUserServiceImpl using an HMAC-SHA256-based signature and a GET request.
  • Added a TestNG test covering the error/exception path for invalid inputs.

🤖 Was this summary useful? React with 👍 or 👎

Copy link
Copy Markdown

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 2 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.


@Override
public boolean checkSessionKey(String openid, String sessionKey) throws WxErrorException {
String signature = SignUtils.createHmacSha256Sign(openid, sessionKey);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 checkSessionKey docs, the endpoint is /wxa/checksession and signature is hmac_sha256(session_key, "") (signing the empty string), so signing openid (and using /checksessionkey in the URL constant) will likely make this API always fail with 87009/invalid signature.

Severity: high

Other Locations
  • weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/constant/WxMaApiUrlConstants.java:371

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

assertNotNull(wxService.getAccessToken(true));
}

@Test(expectedExceptions = WxErrorException.class)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 true) or the specific errcode (e.g., 87009) that the Javadoc mentions.

Severity: medium

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 为 weixin-java-miniapp 补齐微信小程序服务端“检查登录态(api_checksessionkey)”能力,便于在虚拟支付等场景中在构建用户签名前验证 session_key 是否仍有效。

Changes:

  • WxMaApiUrlConstants.User 中新增 CHECK_SESSION_KEY_URL 接口常量。
  • WxMaUserService 新增 checkSessionKey(String openid, String sessionKey) 方法签名,并在 WxMaUserServiceImpl 中实现:按 HMAC_SHA256(openid, sessionKey) 生成签名并发起 GET 请求。
  • 新增对应的 TestNG 测试用例(当前为“期望抛出异常”的用例)。

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/constant/WxMaApiUrlConstants.java 增加 checksessionkey 的 URL 模板常量,供用户服务实现调用
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaUserService.java 对外暴露 checkSessionKey API,并补充使用语义与文档链接
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImpl.java 具体实现 checksessionkey 请求与签名构造逻辑
weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImplTest.java 增加 checkSessionKey 的测试覆盖(目前仅断言抛异常)


@Test(expectedExceptions = WxErrorException.class)
public void testCheckSessionKey() throws WxErrorException {
// 使用无效的 openid 和 sessionKey,预期微信服务端返回错误(如 errcode=87009)并抛出 WxErrorException
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的用例只断言会抛出 WxErrorException,但注释中写“如 errcode=87009”并不成立:传入的 openid 本身就是无效值时,更可能返回的是 openid 不合法等其它错误码,导致测试无法稳定验证“session_key 失效(87009)”这一场景。建议要么去掉对 87009 的暗示、仅说明预期抛异常;要么使用 test-config 中的真实 openid 并构造一个已失效/错误的 sessionKey,再断言具体 errorCode。

Suggested change
// 使用无效的 openid 和 sessionKey,预期微信服务端返回错误(如 errcode=87009)并抛出 WxErrorException
// 使用无效的 openid 和 sessionKey,预期微信服务端返回错误并抛出 WxErrorException

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

微信小程序登录状态-缺少检查登录状态接口

3 participants