接下来就是微信的登录授权
微信的登录授权有两种方式。一种是微信开发者平台微信你登陆授权。一种是微信公众平台的微信登录授权。由于我用的是微信公众号的测试号进行登录授权。所以接下来我用的的就是用的自己的测试号资源进行授权。
首先前往微信的接口文档找到获取用户基本信息。
然后根据微信官方提供的接口,请求方式为get方式,微信会给你返回一个userInfo信息。这个时候就可以根据json格式的userInfo信息获取该用户的基本信息
大概的流程就是这样
最后附上代码。
1、首先配置自己的服务器,微信测试号所给的配置信息,发送的方式为get方式返回值为json格式的数据
public static final String APPID="";//填写自己的appid
public static final String APPSECRET="";//填写自己appsecret
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) {
String result = EntityUtils.toString(entity, "UTF-8");
jsonObject = JSONObject.fromObject(result);
}
httpGet.releaseConnection();//释放连接
return jsonObject;
}
2、其次自己编写一个servlet层,负责进入登录授权模式
@WebServlet("/wxLogin")
public class LoginContorller extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("this is wxLogin----------");
System.out.println("in /WxAuth/wxLogin");
//回调地址
//http://xxx.xx.xx.xx 是我服务器的地址 。当然有域名可以www.****.com
String backUrl = "http://www.****.com/WxAuth/callBack";
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
+ "appid=" + AuthUtil.APPID
+ "&redirect_uri=" + URLEncoder.encode(backUrl)
+ "&response_type=code"
+ "&scope=snsapi_userinfo"
+ "&state=STATE"
+ "#wechat_redirect";
// 重定向用户请求到微信授权URL
resp.sendRedirect(url);
}
}
3、进入对应的servlet后,向微信官网发出请求,与微信进行对接,然后微信会返回登录用户的个人信息,你用一个backurl地址进行回调处理。
@WebServlet("/callBack")
public class CallBackController extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("this is callBack=====");
//1. 获取微信回调请求中的code
System.out.println("in WxAuth/callBack");
String code = request.getParameter("code");
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
+ "appid=" + AuthUtil.APPID
+ "&secret=" + AuthUtil.APPSECRET
+ "&code=" + code
+ "&grant_type=authorization_code";
//2. 向微信发出请求,带上APPSCECRET和code,获取openid和access_toekn
JSONObject jsonObject = AuthUtil.doGetJson(url);
String openid = jsonObject.getString("openid");
String token = jsonObject.getString("access_token");
//4. 获取用户信息
String infoUrl = "https://api.weixin.qq.com/sns/userinfo?"
+ "access_token=" + token
+ "&openid=" + openid
+ "&lang=zh_CN";
JSONObject userInfo = AuthUtil.doGetJson(infoUrl);
System.out.println(userInfo);
request.setAttribute("userInfo", userInfo);
request.getRequestDispatcher("success.jsp").forward(request, response);
}
}
4、最后所获取的个人信息就在JSONObject userInfo = AuthUtil.doGetJson(infoUrl);中
你可以打印查看这些信息包括什么。 System.out.println(userInfo);
主要包括的就是这些字段
5、项目完工以后,就需要将项目打成war包放在公网服务器上,因为本地运行起来,微信是访问不到loacalhost的地址的。所以放在公网tomcat中,然后重启服务器,通过域名进行访问该项目。
浏览器访问
此时浏览器访问会提示你跳转到微信中访问。由于微信测试号的限制,你只能在关注了测试号公众号才能进行访问,获取点击登录人的个人信息。
至此为止,用户点击登录就可以获取该用户的基本信息了,你可以把用户登陆过的信息保存到自己的数据库之中,便可以及时查看那些用户的唯一中:openid。