微信cookie过期怎么弄,如何清除微信的cookie

首页 > 经验 > 作者:YD1662022-11-07 01:27:20

技术要点:

1、如何获取业务系统中用户身份标识

OPENID是微信公众号中用户的唯一标识,通过OPENID与业务系统中USRID建立一对一的对应关系,这样获取到了OPENID也就能知道所对应的USRID,从而获取对应权限的业务数据。

OPENID获取地址:

https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code

2、OPENID如何在页面建传递

由于请求获取OPENID是一个耗时的操作,为了加速页面响应速度,提升用户体验,不可能在每个页面中都请求一次。那么怎么保存传递这个OPENID呢,一种方法是用户首次访问时,调用微信接口获取OPENID,然后将OPENID写入服务器端的Cookies,通过cookie进行OPENID信息传递。下面讲解这种方式具体实施:

(1)添加拦截器,拦截需要获取OPENID进行身份识别的Controller,判断请求的cookies信息中是否含有openid,如果有直接放行,否则转向获取OPENID的url。

/* * <P>微信相关请求拦截器</P> * * @version 1.0 * @author wangpf * @date 2018年5月2日 下午7:07:39 */ public class oauth2Interceptor extends HandlerInterceptorAdapter { /** * 日志对象 */ protected Logger logger = LoggerFactory.getLogger(getClass()); // 从配置文件中读取域名 public static String domainUrl = Global.getConfig("wechat_domain_url"); public static String domain = "http://" Global.getConfig("wechat_domain"); public boolean preHandle(HttpServletrequest request, HttpServletResponse response, Object handler) throws Exception { String uri = request.getRequestURI(); logger.debug("OAuth2Interceptor:" uri); //兼容旧菜单url,直接放行 String code = request.getParameter("code"); if(StringUtils.isNotEmpty(code)) { return true; } //判断cookie中是否有openid,没有则转向获取openid的url Cookie[] cookies = request.getCookies(); String openid = null; // 判断cookie中是否存在openid 若存在则直接跳过,不存在则获取一次 if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("openid")) { openid = cookie.getValue(); } } } if (StringUtils.isEmpty(openid)) { response.sendRedirect(domainUrl "/f/wechat/oauth/oauth2Api?resultUrl=" domain uri); return false; } else { return true; } } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }

该Controller为获取微信用户OPENID的核心Controller,首次访问时调用微信接口获取OPENID,然后设置cookie到客户端(Cookie userCookie=new Cookie("openid",openid))。设置cookie过期时间为负数,表明当用户关闭浏览器的时候自动清空cookie。之后的用户请求可以直接从cookie中获取openid。

/** * <P>获取/保存OPENID相关Controller</P> * * @version 1.0 * @author wangpf * @date 2018年5月2日 下午7:20:38 */ @Controller @RequestMapping(value = "${frontPath}/wechat/oauth") public class OAuth2Controller extends BaseController { //从配置文件获取主域名 public static String domainUrl = Global.getConfig("wechat_domain_url"); /** * 拼装并转向获取OPENID的url * 示例: * https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx74dfe3afa0ebafe3 * &redirect_uri=http://www.xxxx.com/evcg/f/wechat/qrcode?response_type=code * &scope=snsapi_base&state=1#wechat_redirect * * @param request * @param resultUrl * @return */ @RequestMapping(value ="oauth2Api") public String oauth2API(HttpServletRequest request, @RequestParam String resultUrl) { StringBuffer redirectUrl = new StringBuffer(); if (resultUrl != null) { String backUrl = domainUrl "/f/wechat/oauth/oauth2MeUrl?oauth2url=" resultUrl; String appid = Global.getConfig("wechat_appid"); //组装获取OPENID的url redirectUrl.append("https://open.weixin.qq.com/connect/oauth2/authorize?"); redirectUrl.append("appid=").append(appid); redirectUrl.append("&redirect_uri=").append(backUrl); redirectUrl.append("&response_type=code"); redirectUrl.append("&scope=snsapi_base&state=1#wechat_redirect"); } //转向获取OPENID的url return "redirect:" redirectUrl.toString(); } /** * 获取微信用户标识OPENID * @param request * @param response * @param code * @param oauth2url * @return */ @RequestMapping(value = "oauth2MeUrl") public String oauth2MeUrl(HttpServletRequest request,HttpServletResponse response, @RequestParam String code, @RequestParam String oauth2url) { try { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); Httpsession session = request.getSession(); session.setAttribute("code",code); //通过code获取微信用户openid存储在cookie中的信息 String openid = WeixinUtil.getOpenId(code); Cookie userCookie=new Cookie("openid",openid); userCookie.setMaxAge(-1); userCookie.setPath("/"); response.addCookie(userCookie); } catch (Exception e) { e.printStackTrace(); } //转向原始请求url return "redirect:" oauth2url; } }

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.