Java实现微信网页授权

一、开发前的准备:

1、需要有一个公众号,拿到AppID和AppSecret;

2、进入公众号开发者中心页配置授权回调域名。具体位置:接口权限-网页服务-网页账号-网页授权获取用户基本信息-修改注意,这里仅需填写全域名(如www.qq.com、www.baidu.com),勿加 http:// 等协议头及具体的地址字段; 这个域名需要是一个备案过的域名。

二、授权步骤:

1、引导用户进入授权页面同意授权,获取code ;

2、通过code换取网页授权access_token(与基础支持中的access_token不同) ;

3、通过网页授权access_token和openid获取用户基本信息。

三、Java实现

1.生成二维码的授权二维码(getQrCodeServlet的doPost方法),引导用户进入授权页面同意授权,获取code;

protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		//生成唯一ID
		int uuid = (int) (Math.random() * 100000);
		//生成二维码
		String imgName = uuid + "_" + (int) (new Date().getTime() / 1000) + ".png";
		String path = getServletContext().getRealPath("/")+"images/";
		File file = new File(path+imgName);
		String callbackurl = URLEncoder.encode(ResourceUtil.getConfigByName("callbackurl") +"&uuid="+uuid);
		QRCode.encode("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx812eea92b8bd7255&redirect_uri="+callbackurl+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect ", file,"png", BarcodeFormat.QR_CODE, 500, 500, null);
		
		//生成的图片访问地址
		String qrCodeImg = "http://你的服务器地址/images/" + imgName;
		String jsonStr = "{\"uuid\":" + uuid + ",\"qrCodeImg\":\"" + qrCodeImg + "\"}";
		out.print(jsonStr);
		out.flush();
		out.close();
	}

其中callbackurl是微信扫码后跳转跳转的URL,如callbackurl=你的域名/phoneLoginServlet?version=3

2. 通过code换取网页授权access_token,然后通过网页授权access_token和openid获取用户基本信息(phoneLoginServlet的doPost方法);

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uuid = request.getParameter("uuid");
		String code = request.getParameter("code");
		boolean bool = true;
		new WeixinUserServiceImpl().saveWeixinLogin(null);
		//根据回调url返回的code换取网页授权access_token
		if(null != code && code != ""){
			String appid = ResourceUtil.getConfigByName("appid");
			String appsecret = ResourceUtil.getConfigByName("appsecret");
            JSONObject json_accessToken = HttpUtil.httpGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+appsecret+"&code="+code+"&grant_type=authorization_code");
            String access_token = json_accessToken.getString("access_token");
            String openid = json_accessToken.getString("openid");  
            //拉取用户信息
            JSONObject json_userinfo = HttpUtil.httpGet("https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid);
            //TODO 验证登录
            if(bool){
            	//将登陆信息存入map
            	WeixinUser userVo = (WeixinUser) LoginUserVo.getLoginUserMap().get(uuid);
            	if(userVo == null){
            		userVo = new WeixinUser();
            		userVo.setOpenid(json_userinfo.getString("openid"));
            		userVo.setNickname(json_userinfo.getString("nickname"));
            		userVo.setSex(json_userinfo.getString("sex"));
            		userVo.setProvince(json_userinfo.getString("province"));
            		userVo.setCity(json_userinfo.getString("city"));
            		//WeixinUserService weixinUserService = new WeixinUserService();
            		weixinUserService.saveWeixinLogin(userVo);
            		LoginUserVo.getLoginUserMap().put(uuid, userVo);
            	}
            }
		}
		PrintWriter out = response.getWriter();
		out.print(bool);
		out.flush();
		out.close();
	}

获取用户信息返回样例:

[result={
"openid":"oN9UryuC0Y01aQt0jKxZXbfe658w",
"nickname":"lovebread",
"sex":1,
"language":"zh_CN",
"city":"",
"province":"",
"country":"中国",
"headimgurl":"http://wx.qlogo.cn/mmopen/bRLXzTf2f6HNfBTd72heAA7vNKsGKvK3dfreewrewsPff9OaMWib0GibbA8daQmNQvQhagtiaicf4vNC5nYU3ia821QQ/0",
"privilege":[]}]

参考网址:

1.http://www.cnblogs.com/lovebread/p/5513241.html

2.https://git.oschina.net/langqiao123/lrswx1

转载于:https://my.oschina.net/u/940590/blog/760401

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值