整体流程:
微信授权登陆可分为四种:
1、移动端授权登陆;
2、网站应用端授权登陆(只支持扫码登陆, 如果是手机访问,那么就需要两部手机才能完成操作, 截图再扫描是不行的, 试过手机浏览器调起微信进行授权, 发现没有普遍应用, 只有京东、小米网页端有集成微信授权登陆,可能是有单独的接口 , 或者其他的交互流程及方法);
3、公众号、小程序授权登陆;
4、第三方平台(没用过)
一、 公众号授权流程及源码;(这些工具类可在SDK中查找)
1、客户服务端给出授权接口(这个地方给出微信访问客户服务端的回调地址, 注意这个地址是需要在公众平台 “网页授权”中配置的):
@RequestMapping(value = "/")
public String login(){
// String backUrl = "http://surenguangbo.com/suren/callBack";
String backUrl = "http://xxx.com/sur-api/web";
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
+ "appid="+ WXLogin.APPID
+ "&redirect_uri="+URLEncoder.encode(backUrl)
+ "&response_type=code"
+ "&scope=snsapi_userinfo"
+ "&state=STATE#wechat_redirect";
return "redirect:"+url;
}
2、此处直接获取code并请求微信拿到了客户的信息和openid 并给到H5页面, 这种方式的缺点只能将openid以get请求的形式, 拼接在后边;
@RequestMapping(value="web")
public String callBack(HttpServletRequest request) throws ClientProtocolException, IOException{
String code = request.getParameter("code");
// 获取openid
String url = "https://api.weixin.qq.com/sns/oauth2/access_token"
+ "?appid="+WXLogin.APPID
+ "&secret="+WXLogin.APPSECRET
+ "&code="+code
+ "&grant_type=authorization_code";
JSONObject wxuser = WXLogin.doGetjson(url);
String openid = wxuser.getString("openid");
System.out.println(openid);
String token = wxuser.getString("access_token");
System.out.println(token);
// 获取微信昵称、头像等用户信息
String infoUrl = "https://api.weixin.qq.com/sns/userinfo"
+ "?access_token="+token
+ "&openid="+openid
+ "&am