Java實現微信網頁受權

1、開發前的準備:

一、須要有一個公衆號,拿到AppID和AppSecret;html

二、進入公衆號開發者中心頁配置受權回調域名。具體位置:接口權限-網頁服務-網頁帳號-網頁受權獲取用戶基本信息-修改注意,這裏僅需填寫全域名(如www.qq.com、www.baidu.com),勿加 http:// 等協議頭及具體的地址字段; 這個域名須要是一個備案過的域名。git

2、受權步驟:

一、引導用戶進入受權頁面贊成受權,獲取code ;json

二、經過code換取網頁受權access_token(與基礎支持中的access_token不一樣) ;api

三、經過網頁受權access_token和openid獲取用戶基本信息。服務器

3、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=3app

2. 經過code換取網頁受權access_token,而後經過網頁受權access_token和openid獲取用戶基本信息(phoneLoginServlet的doPost方法);dom

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();
	}

獲取用戶信息返回樣例:ui

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

參考網址:url

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

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

相關文章
相關標籤/搜索