微信处理用户授权 openid的持久化 openid存储注意事项 openid缓存等

微信每次的访问 请求 首先经过授权 拿到code换取openid 将openid存入cookie 这以后的所有访问url 全部都从cookie中获取openid 如果没有在获取code换取

  • 可以解决的问题:

1,如何存储获取用户信息及调用第三方接口所需要的token.
2, 第三方页面授权,如何减少从微信服务器获取用户openid的次数以及减少获取用户信息的次数,加速第三方页面的加载速速。

  • 处理逻辑:
    首先,说一下我的测试逻辑:
    1,先用一个穿透工具,配置好自己的测试号域名和appid 等信息。
    2,编写一个WxServlet 拦截wx下的请求,经过处理和url拼接需要的参数进行重定向到具体业务目录上。
    3,启动服务,在微信开发者工具或者微信中打开预设好的url(“。。。。/wx/。。。”),进行访问/index。
    4,index是入口地址,可以先从cookie中获取openid,不过这个暂不使用,还是根据传过来的code在进行获取一次,这样保证每次访问都会进行授权验证,拿到授权后再获取openid,存入cookie设置过期时间,这里可以添加和保存用户信息的逻辑等。
    5,点击页面跳转到index1上。这个加了testUtil 这个工具类,首先获取cookie中的openid,如果没有,需要重新通过code获取。(这里也可以做成拦截器等,这里改造老项目就按需处理了)。
    6,通过index1和index2的相互跳转 获取openid都是可以轻松实现,设置时间可以测试,并亲测过安卓和苹果都没问题,页面进行分享后也是可以正常使用的。(主要处理之前openid拼接在url上的尴尬问题)

微信菜单入口

    /**
     * 微信菜单入口
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response)throws Exception {
//      CookieUtils.deleteCookie(request, response, "userOpenid");
        String cookieValue = CookieUtils.getCookieValue(request, "userOpenid");
        System.out.println("*********c=="+cookieValue);

        String code = request.getParameter("code");
        logger.info("--openid为空--,OAuth2.0页面授权CODE====="+ code);
        //1、如果不是微信中打开则返回 
        if (StringUtils.isNotBlank(code)) {
            JSONObject jsonObject = WeiXinUtil.getAuthorizationAccessToken(WeiXinConstant.getAppid(), WeiXinConstant.getAppsecret(),code);
            //保存一小时cookie
            CookieUtils.setCookie(request, response, "userOpenid", jsonObject.getString("openid"), 10, true);
        }else{
            if (StringUtils.isBlank(code)) {
                return null;
            }
        }
        return "/app/book/student/testIndex";
    }

测试页面1

    @RequestMapping("/index1")
    public String index1(HttpServletRequest request, HttpServletResponse response)throws Exception {
         String testUtil = testUtil(request, response);
         if(StringUtils.isNotBlank(testUtil)){
             return "redirect:"+testUtil;
         }
         String cookieValue = CookieUtils.getCookieValue(request, "userOpenid");
         if(StringUtils.isBlank(cookieValue)){
             System.out.println("opneid is null   写入日志   1111&&&&&");
         }else{
             System.out.println("openid not null   ---开始处理自己的逻辑11111---");
         }
        return "/app/book/student/testIndex1";
    }

测试页面2

    @RequestMapping("/index2")
    public String index2(HttpServletRequest request, HttpServletResponse response)throws Exception {
        String testUtil = testUtil(request, response);
         if(StringUtils.isNotBlank(testUtil)){
             return "redirect:"+testUtil;
         }
         String cookieValue = CookieUtils.getCookieValue(request, "userOpenid");
         if(StringUtils.isBlank(cookieValue)){
             System.out.println("opneid is null   *****写入日志   222");
         }else{
             System.out.println("openid not null*****开始处理自己的逻辑--2222---");
         }
         return "/app/book/student/testIndex2";
    }

处理cookie是否存在openid逻辑工具类

public String testUtil(HttpServletRequest request, HttpServletResponse response)throws Exception {

        String cookieValue = CookieUtils.getCookieValue(request, "userOpenid");
        System.out.println("*********cookieValue=="+cookieValue);
        String code = request.getParameter("code");
        System.out.println("*********code=="+code);
        String authorizationUrl = "";
        if(StringUtils.isBlank(cookieValue)){
             if(StringUtils.isBlank(code)){
                 String uri = request.getRequestURI();
                 String url = "http://" + WeiXinConstant.SERVER_HOST()+uri;
                 authorizationUrl = WeiXinUtil.getAuthorizationCode(WeiXinConstant.getAppid(),url.toString(), WeiXinConstant.SCOPE_SNSAPI_USERINFO);
                 System.out.println("**********Url=="+authorizationUrl);
             }else{
                 JSONObject jsonObject = WeiXinUtil.getAuthorizationAccessToken(WeiXinConstant.getAppid(), WeiXinConstant.getAppsecret(),code);
                 //保存一小时cookie 3600
                 CookieUtils.setCookie(request, response, "userOpenid", jsonObject.getString("openid"), 20, true);
             }
         }
        return authorizationUrl;
    }

通过servlet及线程定时获取access_token并使用的文章

https://blog.csdn.net/alen_en/article/details/80975014

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值