java实现微信公众号授权登录获取用户信息流程
2018-01-19 22:05
1541 查看
参考地址微信公众号开发文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432
前提:需要申请认证的微信公众号;获取对应的APPID和APPSECRET;并且还需要获取到用户信息权限(点击“修改“添加服务器的域名地址),前期工作安装测试账号为例给大家展示下:
1)、公众测试账号获取
访问上面的连接,选择“接口测试号申请”获得直接打开http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index通过微信客户端扫码登录即可登录。
登录完即可获取到一个测试公众账号的信息。主要有appId和appsecret两个参数,这将唯一标示一个公众号,并且需要将他们作为参数获取用户的信息。
2)、关注公众号
用户只有关注了这个公众号了,才能通过打开有公众号信息的链接去授权第三方登录,并获取用户信息的操作。故我们还需要用我们的微信关注微信号,操作如下:
还是刚刚那个登录成功后跳转的页面,我们可以看到,该页面有一个二维码,我们可以通过扫描该二维码进行关注,关注成功在右边的“用户列表”会多一个用户的信息。如下图所示:
3)配置回调函数
我们在微信客户端访问第三方网页(即我们自己的网页)
4000
的时候,我们可以通过微信网页授权机制,我们不仅要有前面获取到的appid和appsecret还需要有当用户授权之后,回调的域名设置,即用户授权后,页面会跳转到哪里。具体的配置如下:
还是在刚刚的页面,有一个“网页授权获取用户基本信息”,点击后面的修改
填写回调的域名:
如果你的网址没有被列入过黑名单,就会在顶部出现
然后,域名配置就成功了!可以进行开发了。
一。授权开发的流程(详情的东西请以官网为准,在此就不多说了):具体而言,网页授权流程分为四步:
1、引导用户进入授权页面同意授权,获取code
2、通过code换取网页授权access_token(与基础支持中的access_token不同)
3、如果需要,开发者可以刷新网页授权access_token,避免过期
4、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制)
二.按照如上流程我就不多说废话直接粘代码供大家参考,请大家多多指教(代码里的所有APPID,APPSECRET都是使用的官网提供测试账号)
1.设计一个公用网络请求工具类:
package com.wxutil.auth;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.util.DigestUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class WXAuthUtil {
public static final String APPID="wx9b39273dce55dc2f";
public static final String APPSECRET ="06f175852111045f81ebf62db28a44a0";
private static final String TOKEN = "immco";
public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException {
JSONObject jsonObject =null;
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet =new HttpGet(url);
HttpResponse response = client.execute(httpGet);
HttpEntity entity =response.getEntity();
if(entity!=null)
{
//把返回的结果转换为JSON对象
String result =EntityUtils.toString(entity, "UTF-8");
jsonObject =JSON.parseObject(result);
}
return jsonObject;
}
}
2.微信登录的实现类(里面包含引导action和确认登录后的回调函数):
package com.wxutil.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.alibaba.fastjson.JSONObject;
import com.wxutil.auth.SHA1;
import com.wxutil.auth.WXAuthUtil;
/**
* @author lbh
* @date 创建时间:2018年1月18日 下午12:35:11
* @version 1.0
* @parameter
* @since
* @return
*/
@Controller
@RequestMapping("/wx")
public class WXLoginController {
private static final Logger logger = Logger.getLogger(WXLoginController.class);
/**
* 公众号微信登录授权
* @param request
* @param response
* @return
* @throws ParseException
* @author lbh
* @date 创建时间:2018年1月18日 下午7:33:59
* @parameter
*/
@RequestMapping(value = "/wxLogin", method = RequestMethod.GET)
public String wxLogin(HttpServletRequest request,
HttpServletResponse response)
throws ParseException {
//这个url的域名必须要进行再公众号中进行注册验证,这个地址是成功后的回调地址
String backUrl="http://d84e26b6.ngrok.io/WX_auth/wx/callBack";
// 第一步:用户同意授权,获取code
String url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+WXAuthUtil.APPID
+ "&redirect_uri="+URLEncoder.encode(backUrl)
+ "&response_type=code"
+ "&scope=snsapi_userinfo"
+ "&state=STATE#wechat_redirect";
logger.info("forward重定向地址{" + url + "}");
//response.sendRedirect(url);
return "redirect:"+url;//必须重定向,否则不能成功
}
/**
* 公众号微信登录授权回调函数
* @param modelMap
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
* @author lbh
* @date 创建时间:2018年1月18日 下午7:33:53
* @parameter
*/
@RequestMapping(value = "/callBack", method = RequestMethod.GET)
public String callBack(ModelMap modelMap,HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
* start 获取微信用户基本信息
*/
String code =req.getParameter("code");
//第二步:通过code换取网页授权access_token
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WXAuthUtil.APPID
+ "&secret="+WXAuthUtil.APPSECRET
+ "&code="+code
+ "&grant_type=authorization_code";
System.out.println("url:"+url);
JSONObject jsonObject = WXAuthUtil.doGetJson(url);
/*
{ "access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
}
*/
String openid = jsonObject.getString("openid");
String access_token = jsonObject.getString("access_token");
String refresh_token = jsonObject.getString("refresh_token");
//第五步验证access_token是否失效;展示都不需要
String chickUrl="https://api.weixin.qq.com/sns/auth?access_token="+access_token+"&openid="+openid;
JSONObject chickuserInfo = WXAuthUtil.doGetJson(chickUrl);
System.out.println(chickuserInfo.toString());
if(!"0".equals(chickuserInfo.getString("errcode"))){
// 第三步:刷新access_token(如果需要)-----暂时没有使用,参考文档https://mp.weixin.qq.com/wiki,
String refreshTokenUrl="https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="+openid+"&grant_type=refresh_token&refresh_token="+refresh_token;
JSONObject refreshInfo = WXAuthUtil.doGetJson(chickUrl);
/*
* { "access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE" }
*/
System.out.println(refreshInfo.toString());
access_token=refreshInfo.getString("access_token");
}
// 第四步:拉取用户信息(需scope为 snsapi_userinfo)
String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token
+ "&openid="+openid
+ "&lang=zh_CN";
System.out.println("infoUrl:"+infoUrl);
JSONObject userInfo = WXAuthUtil.doGetJson(infoUrl);
/*
{ "openid":" OPENID",
" nickname": NICKNAME,
"sex":"1",
"province":"PROVINCE"
"city":"CITY",
"country":"COUNTRY",
"headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
"privilege":[ "PRIVILEGE1" "PRIVILEGE2" ],
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
*/
System.out.println("JSON-----"+userInfo.toString());
System.out.println("名字-----"+userInfo.getString("nickname"));
System.out.println("头像-----"+userInfo.getString("headimgurl"));
/*
* end 获取微信用户基本信息
*/
//获取到用户信息后就可以进行重定向,走自己的业务逻辑了。。。。。。
//接来的逻辑就是你系统逻辑了,请自由发挥
return "login";
}
}
前提:需要申请认证的微信公众号;获取对应的APPID和APPSECRET;并且还需要获取到用户信息权限(点击“修改“添加服务器的域名地址),前期工作安装测试账号为例给大家展示下:
1)、公众测试账号获取
访问上面的连接,选择“接口测试号申请”获得直接打开http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index通过微信客户端扫码登录即可登录。
登录完即可获取到一个测试公众账号的信息。主要有appId和appsecret两个参数,这将唯一标示一个公众号,并且需要将他们作为参数获取用户的信息。
2)、关注公众号
用户只有关注了这个公众号了,才能通过打开有公众号信息的链接去授权第三方登录,并获取用户信息的操作。故我们还需要用我们的微信关注微信号,操作如下:
还是刚刚那个登录成功后跳转的页面,我们可以看到,该页面有一个二维码,我们可以通过扫描该二维码进行关注,关注成功在右边的“用户列表”会多一个用户的信息。如下图所示:
3)配置回调函数
我们在微信客户端访问第三方网页(即我们自己的网页)
4000
的时候,我们可以通过微信网页授权机制,我们不仅要有前面获取到的appid和appsecret还需要有当用户授权之后,回调的域名设置,即用户授权后,页面会跳转到哪里。具体的配置如下:
还是在刚刚的页面,有一个“网页授权获取用户基本信息”,点击后面的修改
填写回调的域名:
如果你的网址没有被列入过黑名单,就会在顶部出现
然后,域名配置就成功了!可以进行开发了。
一。授权开发的流程(详情的东西请以官网为准,在此就不多说了):具体而言,网页授权流程分为四步:
1、引导用户进入授权页面同意授权,获取code
2、通过code换取网页授权access_token(与基础支持中的access_token不同)
3、如果需要,开发者可以刷新网页授权access_token,避免过期
4、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制)
二.按照如上流程我就不多说废话直接粘代码供大家参考,请大家多多指教(代码里的所有APPID,APPSECRET都是使用的官网提供测试账号)
1.设计一个公用网络请求工具类:
package com.wxutil.auth;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.util.DigestUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class WXAuthUtil {
public static final String APPID="wx9b39273dce55dc2f";
public static final String APPSECRET ="06f175852111045f81ebf62db28a44a0";
private static final String TOKEN = "immco";
public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException {
JSONObject jsonObject =null;
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet =new HttpGet(url);
HttpResponse response = client.execute(httpGet);
HttpEntity entity =response.getEntity();
if(entity!=null)
{
//把返回的结果转换为JSON对象
String result =EntityUtils.toString(entity, "UTF-8");
jsonObject =JSON.parseObject(result);
}
return jsonObject;
}
}
2.微信登录的实现类(里面包含引导action和确认登录后的回调函数):
package com.wxutil.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.alibaba.fastjson.JSONObject;
import com.wxutil.auth.SHA1;
import com.wxutil.auth.WXAuthUtil;
/**
* @author lbh
* @date 创建时间:2018年1月18日 下午12:35:11
* @version 1.0
* @parameter
* @since
* @return
*/
@Controller
@RequestMapping("/wx")
public class WXLoginController {
private static final Logger logger = Logger.getLogger(WXLoginController.class);
/**
* 公众号微信登录授权
* @param request
* @param response
* @return
* @throws ParseException
* @author lbh
* @date 创建时间:2018年1月18日 下午7:33:59
* @parameter
*/
@RequestMapping(value = "/wxLogin", method = RequestMethod.GET)
public String wxLogin(HttpServletRequest request,
HttpServletResponse response)
throws ParseException {
//这个url的域名必须要进行再公众号中进行注册验证,这个地址是成功后的回调地址
String backUrl="http://d84e26b6.ngrok.io/WX_auth/wx/callBack";
// 第一步:用户同意授权,获取code
String url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+WXAuthUtil.APPID
+ "&redirect_uri="+URLEncoder.encode(backUrl)
+ "&response_type=code"
+ "&scope=snsapi_userinfo"
+ "&state=STATE#wechat_redirect";
logger.info("forward重定向地址{" + url + "}");
//response.sendRedirect(url);
return "redirect:"+url;//必须重定向,否则不能成功
}
/**
* 公众号微信登录授权回调函数
* @param modelMap
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
* @author lbh
* @date 创建时间:2018年1月18日 下午7:33:53
* @parameter
*/
@RequestMapping(value = "/callBack", method = RequestMethod.GET)
public String callBack(ModelMap modelMap,HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
* start 获取微信用户基本信息
*/
String code =req.getParameter("code");
//第二步:通过code换取网页授权access_token
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WXAuthUtil.APPID
+ "&secret="+WXAuthUtil.APPSECRET
+ "&code="+code
+ "&grant_type=authorization_code";
System.out.println("url:"+url);
JSONObject jsonObject = WXAuthUtil.doGetJson(url);
/*
{ "access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
}
*/
String openid = jsonObject.getString("openid");
String access_token = jsonObject.getString("access_token");
String refresh_token = jsonObject.getString("refresh_token");
//第五步验证access_token是否失效;展示都不需要
String chickUrl="https://api.weixin.qq.com/sns/auth?access_token="+access_token+"&openid="+openid;
JSONObject chickuserInfo = WXAuthUtil.doGetJson(chickUrl);
System.out.println(chickuserInfo.toString());
if(!"0".equals(chickuserInfo.getString("errcode"))){
// 第三步:刷新access_token(如果需要)-----暂时没有使用,参考文档https://mp.weixin.qq.com/wiki,
String refreshTokenUrl="https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="+openid+"&grant_type=refresh_token&refresh_token="+refresh_token;
JSONObject refreshInfo = WXAuthUtil.doGetJson(chickUrl);
/*
* { "access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE" }
*/
System.out.println(refreshInfo.toString());
access_token=refreshInfo.getString("access_token");
}
// 第四步:拉取用户信息(需scope为 snsapi_userinfo)
String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token
+ "&openid="+openid
+ "&lang=zh_CN";
System.out.println("infoUrl:"+infoUrl);
JSONObject userInfo = WXAuthUtil.doGetJson(infoUrl);
/*
{ "openid":" OPENID",
" nickname": NICKNAME,
"sex":"1",
"province":"PROVINCE"
"city":"CITY",
"country":"COUNTRY",
"headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
"privilege":[ "PRIVILEGE1" "PRIVILEGE2" ],
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
*/
System.out.println("JSON-----"+userInfo.toString());
System.out.println("名字-----"+userInfo.getString("nickname"));
System.out.println("头像-----"+userInfo.getString("headimgurl"));
/*
* end 获取微信用户基本信息
*/
//获取到用户信息后就可以进行重定向,走自己的业务逻辑了。。。。。。
//接来的逻辑就是你系统逻辑了,请自由发挥
return "login";
}
}
相关文章推荐
- Django微信公众号开发(一)公众号内网页授权登录后微信获取用户信息
- 微信公众号开发《一》OAuth2.0网页授权认证获取用户的详细信息,实现自动登陆
- 微信公众号开发授权获取用户信息流程?
- 微信订阅号里实现oauth授权登录,并获取用户信息 (完整篇)
- (微信公众号开发《一》OAuth2.0网页授权认证获取用户的详细信息,实现自动登陆)http://blog.csdn.net/liaohaojian/article/details/70175835
- 微信公众号用java中控服务器-实现用户网页授权和获取使用JS-SDK权限
- 微信公众号开发《一》OAuth2.0网页授权认证获取用户的详细信息,实现自动登陆
- Java微信公众号开发:网页授权获取用户基本信息
- 微信公众号-获取用户信息(网页授权获取)实现步骤
- ASP .NET MVC 微信授权获取用户信息并实现登录
- Java 微信登录授权后获取微信用户信息昵称乱码问题解决
- java实现微信授权获取用户openid及授权用户相关信息
- 【Android应用开发详解】第01期:第三方授权认证(一)实现第三方授权登录、分享以及获取用户资料
- 微信开发笔记——微信网页登录授权,获取用户信息
- Android之QQ授权登录获取用户信息
- 微信公众号开发系列-网页授权获取用户基本信息
- C#微信公众号开发之网页授权oauth2.0获取用户基本信息(一)
- 微信开发笔记——微信网页登录授权,获取用户信息