@RequestMapping("/connector/qq") public String qq(HttpServletRequest request) { String redirect = "redirect:/redirect"; try { AccessToken accessToken = OAUTH.getAccessTokenByRequest(request); String id = new OpenID(accessToken.getAccessToken()).getUserOpenID(); QQUser qqUser = new QQUser(); qqUser.setId(id); QQAccessToken token = new QQAccessToken(); // token.setAppId(appId); token.setToken(accessToken.getAccessToken()); token.setExpireIn(accessToken.getExpireIn()); Date current = new Date(); token.setCreatedTime(current); token.setLastUpdated(current); qqUser.getTokens().add(token); User user = userService.register( qqUser, new UserInfo(accessToken.getAccessToken(), id).getUserInfo()); SecurityContextHolder.getContext().setAuthentication(new QQAuthToken(user)); } catch (QQConnectException e) { // TODO Auto-generated catch block e.printStackTrace(); } return redirect; }
public void doLoginRedirect( @Param("code") String code, @Param("state") String state, Navigator nav, TurbineRunData rundata, Context context) { try { AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request); String accessToken = accessTokenObj.getAccessToken(); long tokenExpireIn = accessTokenObj.getExpireIn(); OpenID openIDObj = new OpenID(accessToken); String openID = openIDObj.getUserOpenID(); UserInfo qzoneUserInfo = new UserInfo(accessToken, openID); UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo(); UserDO userDO = new UserDO(); userDO.setNickName(userInfoBean.getNickname()); userDO.setFaceImageUrl(userInfoBean.getAvatar().getAvatarURL100()); userDO.setSourceType(SnsSourceType.TENXUN_QQ.getType()); userDO.setAccessToken(accessToken); userDO.setSourceId(openID); userDO.setStatus(MainStatus.NORMAL.getType()); userDO.setPermissions(PermissionsType.NORMAL.getType()); userAO.save(userDO); setCurrentLoginUser(userDO); } catch (QQConnectException e) { e.printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } }
/** * qq登录回调方法 * * @throws QQConnectException */ public void qqlogincallback() throws QQConnectException, IOException { HttpServletRequest request = getRequest(); AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request); String accessToken = null, openID = null; if (accessTokenObj.getAccessToken().equals("")) { renderText("用户取消了授权或没有获取到响应参数"); } else { accessToken = accessTokenObj.getAccessToken(); // 利用获取到的accessToken 去获取当前用的openid -------- start OpenID openIDObj = new OpenID(accessToken); openID = openIDObj.getUserOpenID(); UserInfo qzoneUserInfo = new UserInfo(accessToken, openID); UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo(); String avatar = userInfoBean.getAvatar().getAvatarURL50(); String nickname = userInfoBean.getNickname(); User user = (User) getSession().getAttribute(Constants.USER_SESSION); if (user == null) { user = User.me.findByOpenID(openID, "qq"); String id = StrUtil.getUUID(); if (user == null) { user = new User(); user.set("id", id) .set("qq_nickname", nickname) .set("qq_avatar", avatar) .set("qq_open_id", openID); } else { user.set("qq_nickname", nickname).set("qq_avatar", avatar); } setSessionAttr("open_id", openID); setSessionAttr("thirdlogin_type", "qq"); setSessionAttr("unsave_user", user); } else { User user1 = User.me.findByOpenID(openID, "qq"); if (user1 != null && !user1.get("id").equals(user.get("id"))) { getResponse().setCharacterEncoding("utf-8"); getResponse() .getWriter() .write( "<script>alert('此QQ号已经绑定其他账户,请更换绑定');location.href=\'/user/setting\'</script>"); return; } else { user.set("qq_nickname", nickname) .set("qq_open_id", openID) .set("qq_avatar", avatar) .update(); } } if (StrUtil.isBlank(user.getStr("email"))) { redirect("/reg.html?third=qq"); } else { setSessionAttr(Constants.USER_SESSION, user); setCookie( Constants.USER_COOKIE, StrUtil.getEncryptionToken(user.getStr("token")), 30 * 24 * 60 * 60); String source = (String) getSession().getAttribute("source"); if (!StrUtil.isBlank(source)) { if (source.equalsIgnoreCase("usersetting")) { getSession().removeAttribute("source"); redirect("/user/setting"); } } else { redirect("/"); } } } }
@RequestMapping("/bindQQ") public void bindQQ(HttpServletRequest request, HttpServletResponse response) { try { ThirdPartyAccess qq = thirdPartyAccessService.findByType(ThirdPartyAccess.TYPE_QQ); GlobalSetting globalSetting = (GlobalSetting) request.getSession().getAttribute("setting"); AccessToken accessTokenObj = (new Oauth()) .getAccessTokenByRequest( request, qq.getAccessKey(), qq.getAccessSecret(), "http://" + globalSetting.getAppUrl() + "/op/login/QQLogin"); String accessToken = null, openID = null; long tokenExpireIn = 0L; if ("".equals(accessTokenObj.getAccessToken())) { // 我们的网站被CSRF攻击了或者用户取消了授权 // 做一些数据统计工作 LOG.info("没有获取到响应参数"); } else { accessToken = accessTokenObj.getAccessToken(); tokenExpireIn = accessTokenObj.getExpireIn(); request.getSession().setAttribute("token_expirein", String.valueOf(tokenExpireIn)); // 利用获取到的accessToken 去获取当前用的openid -------- start OpenID openIDObj = new OpenID(accessToken); openID = openIDObj.getUserOpenID(); request.getSession().setAttribute("openId", openID); request.getSession().setAttribute("loginType", UserSession.TYPE_QQ); // 利用获取到的accessToken 去获取当前用户的openid --------- end // 为空代表首次登录,此处获取的信息尚未完全 ThirdPartyAccount tpa = tpaService.findByOpenId(openID); if (tpa == null) { // 获取用户QQ空间的信息 UserInfo qzoneUserInfo = new UserInfo(accessToken, openID); UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo(qq.getAccessKey()); if (userInfoBean != null) { tpa = new ThirdPartyAccount(); tpa.setOpenId(openID); tpa.setAccountType(UserSession.TYPE_QQ); tpa.setAccessToken(accessToken); tpa.setHeadIconBig(userInfoBean.getAvatar().getAvatarURL100()); tpa.setHeadIconMid(userInfoBean.getAvatar().getAvatarURL50()); tpa.setHeadIconSmall(userInfoBean.getAvatar().getAvatarURL30()); tpa = tpaService.update(tpa); } } UserSession userSession = (UserSession) request.getSession(false).getAttribute("userSession"); tpa.setUser(userSession.getUser()); tpaService.update(tpa); // fate.qq4j.weibo.UserInfo weiboUserInfo = new fate.qq4j.weibo.UserInfo( // accessToken, openID); // com.qq.connect.javabeans.weibo.UserInfoBean weiboUserInfoBean = weiboUserInfo // .getUserInfo(qq.getAccessKey()); response.sendRedirect("thirdParty"); } } catch (QQConnectException e) { LOG.error("连接到QQ失败", e); } catch (IOException e) { LOG.error("重定向会本站失败", e); } }