手机端网页-微信授权登录

来源:互联网 发布:网络高清录像机 编辑:程序博客网 时间:2024/04/27 21:23

近期有一个M站微信登录需求,了解了下微信网页授权登录功能:

参考文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

1、创建j2ee项目:weixinAuth

2、增加引入jar文件。












注意:jar包的版本不一样,可能会出现不同错误。commons-lang-2.6.jar、commons-collections-3.2.jar、httpcore-4.4.jar这三

个jar开始我使用的版本不一样,出现了各种异常。

3、环境配置:由于只有订阅号,采用“开发者工具-公众号平台测试账号”进行测试。

参考:https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

修改设置自己的域名。注意:这里不需要填写http://。格式:www.baidu.com

4、下面进行代码开发了

4.1新建工具类:AuthUtil.java

代码如下:

public class AuthUtil {
Log logger = LogFactory.getLog(AuthUtil.class);
public static String APPID = "";
public static String APPSECRET = "";
public static String SCOPE = "snsapi_userinfo";
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(null != entity){
String result = EntityUtils.toString(entity,"UTF-8");
jsonObject = JSONObject.fromObject(result);
}
httpGet.releaseConnection();
return jsonObject;
}
}

4.2 增加index.jsp入口页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="viewport" content="width=device-width,initial-scale=1.0">
<title>Insert title here</title>
</head>
<body style="font-size: 40px;text-align: center;">
<a href="/weixinAuth/wxLogin">微信公众号授权登录</a>
</body>
</html>

4.3 增加登录servlet。LoginServlet.java

@WebServlet("/wxLogin")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
String backUrl = "http://域名/weixinAuth/callback";
// 第一步:用户同意授权,获取code
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+AuthUtil.APPID+""
+ "&redirect_uri="+URLEncoder.encode(backUrl)
+ "&response_type=code"
+ "&scope="+AuthUtil.SCOPE
+ "&state=STATE#wechat_redirect";
resp.sendRedirect(url);
}
}


4.4 增加回调servlet。CallBackServlet.java

@WebServlet("/callback")
public class CallBackServlet extends HttpServlet {
Log logger = LogFactory.getLog(CallBackServlet.class);
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 第二步:通过code换取网页授权access_token
String code = req.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";
JSONObject jsonObject = AuthUtil.doGetJson(url);
String openid = jsonObject.getString("openid");
String token = jsonObject.getString("access_token");
// 第三步:刷新access_token(如果需要)
// TODO 
// 第四步:拉取用户信息(需scope为 snsapi_userinfo)
String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+token
+ "&openid="+openid
+ "&lang=zh_CN";
JSONObject userInfo = AuthUtil.doGetJson(infoUrl);
// unionid 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。
url = "http://域名/weixinAuth/success.jsp";
resp.sendRedirect(url);
}
}

到这里基本上就完成了微信授权网页登录了。至于自己的其他业务扩展,根据需要自己编写吧。


具体源代码参考:http://download.csdn.net/download/mingxiuping1988/10163460

原创粉丝点击