(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
(1)获取token信息成功,则取出access_token
(2)判断是否是第一次登陆
通过判断会员数据库是否存在uid(uid需要使用access_token查询第三方授权的用户信息)
如果uid已存在数据库中则表示不是第一次登陆,则只需更新access_token即可(token每登录一次会改变,并且过期时间为1天)
如果uid不存在,则表示第一次登陆,直接进行注册,注册信息从第三方授权用户信息中获取
/**
* 处理社交登陆
*/
@Slf4j
@Controller
public class OAuth2Controller {
@Autowired
MemberFeignService 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 headers,
//Map querys,
//Map bodys
post = 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
*/
@Override
public MemberEntity oauth2Login(SocialUser socialUser) throws Exception {
//获取gitee授权用户的资料 https://gitee.com/api/v5/user
//String host, String path, String method,
//Map headers,
//Map quer
MemberEntity 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);
//返回MemberEntity
return 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;
}
}