现在的很多网站 为了让用户可以快速登录 使用第三方QQ、微信、微博之类的授权登录 QQ互联的官网地址
1:先去QQ互联申请成为开发者
当审核通过之后 就可以创建一个应用 然后开发实际的登录效果啦
2:直接上QQ授权登录的代码
package com.xinjue.web;import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.UUID;import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;import com.xinjue.common.Globals;
import com.xinjue.meta.Users;
import com.xinjue.service.UsersService;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;@Controller
public class QQloginController extends BaseController{@Resource UsersService usersService;@RequestMapping(value = "/qqlogin", method = RequestMethod.GET)public void qqlogin(ModelMap model, HttpServletRequest request,HttpServletResponse response) throws IOException {//Object demo_access_token = request.getSession().getAttribute("demo_access_token");//if(demo_access_token == null){try {response.sendRedirect(new Oauth().getAuthorizeURL(request));} catch (QQConnectException e) {e.printStackTrace();}//}else{//response.sendRedirect("test");//}//return "qqlogin";}/*** 请求跳转到QQ登录授权* @param model* @param request* @param response* @throws IOException*/@RequestMapping(value = "/get_qqlogin", method = RequestMethod.POST)public void get_qqlogin(ModelMap model, HttpServletRequest request,HttpServletResponse response) throws IOException {response.setContentType("text/html;charset=utf-8");String msg = "";try {//response.sendRedirect(new Oauth().getAuthorizeURL(request));msg = new Oauth().getAuthorizeURL(request);} catch (QQConnectException e) {e.printStackTrace();}response.getWriter().write(msg);}/*** QQ授权登录回调地址* @param model* @param request* @param response* @throws IOException* @throws InterruptedException */@RequestMapping(value = "/qqlogin_data", method = RequestMethod.GET)public void qqlogin_data(ModelMap model, HttpServletRequest request,HttpServletResponse response) throws IOException, InterruptedException {response.setContentType("text/html; charset=utf-8");System.out.println("code: "+request.getParameter("code"));System.out.println("state: "+request.getParameter("state"));//System.out.println("usercancel: "+request.getParameter("usercancel"));用户取消授权 这个值为非零PrintWriter out = response.getWriter();try {AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);String accessToken = null,openID = null;long tokenExpireIn = 0L;if (accessTokenObj.getAccessToken().equals("")) {
// 我们的网站被CSRF攻击了或者用户取消了授权
// 做一些数据统计工作System.out.print("没有获取到响应参数");} else {accessToken = accessTokenObj.getAccessToken();tokenExpireIn = accessTokenObj.getExpireIn();request.getSession().setAttribute("demo_access_token", accessToken);request.getSession().setAttribute("demo_token_expirein", String.valueOf(tokenExpireIn));// 利用获取到的accessToken 去获取当前用的openid -------- startOpenID openIDObj = new OpenID(accessToken);openID = openIDObj.getUserOpenID();out.println("欢迎你,代号为 " + openID + " 的用户!");request.getSession().setAttribute("demo_openid", openID);System.out.println("openid: "+request.getSession().getAttribute("demo_openid"));// 利用获取到的accessToken 去获取当前用户的openid --------- endout.println("<p> start -----------------------------------利用获取到的accessToken,openid 去获取用户在Qzone的昵称等信息 ---------------------------- start </p>");UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();out.println("<br/>");if (userInfoBean.getRet() == 0) {//"XJ_qq";//"XJ_wx";out.println("用户昵称:"+userInfoBean.getNickname() + "<br/>");out.println("性别:"+userInfoBean.getGender() + "<br/>");/* out.println("黄钻等级: " + userInfoBean.getLevel() + "<br/>");out.println("会员 : " + userInfoBean.isVip() + "<br/>");out.println("黄钻会员: " + userInfoBean.isYellowYearVip() + "<br/>");out.println("<image src=" + userInfoBean.getAvatar().getAvatarURL30() + "><br/>");out.println("<image src=" + userInfoBean.getAvatar().getAvatarURL50() + "><br/>");//没有替换之前是QQ空间的头像图片地址out.println("<image src=" + userInfoBean.getAvatar().getAvatarURL100().replace("qzapp.qlogo.cn", "thirdqq.qlogo.cn").replace("qzapp", "qqapp") + "><br/>");//"http://thirdqq.qlogo.cn/qqapp/101483567/8AD0B25EA6EFF86F0D77B707DBEB1961/100" QQ头像地址// http://qzapp.qlogo.cn/qzapp/101483567/8AD0B25EA6EFF86F0D77B707DBEB1961/30 QQ空间头像地址* */ String imageUrl = userInfoBean.getAvatar().getAvatarURL100().replace("qzapp.qlogo.cn", "thirdqq.qlogo.cn").replace("qzapp", "qqapp");String uuid = UUID.randomUUID().toString();String openid ="qq_"+openID;Users users = new Users();users = usersService.selectByQqOpenId(openid);if( users != null){ //已存在 if(users.getFrozenStatus() == 1){//跳转// out.println("您的账户已被冻结,无法继续操作,3秒之后自动跳转到网站首页");// Thread.sleep(3000);response.sendRedirect("qqlogin_n");return; }else{request.getSession().setAttribute(Globals.SSESION_USER, users);logingLog(request,users.getUserId()); }}else{users = new Users();users.setQqOpenid(openid);users.setNickName("XJ_qq"+uuid.split("-")[4]);users.setImageUrl(imageUrl);users.setSex(userInfoBean.getGender());users.setType(0);users.setLoginType(1);第三方登录微信2 QQ为1 h5为3users.setAddtime(new Date());users.setIp(getClientIpAddr(request));usersService.insertUser(users);logingLog(request,users.getUserId());request.getSession().setAttribute(Globals.SSESION_USER, users); }}else {response.getWriter().write(userInfoBean.getMsg());out.println("很抱歉,我们没能正确获取到您的信息,原因是: " + userInfoBean.getMsg());return;}response.sendRedirect("index");}} catch (QQConnectException e) {e.printStackTrace();}// return "index";}//判断openid是否存在。// 如果openid存在,则说明此用户之前登录过或者已与本地user表中的用户绑定。写入cookie,使用户为登录状态,到此结束。//如果用户openid不存在,则判断用户名是否存在。//如果用户名不存在,则直接生成新的本地用户,并绑定uid与openid。写入cookie,使用户为登录状态,到此结束。//如果用户名存在,提醒用户是否验证并与之绑定。如果用户选择验证,并验证通过,则与之绑定。写入cookie,使用户为登录状态,到此结束。//如果用户放弃验证,或者验证失败,则生成新的本地用户,并生成新的用户名,绑定uid与openid。写入cookie,使用户为登录状态,到此结束。/*** 通过前端JS保存QQ登录成功用户信息* @param model* @param request* @param response* @throws IOException*/@RequestMapping(value = "/save_qqlogindata", method = RequestMethod.POST)public void save_qqlogindata(ModelMap model, HttpServletRequest request,HttpServletResponse response) throws IOException {response.setContentType("text/html; charset=utf-8");String nickname = request.getParameter("nickname"); //用户名String openid = request.getParameter("openid"); // 用户身份的唯一标识。建议保存在本地,以便用户下次登录时可对应到其之前的身份信息,不需要重新授权。String figureurl_qq_1 = request.getParameter("figureurl_qq_1"); //用户的头像 这个地址是一定存在的String token = request.getParameter("token"); //表示当前用户在此网站/应用的登录状态与授权信息,建议保存在本地。response.getWriter().write(nickname+","+Globals.SUCCESS);}
}
3:还需要配置一个文件
app_ID = 你的APPID
app_KEY = 你的APPID秘钥
redirect_URI = http\://www.xinjue.com\:8080/qqlogin_data 这个地方是你的回调地址scope = get_user_info,add_topic,add_one_blog,add_album,upload_pic,list_album,add_share,check_page_fans,add_t,add_pic_t,del_t,get_repost_list,get_info,get_other_info,get_fanslist,get_idollist,add_idol,del_ido,get_tenpay_addr
baseURL = https://graph.qq.com/
getUserInfoURL = https://graph.qq.com/user/get_user_info
accessTokenURL = https://graph.qq.com/oauth2.0/token
authorizeURL = https://graph.qq.com/oauth2.0/authorize
getOpenIDURL = https://graph.qq.com/oauth2.0/me
addTopicURL = https://graph.qq.com/shuoshuo/add_topic
addBlogURL = https://graph.qq.com/blog/add_one_blog
addAlbumURL = https://graph.qq.com/photo/add_album
uploadPicURL = https://graph.qq.com/photo/upload_pic
listAlbumURL = https://graph.qq.com/photo/list_album
addShareURL = https://graph.qq.com/share/add_share
checkPageFansURL = https://graph.qq.com/user/check_page_fans
addTURL = https://graph.qq.com/t/add_t
addPicTURL = https://graph.qq.com/t/add_pic_t
delTURL = https://graph.qq.com/t/del_t
getWeiboUserInfoURL = https://graph.qq.com/user/get_info
getWeiboOtherUserInfoURL = https://graph.qq.com/user/get_other_info
getFansListURL = https://graph.qq.com/relation/get_fanslist
getIdolsListURL = https://graph.qq.com/relation/get_idollist
addIdolURL = https://graph.qq.com/relation/add_idol
delIdolURL = https://graph.qq.com/relation/del_idol
getTenpayAddrURL = https://graph.qq.com/cft_info/get_tenpay_addr
getRepostListURL = https://graph.qq.com/t/get_repost_list
version =2.0.0.0