1.社交登录介绍
1.用户在passport认证中心准备登录时可以点击第三方登录按钮跳转到
https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI
引导用户进入用户授权页面
2.用户授权完成后
http://授权完成后/success?code=fef987b3f9ad1169955840b467bfc661
第三方网站将调用我们在第三方的回调地址,再将授权码写入到数据库中
3.通过授权码code发送post请求到第三方网站,换取access_token
授权码请求公式
https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
将换取来的token写入到我们用户的数据库
4.在用户使用过程中通过access_token获取用户(第三方网站用户数据)
14.整合社交登录
1.地址一:
引导用户与第三方平台交互地址(第三方平台地址,页面上有我们的授权信息)
https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI
2. 地址二:
授权完成后返回授权码
http://授权完成后/success?code=fef987b3f9ad1169955840b467bfc661
3.地址三:
https://api.weibo.com/oauth2/access_token
使用获得的授权码code交换获得access_token的地址,需要加入client_secret应用密匙,并且使用post请求进行交换,access_token
4.地址四:
https://api.weibo.com/2/users/show.json
使用access_token换取用户信息地址
@RequestMapping("vlogin")
public String vlogin(String code,HttpServletRequest request){
//授权码换取access_token
String s3="https://api.weibo.com/oauth2/access_token?";
//?client_id=3193708961&client_secret=df2b2370fbd3483cdd8a9eb422ca325&grant_type=authorization_code&redirect_uri=http://passport.gmall.com:8085/vlogin&code=CODE";
HashMap<String, String> paramMap = new HashMap<>();
paramMap.put("client_id","3193708961");
paramMap.put("client_secret","df2b2370fbd3483cdd8a9eb422ca3253");
paramMap.put("grant_type","authorization_code");
paramMap.put("redirect_uri","http://passport.gmall.com:8085/vlogin");
paramMap.put("code",code);
String access_token_json = HttpclientUtil.doPost(s3, paramMap);
Map<String,String> access_map = JSON.parseObject(access_token_json, Map.class);
//access_token获取用户信息
String access_token = access_map.get("access_token");
String uid = access_map.get("uid");
String s4="https://api.weibo.com/2/users/show.json?access_token="+access_token+"&uid="+uid;
String s = HttpclientUtil.doGet(s4);
Map<String,Object> map = JSON.parseObject(s, Map.class);
//将用户信息保存到数据库,将用户类型设置为微博用户
UmsMember umsMember = new UmsMember();
umsMember.setSourceType("2");
umsMember.setAccessCode(code);
umsMember.setAccessToken(access_token);
umsMember.setNickname((String)map.get("screen_name"));
umsMember.setSourceUid(((Long)map.get("id")));
umsMember.setCity((String) map.get("location"));
umsMember.setGender((String) (map.get("gender")));
UmsMember umsCheck = new UmsMember();
umsCheck.setSourceUid(umsMember.getSourceUid());
UmsMember checkUmsMember = userService.checkOauthUser(umsCheck);
if(checkUmsMember==null){
userService.addOldOauthUser(umsMember);
}else{
umsMember=umsCheck;
}
//生成JWT的token,并且重定向到首页,携带token
String token="";
String memberId = umsMember.getId();
String nickname = umsMember.getNickname();
Map<String,Object> userMap=new HashMap<>();
userMap.put("memberId",memberId);
userMap.put("nickname",nickname);
String ip = request.getHeader("x-forwarded-for");
//从request中获取IP
if(StringUtils.isBlank(ip)) {
ip=request.getRemoteAddr();
if(StringUtils.isBlank(ip)) {
ip="127.0.0.1";
}
}
//按照设计的算法对参数进行加密后,生成token
token = JwtUtil.encode("2019gmall0105", userMap, ip);
return "redirect:http://search.gmall.com:8083/index?token="+token;
}