springboot網站應用使用第三方qq登錄的實現過程
使用第三方qq登錄需要在QQ互聯官網https://connect.qq.com/中申請成功才可以使用。
1.登錄成功後進入個人設置中心設置個人信息
2.選擇公司或個人接入,這裡我選擇個人接入
3.註冊審核通過以後,點擊應用管理,右側會顯示審核已通過,然後就可以申請網站應用的第三方登錄瞭
4.填寫網站信息
5.然後就可以獲得APP ID和 APP Key,有瞭這兩個就可以使用qq登陸瞭
6.然後下邊是如何使用登錄接口進行授權登錄
這個java使用qq接口登錄的源碼鏈接:
鏈接: https://pan.baidu.com/s/1dqXPtPPYJdri-SE279OzQg
提取碼: shm9
6.1首先創建一個springboot應用learnstudy
目錄結構
6.2導入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> !--qq互聯--> <!-- https://mvnrepository.com/artifact/net.gplatform/Sdk4J --> <dependency> <groupId>net.gplatform</groupId> <artifactId>Sdk4J</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
6.3創建登錄控制類 LoginController
package com.study.learn.controller; import com.qq.connect.QQConnectException; import com.qq.connect.api.OpenID; import com.qq.connect.api.qzone.PageFans; import com.qq.connect.api.qzone.UserInfo; import com.qq.connect.javabeans.AccessToken; import com.qq.connect.javabeans.qzone.PageFansBean; import com.qq.connect.javabeans.qzone.UserInfoBean; import com.qq.connect.oauth.Oauth; import com.study.learn.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.Map; @Controller public class LoginController { @RequestMapping("/hello") @ResponseBody public String hello(){ return "hello"; } @RequestMapping("/") public String index(){ return "index"; } /*請求qq登錄*/ @RequestMapping("/LoginByQQ") public void LoginByQQ(HttpServletRequest request, HttpServletResponse response){ try { response.sendRedirect(new Oauth().getAuthorizeURL(request)); } catch (QQConnectException | IOException e) { e.printStackTrace(); } } /*回調地址*/ @RequestMapping("/login") public String AfterLoginRedirect(HttpServletRequest request, HttpServletResponse response, Model model){ 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 -------- start OpenID openIDObj = new OpenID(accessToken); openID = openIDObj.getUserOpenID(); // out.println("歡迎你,代號為 "%20+%20openID%20+%20" 的用戶!"); request.getSession().setAttribute("demo_openid", openID); // out.println("<a href="%20+%20"/shuoshuoDemo.html"%20+%20 " target=\"_blank\">去看看發表說說的demo吧</a>"); // 利用獲取到的accessToken 去獲取當前用戶的openid --------- end // out.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) { Map<String,Object> map= new HashMap<>(); String name=userInfoBean.getNickname(); String gender=userInfoBean.getGender(); String imgurl=userInfoBean.getAvatar().getAvatarURL30(); map.put("openId",openID); map.put("name",name); map.put("gender",gender); map.put("imgUrl",imgurl); User user=new User(openID,name,gender,imgurl); model.addAttribute("user",user); // out.println(userInfoBean.getNickname()%20+%20"<br/>"); // out.println(userInfoBean.getGender()%20+%20"<br/>"); // out.println("黃鉆等級: "%20+%20userInfoBean.getLevel()%20+%20"<br/>"); // out.println("會員 : "%20+%20userInfoBean.isVip()%20+%20"<br/>"); // out.println("黃鉆會員: "%20+%20userInfoBean.isYellowYearVip()%20+%20"<br/>"); // out.println("<image src="%20+%20userInfoBean.getAvatar().getAvatarURL30()%20+%20"/><br/>"); // out.println("<image src="%20+%20userInfoBean.getAvatar().getAvatarURL50()%20+%20"/><br/>"); // out.println("<image src="%20+%20userInfoBean.getAvatar().getAvatarURL100()%20+%20"/><br/>"); } else { // out.println("很抱歉,我們沒能正確獲取到您的信息,原因是: "%20+%20userInfoBean.getMsg()); System.out.println("很抱歉,我們沒能正確獲取到您的信息,原因是: "%20+%20userInfoBean.getMsg()); } } } catch (QQConnectException e) { } return "AfterLogin"; } }
6.4創建實體類 User
package com.study.learn.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { private String openId; private String name; private String gender; private String imgurl; }
6.5前端編寫
創建登錄主頁index.html
(圖片素材)
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>test</title> </head> <body> <a th:href="@{/LoginByQQ}"> <img th:src="@{/images/qq2.png}"></a> </body> </html>
效果
創建登錄點擊登錄授權成功的頁面AfterLogin.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div th:text="${user.openId}"></div> <div th:text="${user.gender}"></div> <div th:text="${user.imgurl}"></div> <div th:text="${user.name}"></div> </body> </html>
最後加入qqconnectconfig.properties
app_ID = 1012394 app_KEY = 0bf9ea3558af4d82056472476d9d redirect_URI = http://wanhhh.grouggp/studffy/login 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
最後就大功告成瞭!!!!!
要是網站應用還沒通過審核會顯示以下情況
通過審核後顯示如下
到此這篇關於springboot網站應用使用第三方qq登錄的文章就介紹到這瞭,更多相關springboot第三方qq登錄內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 手把手教你如何獲取微信用戶openid
- SpringCloud超詳細講解微服務網關Zuul
- 微信公眾號開發消息推送功能
- SpringSecurityOAuth2 如何自定義token信息
- SpringBoot整合SpringSession實現分佈式登錄詳情