1. 点击跳转至第三方授权
2. 这是使用gitee作为第三方授权进行验证
3. 授权成功则跳转至 redirect_url
4. 社交登陆回调逻辑
一、根据第三方授权提供的方式获取token
(1)发送请求获取code码(每次发送请求,code码会改变)
https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code
(2)根据获取的code码拼装url,发送POST请求获取token信息
https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
(3)获取access_token
二、判断token信息是否获取成功,获取成功代码第三方授权登陆成功,可以直接跳转至主页,获取失败则刷新登陆页
(1)获取token信息成功,则取出access_token
(2)判断是否是第一次登陆
通过判断会员数据库是否存在uid(uid需要使用access_token查询第三方授权的用户信息)
如果uid已存在数据库中则表示不是第一次登陆,则只需更新access_token即可(token每登录一次会改变,并且过期时间为1天)
如果uid不存在,则表示第一次登陆,直接进行注册,注册信息从第三方授权用户信息中获取
/*** 处理社交登陆*/
@Slf4j
@Controller
public class OAuth2Controller {@AutowiredMemberFeignService memberFeignService;//http://auth.gulimall.com/oauth2.0/gitee/success?code=b43c71db8f207af30474f8331d76789c7cf705899ef7b276b7e60bb35c83cabd@GetMapping("/oauth2.0/gitee/success")public String oauth2(@RequestParam("code") String code) throws Exception {HttpResponse post = null;try {//https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}Map<String,String> map = new HashMap<>();map.put("grant_type",Oauth2Constant.OAUTH2_GRANT_TYPE);map.put("code",code);map.put("client_id", Oauth2Constant.OAUTH2_CLIENT_ID);map.put("redirect_uri",Oauth2Constant.OAUTH2_RIDIRECT_URI);map.put("client_secret",Oauth2Constant.OAUTH2_CLIENT_SECRET);//String host, String path, String method,//Map<String, String> headers,//Map<String, String> querys,//Map<String, String> bodyspost = HttpUtils.doPost("https://gitee.com", "/oauth/token", "POST", new HashMap<>(), map, new HashMap<>());} catch (Exception e) {e.printStackTrace();}//取出token//获取状态行的响应码,如果是200就代表响应成功if (post.getStatusLine().getStatusCode() == 200){HttpEntity entity = post.getEntity();//将HttpEntity转换成String类型String entityJsonString = EntityUtils.toString(entity);//Json字符串 -> java对象 参数必须是String类型Json串SocialUser socialUser = JSON.parseObject(entityJsonString,SocialUser.class);//判断账号是否是第一次登陆,如果是第一次登陆就直接注册到会员服务R<MemberOAuthVo> r = memberFeignService.oauth2Login(socialUser);if (r.getCode() == 0){MemberOAuthVo memberOAuthVo = r.getData(new TypeReference<MemberOAuthVo>() {});log.info("用户信息:{}",memberOAuthVo);return "redirect:http://gulimall.com";}}else {//获取失败 -> 重定向到登录页return "redirect:http://auth.gulimall.com/login.html";}//第三方授权成功 -> 跳转至登陆页return "redirect:http://gulimall.com";}}
会员注册
/*** 判断账号是否是第一次登陆*/@PostMapping("/oauth2Login")public R<MemberEntity> oauth2Login(@RequestBody SocialUser socialUser) throws Exception {MemberEntity member = memberService.oauth2Login(socialUser);if(member != null){return R.ok().setData(member);}else {return R.error(BizCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getCode(), BizCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getMsg());}}
/*** 判断账号是否是第一次登陆* 1.查询uid(created_at) 是否存在,uid存在则表示不是第一次登陆,只需要更新access_token即可* 2.如果uid不存在,则查询第三方授权的用户信息,将用户信息、uid、access_token存入member服务* 3.返回MemberEntity*/@Overridepublic MemberEntity oauth2Login(SocialUser socialUser) throws Exception {//获取gitee授权用户的资料 https://gitee.com/api/v5/user//String host, String path, String method,//Map<String, String> headers,//Map<String, String> querMemberEntity oatuthMemberEntity = null;String giteeId = null;String giteeName = null;try {Map<String,String> map = new HashMap<>();map.put("access_token",socialUser.getAccess_token());HttpResponse response = HttpUtils.doGet("https://gitee.com", "/api/v5/user", "GET", new HashMap<>(), map);oatuthMemberEntity = new MemberEntity();giteeId = "";giteeName = "";if (response.getStatusLine().getStatusCode() == 200){HttpEntity entity = response.getEntity();String s = EntityUtils.toString(entity);JSONObject jsonObject = JSON.parseObject(s);giteeId = jsonObject.getString("id");giteeName = jsonObject.getString("name");}} catch (Exception e) {e.printStackTrace();}//判断账号是否是第一次登陆MemberEntity memberEntity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("social_uid", giteeId));if (memberEntity != null){//uid存在,表示不是第一次登陆,则更新token即可MemberEntity member = new MemberEntity();member.setId(memberEntity.getId());member.setAccessToken(socialUser.getAccess_token());member.setExpiresIn(Long.toString(socialUser.getExpires_in()));this.baseMapper.updateById(member);//返回MemberEntityreturn memberEntity;}else {//查询为空表示是第一次登陆,根据第三方提供的api查询用户信息,然后注册oatuthMemberEntity.setSocialUid(giteeId);oatuthMemberEntity.setUsername(giteeName);oatuthMemberEntity.setCreateTime(new Date());oatuthMemberEntity.setAccessToken(socialUser.getAccess_token());oatuthMemberEntity.setExpiresIn(Long.toString(socialUser.getExpires_in()));this.baseMapper.insert(oatuthMemberEntity);return oatuthMemberEntity;}}