网站首页 > 资源文章 正文
技术要点:
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; } }
猜你喜欢
- 2024-09-09 什么是双子座扩展?让谷歌的聊天机器人比ChatGPT更智能
- 2024-09-09 生产环境url告警检测--通过curl来获取http各阶段时间
- 2024-09-09 虚拟主机可以安装宝塔吗?(虚拟主机可以安装宝塔吗)
- 2024-09-09 SEO必知:如何将顶级域名做301重定向到www二级域名
- 2024-09-09 网站页面绝对路径相对路径有哪些优缺点?
- 2024-09-09 nPlayer Plus| 付费软件已购共享(nplayer付费版分享)
- 2024-09-09 Java面试笔试必考题总结(java面试题整理)
- 2024-09-09 教学笔记:HTTP状态码之300段系列码
- 2024-09-09 nginx实现url重写实例参考(nginx url重定向)
- 2024-09-09 CC攻击原理及防范新思路(cc攻击是针对什么协议)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- 电脑显示器花屏 (79)
- 403 forbidden (65)
- linux怎么查看系统版本 (54)
- 补码运算 (63)
- 缓存服务器 (61)
- 定时重启 (59)
- plsql developer (73)
- 对话框打开时命令无法执行 (61)
- excel数据透视表 (72)
- oracle认证 (56)
- 网页不能复制 (84)
- photoshop外挂滤镜 (58)
- 网页无法复制粘贴 (55)
- vmware workstation 7 1 3 (78)
- jdk 64位下载 (65)
- phpstudy 2013 (66)
- 卡通形象生成 (55)
- psd模板免费下载 (67)
- shift (58)
- localhost打不开 (58)
- 检测代理服务器设置 (55)
- frequency (66)
- indesign教程 (55)
- 运行命令大全 (61)
- ping exe (64)
本文暂时没有评论,来添加一个吧(●'◡'●)