授权登录的原理大致一样.
微信授权和支付宝的差别首先是回调地址,微信的是目标页面uri,支付宝是需要在开放平台设置回调网关,
String realPath = req.getRequestURI() + queryString;
resp.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid="
+ WechatBaseData.APPID
+ "&redirect_uri="
+ URLEncoder.encode("https://" + custom_wechat_domain + realPath, "utf-8")
+ "&response_type=code&scope=snsapi_userinfo#wechat_redirect");
当微信收到请求后,会访问设置的redirect_uri,并且携带code参数
String code = request.getParameter("code");
String result = RequestUrlTool.get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" +
WechatBaseData.APPID + "&secret=" + WechatBaseData.APPSECRET + "&code=" + code + "&grant_type=authorization_code");
就可以得到用户的 unionid 和 openid