收藏
回答

微信公众号H5授权登录code问题,目前小米手机几天获取的都是同一个code?其他手机正常

@GetMapping("/wxlogin")
    public ModelAndView wxChatlogin(HttpServletRequest  request) {
        String referralCode = request.getParameter("referralCode");
        try {
            String redirect_uri = URLEncoder.encode(WeiXinConfig.getCallBackUrl(), "UTF-8");
            String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
                    + "appid=" + WeiXinConfig.getAppId()
                    + "&redirect_uri=" + redirect_uri
                    + "&response_type=code"
                    + "&scope=snsapi_userinfo"
                    + "&state=" + URLEncoder.encode(referralCode, "UTF-8")
                    + "&connect_redirect=1#wechat_redirect";
//            return AjaxResult.success("请求成功", url);
            return jsonResultMav(url);
        } catch (Exception e) {
            e.printStackTrace();
//            return AjaxResult.error("微信授权登录失败");
            throw new ParameterException("微信授权登录失败");
        }
    }
//回调
@GetMapping("/callBack")
    public void callBack(HttpServletRequest request, HttpServletResponse response) {
        String code = request.getParameter("code");
        String state = request.getParameter("state");
        log.info("微信授权登录回调开始,参数为:code = {}, state = {}", code, state);
        // 1. 检查参数
        if (StringUtil.isEmpty(code)) {
            throw new ParameterException("缺少必要参数: code");
        }
        Map map = new HashMap<>(16);
        // 2. 检查Redis中是否已有缓存
        String cacheKey = "wechat:auth:code:" + code;
//        String cachedData = redisTemplate.opsForValue().get(cacheKey).toString();
        // 改为安全的写法
        Object cachedValue = redisTemplate.opsForValue().get(cacheKey);
        String cachedData = cachedValue != null ? cachedValue.toString() : null;
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
        + "appid=" + WeiXinConfig.getAppId()
        + "&secret=" + WeiXinConfig.getSecret()
        + "&code=" + code
        + "&grant_type=authorization_code";
com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(HttpUtil.get(url));
Integer errcode = (Integer) jsonObject.get("errcode");
if (errcode != null) {
    String errmsg = jsonObject.getString("errmsg");
    throw new ParameterException("【微信回调错误】错误码:" + errcode + ",错误信息:" + errmsg);
}


目前其他手机能正常使用,小米手机H5授权一直code都是重复的,几天都是没有改变过。微信版本:8.0.63。手机系统:XiaoMi HyerOs.

微信授权登录回调开始,参数为:code = 001vdUkl2162og4aFgnl2MorkE1vdUks, state = 

ExceptionHandler=【微信回调错误】错误码:40163,错误信息:code been used, rid: 68da3fa9-3ee801b2-4f941548

[2025-09-29 16:13:29:088][ INFO][com.hyd.hyd_demo.gxetu.base.interceptor.SystemInterceptors]:38 - > SystemInterceptors 获取全路径=http://www.xxxxxxxxxxxxx.top/wxmp/callBack?code=001vdUkl2162og4aFgnl2MorkE1vdUks&state=

2025-09-29 16:13:29.089 [22202] [   http-nio-8198-exec-341] [ INFO ] c.h.h.w.w.w.controller.LoginController   : 微信授权登录回调开始,参数为:code = 001vdUkl2162og4aFgnl2MorkE1vdUks, state = 

最后一次编辑于  2025-09-29
回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容