QQ快速登录
qq互聯的配置:
app_ID =?
app_KEY =?
redirect_URI =?
只需要改以上三個
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
server
從數據庫中查詢openid的用戶信息
如果數據庫中沒有openid代表沒有關聯qq
//查詢openid用戶信息
?? ?@Override
?? ?public ResponseBase findByopenidUser(@RequestParam("openid") String openid) {
?? ??? ?// 驗證參數
?? ??? ?if(StringUtils.isEmpty(openid))
?? ??? ?{
?? ??? ??? ?return ?setResultErro("系統錯誤");
?? ??? ??? ?
?? ??? ?}
?? ??? ?//使用openid查找數據庫對象的user表的信息
?? ??? ? UserEntity openid2 = userDao.openid(openid);
?? ??? ?if(openid2==null)
?? ??? ?{
?? ??? ?return?? ?setResultErro(Constants.HTTP_RES_CODE_201, "改openid沒有關聯");?? ?
?? ??? ?}
?? ??? ?//自動登錄
?? ??? ?return logintoke(openid2);
//授權QQ登錄
得到用戶的openid如果查找到opendd先進行賬號驗證,如果驗證正確開始自動登錄獲取token信息
從數據庫查找用戶userid修改數據庫對應的id,修改成功就代表關聯成功
?? ?@Override
?? ?public ResponseBase findBytokenUser(@RequestParam("token") String token) {
?? ??? ? //驗證參數
?? ??? ?if(StringUtils.isEmpty(token))
?? ??? ?{
?? ??? ??? ? return setResultErro("token不能為空");
?? ??? ??? ??? ??? ??
?? ??? ??? ?
?? ??? ?}
?? ? ?//從redis中 使用tocken 查找對應userid
?? ??? ?String stuserid = (String) baseRedissService.getString(token);
?? ??? ?if(StringUtils.isEmpty(stuserid))
?? ??? ?{
?? ??? ??? ? ?return setResultErro("tocken無效或者已過期");
?? ??? ??? ?
?? ??? ?}
?? ??? ?//使用userid 數據庫查詢用戶返回信息給客戶端
?? ??? ? ? Long parseLong = Long.parseLong(stuserid);?
?? ??? ? ? UserEntity findById = userDao.findById(parseLong);
?? ??? ? ? if(findById==null)
?? ??? ? ? {
?? ??? ??? ? ? return setResultErro("你查找的用戶不存在");
?? ??? ??? ? ??
?? ??? ? ? }
?? ??? ?return setResultSuccess(findById);
?? ?}
//授權QQ登錄
?? ?@Override
?? ?public ResponseBase qqlogin(@RequestBody UserEntity entity) {
?? ? ? ? ? //驗證參數
?? ??? ?String openid = entity.getOpenid();
?? ??? ?if(StringUtils.isEmpty(openid))
?? ??? ?{
?? ??? ??? ?return setResultErro("openid不能為空");
?? ??? ?}
?? ??? ?
?? ??? ?//先進行賬號登錄 ? 需要驗證
?? ??? ?ResponseBase Logintoke = login(entity);
?? ??? ?if(!Logintoke.getRtncode().equals(Constants.HTTP_RES_CODE_200))
?? ??? ?{
?? ??? ??? ?return Logintoke;
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?
?? ??? ?}
?? ??? ?// 自動登錄
?? ??? ?JSONObject data =(JSONObject) Logintoke.getData();
?? ??? ?//獲取token信息
?? ??? ?String string = data.getString("matoken");
?? ??? ?ResponseBase FindBytokenUser = findBytokenUser(string);
?? ??? ?if(!FindBytokenUser.getRtncode().equals(Constants.HTTP_RES_CODE_200))
?? ??? ?{
?? ??? ??? ?return FindBytokenUser;
?? ??? ??? ?
?? ??? ?}
?? ??? ?UserEntity data2 = (UserEntity) FindBytokenUser.getData();
?? ??? ?//修改對應的數據庫id
?? ??? ?Integer id = data2.getId();
?? ??? ? ?Integer Upadopeniduser = userDao.upadopeniduser(openid, id);
?? ??? ? ?if(Upadopeniduser<=0)
?? ??? ? ?{
?? ??? ??? ? ?return setResultErro("qq賬號管理失敗");
?? ??? ??? ? ?
?? ??? ? ?}
?? ??? ?return Logintoke;
?? ?}
contllen
?? ?//生成qq授權登錄連接
@RequestMapping("/LocaQQlogin")
?? ?public String locaQQlogin(HttpServletRequest request ) throws QQConnectException
?? ?{
?? ??? ? //生成授權連接
?? ??? ? String authorizeURL = new Oauth().getAuthorizeURL(request);
?? ??? ?return "redirect:"+authorizeURL;
?? ??? ?
?? ?}
獲取授權碼使用授權碼查詢accctoken 拿到acctoken 使用acctoken獲取openid
//調用會員服務接口 使用useropenid 查找是否已經關聯賬號
//如果已經綁定賬號了 自動登錄,將token信息存在cokke中
public String QQloginCall(HttpServletRequest request,HttpSession httpSession,HttpServletResponse respons) throws QQConnectException
?? ?{
?? ??? ?//獲取授權碼
?? ??? ?//使用授權碼或去acceetoken
?? ??? ?AccessToken accessTokenByRequest = new Oauth().getAccessTokenByRequest(request);
?? ??? ?if(accessTokenByRequest==null)
?? ??? ?{
?? ??? ??? ?request.setAttribute("error", "QQ授權失敗");
?? ??? ??? ?return "errro";
?? ??? ?}
?? ??? ?//拿到accken
?? ??? ?String accessToken = accessTokenByRequest.getAccessToken();
?? ??? ?if(accessToken==null)
?? ??? ?{
?? ??? ??? ?request.setAttribute("error", "ACCtokennull");
?? ??? ??? ?return "errro";
?? ??? ?}
?? ??? ?
?? ??? ?//使用acceetoken獲取openid
?? ??? ?OpenID openID = new OpenID(accessToken);
?? ??? ?String userOpenID = openID.getUserOpenID();
?? ??? ?if(userOpenID==null)
?? ??? ?{
?? ??? ??? ?
?? ??? ??? ?request.setAttribute("openID", "openID不能為空");
?? ??? ??? ?return "errro";
?? ??? ?}
//qq授權關聯頁面 沒有賬號
? //得到openid,調用登錄接口
? public String ?qqRelation(UserEntity entity,HttpServletResponse respons,HttpServletRequest reques,HttpSession httpSession)
??? ? ? {
? ? ? ? ? ? ? ? ?//得到openid
?? ??? ?String attribute = (String) httpSession.getAttribute("qqrelation");
?? ??? ?if(StringUtils.isEmpty(attribute))
?? ??? ?{
?? ??? ??? ?reques.setAttribute("error", "沒有獲取opid");
?? ??? ??? ?return ?"error";
?? ??? ??? ?
?? ??? ?}
?? ? ? ? ? ?//調用登錄接口,
?? ??? ?entity.setOpenid(attribute);
?? ??? ?ResponseBase login = memberServicE.qqlogin(entity);
?? ?
?? ??? ?if(!login.getRtncode().equals(Constants.HTTP_RES_CODE_200))
?? ??? ?{
?? ??? ??? ?
?? ??? ??? ?reques.setAttribute("error", "賬號或者密碼錯誤");
?? ??? ??? ?return Longin;
?? ??? ??? ?
?? ??? ?}
?? ?
?? ??? ?
?? ??? ? //將token信息存放在cookeie里面
?? ??? ?LinkedHashMap logindata = (LinkedHashMap) login.getData();
?? ??? ?String ?toke = (String) logindata.get("token");
? ? ? ? ? ?if(StringUtils.isEmpty(toke))
? ? ? ? ? ?{
? ? ? ? ?? ? ? reques.setAttribute("error", "會話失效!");
? ? ? ? ?? ? ? ?return Longin ;?? ? ??
? ? ? ? ? ?}
? ? ? ? setcookie(toke, respons);
?? ??? ? ? return CHAXUN;
?? ??? ? ??
?? ? ? }
總結
- 上一篇: 第十四课:树莓派网络视频监控(mjpg-
- 下一篇: FPGA Verilog 串口无限多字节