/** 用户选择登陆渠道 */ @RequestMapping("/preLogin.do") public void preLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ApiException { String regMethod = request.getParameter("regMethod"); if ("weibo".equalsIgnoreCase(regMethod)) { // 新浪微博登陆 weibo4j.Oauth oauth = new weibo4j.Oauth(); try { String openUrl = oauth.authorize("code", ConstatVar.WEIBO_APPKEY, ConstatVar.WEIBO_SECRET); response.sendRedirect(openUrl); } catch (weibo4j.model.WeiboException e) { e.printStackTrace(); // TODO 跳转到错误页,提示用户重新选择注册渠道 } } else if ("qq".equalsIgnoreCase(regMethod)) { // QQ登陆 com.qq.connect.oauth.Oauth oauth = new com.qq.connect.oauth.Oauth(); try { String openUrl = oauth.getAuthorizeURL(request); response.sendRedirect(openUrl); } catch (com.qq.connect.QQConnectException e) { e.printStackTrace(); // TODO 跳转到错误页,提示用户重新选择注册渠道 } } else if ("taobao".equalsIgnoreCase(regMethod)) { // Taobao登陆 // TODO } else { response.sendRedirect("/loginPage.do"); } return; }
@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; }
@RequestMapping("/connector/qq_login") public String qqLogin(HttpServletRequest request) { String redirect = "redirect:/redirect"; try { String redirectURI = request.getScheme() + "://" + request.getServerName() + request.getContextPath() + "/connector/qq"; QQConnectConfig.updateProperties(QQConnectInitService.REDIRECT_URI, redirectURI); redirect = "redirect:" + OAUTH.getAuthorizeURL(request); } catch (QQConnectException e) { e.printStackTrace(); } return redirect; }