一、写在前面
关于qq微信登录的原理之流我就不一一赘述了,对应的官网都有,在这里主要是展示我是怎么实现出来的,看了好几个博客,有的是直接复制官网的,有的不知道为什么实现不了。我只能保证我的这个是我实现后才贴出来的,本文有看不懂的地方请结合官网看。(话说我感觉我写博客废话好多)
二、准备工作
通过以下官网获得相应AppID和AppSecret以及对应的回调地址。
QQ登录官网:https://connect.qq.com
微信登录官网:https://open.weixin.qq.com
三、登录实现第三方
3.1、QQ授权登录实现
这里的实现主要用JS_SDK来实现,通过这种实现回调地址基本没有什么用,区别于这种实现方式:如何在自己的网站上实现QQ授权登录?。步骤就不一一说了,直接上代码(红色部分为qq授权相关的,里面的appid和回调地址改成自己申请时候的既可)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%String path = request.getContextPath() ;%>登录页
这里用到的是 QC.Login.showPopup,期间遇到了showPopup 这种不能回调的情况,参考了:QC.Login.showPopup可有回调? 。
qq授权后台处理思路:通过点击qq登录,登录成功后回调,在回调中通过 QC.api("get_user_info")获取登录后的信息,在后台通过qq的openid来查询数据库,若是库中有值,则直接进入登录成功流程,若是没有值则跳转到手机号注册流程。(回调地址里面基本为空)
3.2、微信授权登录实现
上面代码的蓝色部分即为微信登录的连接,相应地方改成申请的既可.
这里有三个地方需要注意:
1、地址需要改成转义后的,%3A%2F%2F就等于://这样的形式。
2、若是前面微信开放平台的是https,对应的回调也是https。
3、这里写的回调地址是http://www.xxxxxx.com/security/getWebchatCode.do,但是微信申请里写http://www.xxxxxx.com既可。
下面看看微信登录成功后的后台处理代码
/*** 微信登录获取code*/@RequestMapping(value = "/getWebchatCode.do")public ModelAndView getWebchatCode(HttpServletRequest request, HttpServletResponse response) {String code = request.getParameter("code");System.out.println("微信登录获取code=="+code);String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret=XXX&code="+code+"&grant_type=authorization_code";Mapmap = new HashMap ();map.put("from", code);try { JSONObject jb = HttpUtils.httpRequest(url,"GET",null);System.out.println("通过code获取token=="+jb.toString());String access_token= jb.getString("access_token"); JSONObject userinfo = HttpUtils.httpRequest("https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+appid+"","GET",null);System.out.println("通过token获取=="+userinfo.toString());String loginName= userinfo.getString("nickname");String openId=userinfo.getString("openid");Member member = memberService.selectOneByWeixinOpenId(openId);if (member != null ) { //通过qq唯一openID判断该qq是否之前用过,同时判断loginName是否唯一member.setLastLoginTime(new Date());memberService.update(member);// 设置线程变量 CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_USER_ID, member.getId());CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_MEMBER, member);CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_MANAGE_SHOP_ID, member.getManageShopId());// 单点登录CookieCookie cookie_sso = new Cookie(CurrentThreadContext.COOKIES_LOGIN_KEY, AESUtil.encrypt(loginName,PropertiesUtil.getInstance().getValue("security.cipher.key")));cookie_sso.setMaxAge(-1);cookie_sso.setDomain(PropertiesUtil.getInstance().getValue("security.root.domain"));cookie_sso.setPath("/");response.addCookie(cookie_sso);// 用户CookieloginName = DESUtil.strEnc(loginName, PropertiesUtil.getInstance().getValue("security.cipher.key"), "","");String shopFlage = "";if (member.getManageShopId() != null) {Shop t = new Shop();t.setId(member.getManageShopId());Shop shop = shopApiService.selectOne(t);if (shop != null && shop.getStatus() != null&& shop.getStatus().equals(ShopConstant.SHOP_STATUS_3)) {shopFlage = member.getManageShopId().toString();}}Cookie cookie_memberinfo = new Cookie(CurrentThreadContext.COOKIES_MEMBERINFO,loginName + "|" + shopFlage + "|" + member.getId());cookie_memberinfo.setMaxAge(-1);cookie_memberinfo.setDomain(PropertiesUtil.getInstance().getValue("security.root.domain"));cookie_memberinfo.setPath("/");response.addCookie(cookie_memberinfo);map.put("code", "200");map.put("msg", "登录成功");return new ModelAndView("redirect:/",map);} else {//qq授权的不存在,跳转到输入手机验证码的地方map.put("code", "101");Member membertemp = memberService.selectOneByLoginName(loginName);if(membertemp!=null){map.put("loginName", loginName+"_xima"+new Random().nextInt(1000));}else{map.put("loginName", loginName);}map.put("wqType", "weixin");map.put("openId", openId);/* map.put("msg", "用户名或密码不正确");*/return new ModelAndView("security/qwRegister",map);}} catch (Exception e) {// TODO Auto-generated catch block e.printStackTrace();}return new ModelAndView("security/login",map);}
主要是几次握手过程。
四、总结
qq微信的第三方登录按照官方文档一步一步实习还是比较容易的,有什么问题可以交流交流。
以上用到的连接若是对该作者造成影响的,可以联系删掉。