什么是OAuth2.0
– OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的所有内容。
– OAuth2.0:对于用户相关的OpenAPI(例如获取用户信息,动态同步,照片,日志,分享等),为了保护用户数据的安全和隐私,第三方网站访问用户数据前都需要显式的向用户征求授权。
创建gitee第三方应用
文档地址:https://gitee.com/api/v5/oauth_doc#/
登录gitee–设置–第三方应用–创建新应用
代码编写
前端
应用通过 浏览器 或 Webview 将用户引导到码云三方认证页面上( GET请求 )
https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code
<a href="https://gitee.com/oauth/authorize?client_id=9ba91021257126a00aadd1e0c07c14a7d52b0cc9172452b7212dcd67e5f742e3&redirect_uri=http://auth.gulimail.com/oauth2.0/gitee/success&response_type=code&scope=user_info%20emails">
<img src="GIT_img/gitee.png" style="width: 50px;height: 18px" />
<span></span>
</a>
后端
@GetMapping("/oauth2.0/gitee/success")
public String gitee(@RequestParam("code") String code, HttpSession session, HttpServletResponse servletResponse, HttpServletRequest request) throws Exception {
Map<String, String> header = new HashMap<>();
Map<String, String> query = new HashMap<>();
Map<String, String> map = new HashMap<>();
map.put("client_id", "9ba91021257126a00aadd1e0c07c14a7d52b0cc9172452b7212dcd67e5f742e3");
map.put("client_secret", "???????????????????????????");
map.put("grant_type", "authorization_code");
map.put("redirect_uri", "http://auth.gulimail.com/oauth2.0/gitee/success");
map.put("code", code);
//1、根据code换取accessToken;
//应用服务器 或 Webview 使用 access_token API 向 码云认证服务器发送post请求传入 用户授权码 以及 回调地址( POST请求 ),自己寻找HttpUtils工具发送请求
//注:请求过程建议将 client_secret 放在 Body 中传值,以保证数据安全。
//https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", header, query, map);
//2、处理
if (response.getStatusLine().getStatusCode() == 200) {
//获取到了 accessToken
String json = EntityUtils.toString(response.getEntity());
SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
Map<String, String> map1 = new HashMap<>();
map1.put("access_token", socialUser.getAccess_token());
HttpResponse response1 = HttpUtils.doGet("https://gitee.com", "/api/v5/user", "get", header, map1);
String json1 = EntityUtils.toString(response1.getEntity());
JSONObject jo = JSON.parseObject(json1);
socialUser.setUid(jo.getString("id"));
//知道当前是哪个社交用户
//1)、当前用户如果是第一次进网站,自动注册进来(为当前社交用户生成一个会员信息账号,以后这个社交账号就对应指定的会员)
//登录或者注册这个社交用户
R oauthlogin = memberFeignService.oauthlogin(socialUser);
if (oauthlogin.getCode() == 0) {
MemberRespVo data = oauthlogin.getData("data", new TypeReference<MemberRespVo>() {
});
log.info("登录成功:用户:{}", data.toString());
//1、第一次使用session;命令浏览器保存卡号。JSESSIONID这个cookie;
//以后浏览器访问哪个网站就会带上这个网站的cookie;
//子域之间; gulimall.com auth.gulimall.com order.gulimall.com
//发卡的时候(指定域名为父域名),即使是子域系统发的卡,也能让父域直接使用。
//TODO 1、默认发的令牌。session=dsajkdjl。作用域:当前域;(解决子域session共享问题)
//TODO 2、使用JSON的序列化方式来序列化对象数据到redis中
session.setAttribute(AuthServerConstant.LOGIN_USER, data);
// new Cookie("JSESSIONID","dadaa").setDomain("");
// servletResponse.addCookie();
//2、登录成功就跳回首页
return "redirect:http://gulimail.com";
} else {
return "redirect:http://auth.gulimail.com/login.html";
}
} else {
return "redirect:http://auth.gulimail.com/login.html";
}
}