1.1 第三方登录
所谓的第三方登录,就是APP识别到用户将第三方的账号绑定到自己平台的 ID上直接完成登录的过程,简单来说,是指基于用户在第三方平台上已有的账号和密码来快速完成己方应用的登录或者注册的功能。常见的第三方登录平台,一般是已经拥有大量用户的平台,国内的就是各大厂:微信、微博、QQ 等,国外有 Facebook、Twitter。
1.2 扫码登录
现在的第三方平台基本都支持扫码登录,因为扫码登录更加安全,
而且不用记账号和密码
如:微信、qq、支付宝
- QQ第三方登录
2.1 qq互联
2.2 注册开发者
2.3 文档
- OAuth2.0 介绍
3.1. QQ登录OAuth2.0总体处理流程
QQ登录OAuth2.0总体处理流程如下:
Step1:申请接入,获取appid和apikey;
Step2:开发应用,并设置协作者帐号进行测试联调;
Step3:放置QQ登录按钮;
Step4:通过用户登录验证和授权,获取Access Token;
Step5:通过Access Token获取用户的OpenID;
Step6:调用OpenAPI,来请求访问或修改用户授权的资源。
- 项目集成qq登录
- maven导入包
<!-- QQ第三方登录-->
<dependency>
<groupId>net.gplatform</groupId>
<artifactId>Sdk4J</artifactId>
<version>2.0</version>
</dependency>
4.2. 配置
最好查看qq给出的demo例子
创建 qqconnectconfig.properties放在resources根目录下
app_ID = 101543517
app_KEY = 3d43255254a6f50876661fe2bec86684
redirect_URI = http://javatv.cn/loginAfter
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
4.3. 建包oauth2并扫描
<context:component-scan base-package="cn.itsource.oauth2" />
4.4. 编写qqcontroller
@Controller
public class QQController {
@Autowired
RestTemplate restTemplate;
@Autowired
UserService userService;
// 将页面重定向到qq第三方的登录页面
@RequestMapping(value = "/qqLogin")
public void qqLogin(HttpServletRequest request, HttpServletResponse response) {
try {
response.setContentType("text/html;charset=utf-8");
response.sendRedirect(new Oauth().getAuthorizeURL(request));//将页面重定向到qq第三方的登录页面
} catch (Exception e) {
e.printStackTrace();
}
}
// 登录之后回调地址
@RequestMapping(value = "/loginAfter")
public String qqLoginAfter(HttpServletRequest request, HttpServletResponse response) {
try {
System.err.println("============登录之后回调地址==========");
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();
// 利用获取到的accessToken 去获取当前用的openid -------- start
OpenID openIDObj = new OpenID(accessToken);
openID = openIDObj.getUserOpenID();
System.err.println("===============" + openID);
// 获取qq信息
String url = "https://graph.qq.com/user/get_user_info?access_token="
+ accessToken + "&oauth_consumer_key=101543517" + "&openid=" + openID + "&format=json ";
String json = restTemplate.getForObject(url, String.class);
ObjectMapper objectMapper = new ObjectMapper();
HashMap map = objectMapper.readValue(json, HashMap.class);
System.err.println(map);
System.err.println(map.get("figureurl_qq_1"));
// 如果用户表没有当前openid,那就添加用户,如果就有就直接跳转主界面main.jsp\
User user = new User();
user.setQqOpenId(openID);
User u = userService.queryByParam(user);
if (u != null) {
System.out.println(u);
return "main";
} else {
// 添加用户
user.setHeadImg((String) map.get("figureurl_qq_1"));
user.setLoginName(UUID.randomUUID().toString());
user.setLoginPwd("123456");
userService.insert(user);
return "main";
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
4.5 登录页面加入QQ登录
页面加入
绑定事件
4.6. 修改 hosts
C:\Windows\System32\drivers\etc
修改hosts文件,在末尾加入
127.0.0.1 javatv.cn