新增微信小程序检查登录状态(checkSessionKey)接口实现
Agent-Logs-Url: https://github.com/binarywang/WxJava/sessions/d488dd87-3bc7-4498-a3df-99d106a68abe Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
99fcf33e68
commit
47add3a19b
@@ -108,4 +108,20 @@ public interface WxMaUserService {
|
||||
* @throws WxErrorException 调用微信接口失败时抛出
|
||||
*/
|
||||
WxMaCode2VerifyInfoResult getCode2VerifyInfo(String code, String checkcode) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 检查登录态(checkSessionKey).
|
||||
* <p>
|
||||
* 检验登录态是否有效,用于虚拟支付等场景构建用户签名前的登录态验证。
|
||||
* 登录态有效时返回 {@code true};登录态已失效时,微信服务端将返回错误码(如 87009),
|
||||
* 并以 {@link me.chanjar.weixin.common.error.WxErrorException} 的形式抛出。
|
||||
* </p>
|
||||
* 文档地址:<a href="https://developers.weixin.qq.com/miniprogram/dev/server/API/user-login/api_checksessionkey.html">检查登录态</a>
|
||||
*
|
||||
* @param openid 用户唯一标识符
|
||||
* @param sessionKey 用户的 session_key,通过 {@link #getSessionInfo(String)} 获取
|
||||
* @return 登录态有效时返回 {@code true}
|
||||
* @throws WxErrorException 登录态已失效或调用微信接口失败时抛出(失效时 errcode 为 87009)
|
||||
*/
|
||||
boolean checkSessionKey(String openid, String sessionKey) throws WxErrorException;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
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 class WxMaUserServiceImpl implements WxMaUserService {
|
||||
return WxMaCode2VerifyInfoResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkSessionKey(String openid, String sessionKey) throws WxErrorException {
|
||||
String signature = SignUtils.createHmacSha256Sign(openid, sessionKey);
|
||||
String url = String.format(CHECK_SESSION_KEY_URL, openid, signature);
|
||||
this.service.get(url, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -366,6 +366,9 @@ public class WxMaApiUrlConstants {
|
||||
String GET_PHONE_NUMBER_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber";
|
||||
/** 多端登录验证接口 */
|
||||
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";
|
||||
}
|
||||
|
||||
public interface Ocr {
|
||||
|
||||
@@ -81,4 +81,10 @@ public class WxMaUserServiceImplTest {
|
||||
public void testGetAccessToken() throws Exception{
|
||||
assertNotNull(wxService.getAccessToken(true));
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = WxErrorException.class)
|
||||
public void testCheckSessionKey() throws WxErrorException {
|
||||
// 使用无效的 openid 和 sessionKey,预期微信服务端返回错误(如 errcode=87009)并抛出 WxErrorException
|
||||
this.wxService.getUserService().checkSessionKey("invalid_openid", "invalid_session_key");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user