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

阅读: 评论:0

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

微信处理用户授权 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 = CookieValue(request, "userOpenid");System.out.println("*********c=="+cookieValue);String code = Parameter("code");logger.info("--openid为空--,OAuth2.0页面授权CODE====="+ code);//1、如果不是微信中打开则返回 if (StringUtils.isNotBlank(code)) {JSONObject jsonObject = Appid(), Appsecret(),code);//保存一小时cookieCookieUtils.setCookie(request, response, "userOpenid", String("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 = CookieValue(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 = CookieValue(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 = CookieValue(request, "userOpenid");System.out.println("*********cookieValue=="+cookieValue);String code = Parameter("code");System.out.println("*********code=="+code);String authorizationUrl = "";if(StringUtils.isBlank(cookieValue)){if(StringUtils.isBlank(code)){String uri = RequestURI();String url = "" + WeiXinConstant.SERVER_HOST()+uri;authorizationUrl = Appid(),String(), WeiXinConstant.SCOPE_SNSAPI_USERINFO);System.out.println("**********Url=="+authorizationUrl);}else{JSONObject jsonObject = Appid(), Appsecret(),code);//保存一小时cookie 3600CookieUtils.setCookie(request, response, "userOpenid", String("openid"), 20, true);}}return authorizationUrl;}

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

本文发布于:2024-01-31 05:54:29,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170665167226020.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:缓存   持久   注意事项   用户   openid
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23