1.这里需要两个jar包:connect-1.3.0.jar、json-lib-2.4-jdk15。你可以下载这两个jar包添加到你的项目依赖,如果你的项目是maven项目可以把下载后的connect-1.3.0.jar添加到你的本地仓库,然后在pom文件中添加这两个jar包的依赖,如下:
<dependency> <groupId>com.qq</groupId> <artifactId>connect</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency>
2.新建connect.properties文件放到src下面,把你申请的腾讯开放平台APP_ID、APP_KEY以及你的应用回调地址(可随意定制)编辑到这个文件中,如下:
#APP_ID APP_ID = 1xxx5xxx6 #APP_KEY APP_KEY = d707813xxxx2495b4e880bxxxx465 #OAUTH_CALLBACK OAUTH_CALLBACK =http\://www.xxxx.com/login/callback_qq.xhtml
3.接下来开发控制层代码,这里你可以用filter、拦截器或action的方式实现,本人采用action方式实现,代码如下:
private String oauth_token; private String openid; private String oauth_signature; private String oauth_vericode; private String timestamp; /** * 使用QQ快捷登录 * * @return */ public String login() { RequestToken requesttoken = new RequestToken(); String request_token = null; try { request_token = requesttoken.getRequestToken(); } catch (IOException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } HashMap<String, String> tokens = ParseString .parseTokenString(request_token); String oauth_token_secret = (String) tokens.get("oauth_token_secret"); session.setAttribute("oauth_token_secret", oauth_token_secret); RedirectToken redirecttoken = new RedirectToken(); try { WebUtils.sendHTML(response, redirecttoken.getRedirectURL(tokens)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } /** * QQ快捷登录回调函数 * * @return * @throws IOException * @throws SQLException */ public String callback() throws IOException, SQLException { try { if (StringUtils.isNotBlank(openid)) { session.setAttribute("openid", openid); } else { openid = ((String) session.getAttribute("openid")); } if (StringUtils.isNotBlank(timestamp)) { session.setAttribute("timestamp", timestamp); } else { timestamp = ((String) session.getAttribute("timestamp")); } if (StringUtils.isNotBlank(oauth_signature)) { session.setAttribute("oauth_signature", oauth_signature); } else { oauth_signature = ((String) session .getAttribute("oauth_signature")); } if (!Verify.verifyOpenID(openid, timestamp, oauth_signature)) { WebUtils.sendHTML(response, "openid verify false!"); } } catch (InvalidKeyException e1) { e1.printStackTrace(); } catch (NoSuchAlgorithmException e1) { e1.printStackTrace(); } AccessToken token = new AccessToken(); String oauth_token_secret = (String) session.getAttribute("oauth_token_secret"); String access_token = null; try { access_token = token.getAccessToken(oauth_token, oauth_token_secret, oauth_vericode); } catch (InvalidKeyException e1) { e1.printStackTrace(); } catch (NoSuchAlgorithmException e1) { e1.printStackTrace(); } HashMap<String, String> tokens = ParseString.parseTokenString(access_token); if (StringUtils.isNotBlank(tokens.get("error_code"))) { WebUtils.sendHTML(response,(String) tokens.get("error_code")); } try { if (!Verify.verifyOpenID((String) tokens.get("openid"), (String) tokens.get("timestamp"), (String) tokens.get("oauth_signature"))) WebUtils.sendHTML(response,"openid verify false2!"); } catch (InvalidKeyException e1) { e1.printStackTrace(); } catch (NoSuchAlgorithmException e1) { e1.printStackTrace(); } oauth_token = ((String) tokens.get("oauth_token")); oauth_token_secret = (String) tokens.get("oauth_token_secret"); openid = ((String) tokens.get("openid")); InfoToken infotoken = new InfoToken(); String info_json = null; try { info_json = infotoken.getInfo(oauth_token, oauth_token_secret,openid, "JSON"); } catch (IOException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } JSONObject json = JSONObject.fromObject(info_json); session.setAttribute("openid", openid); session.setAttribute("nickname",json.getString("nickname")); session.setAttribute("gender",json.getString("gender")); session.setAttribute("oauth_token", oauth_token); session.setAttribute("oauth_token_secret",oauth_token_secret); Integer count = userExtendService.getRowCountQQReg() + 1; session.setAttribute("account", "qq_" + json.getString("nickname") + "_" + count); userExtend = userExtendService.findByQQOpenID(openid); if (userExtend == null) { response.sendRedirect("/view/login/regbind.html"); return null; } else { //更新最后登录时间 userExtend.setLastLoginDate(new Date()); userExtendService.updateById(userExtend); user = userService.findById(userExtend.getUserId()); session.setAttribute(CommonConstant.SESSION_KEY_USER_INFO, user); LOGGER.info("用户【" + user.getUserAccount() + "】 通过QQ快捷登陆登录成功!"); response.sendRedirect("/view/login/close.html"); } return null; }
第一个action为点击快捷登录时访问的action,点击后弹出腾讯的快捷登录框,选择相应QQ帐号后点击登录,系统会自动返回你所配置的回调函数,也就是第二个action(即回调函数),你需要做的也就是在回调函数中对自己的业务进行处理,如果是第一次用快捷方式登录,你可以选择新注册用户,也可以绑定已有用户,再次登录的话即可直接登录。
没什么难度,就是刚开始的时候有点绕通过这个小例子相信在你的项目中添加快捷登录应该不是问题,如有疑问可以给我留言o(∩_∩)o ...