1.什么是第三方登录
简单说,就是一种授权机制。数据的所有者告诉系统,同意授权第三方应用进入系统,获取这些数据。系统从而产生一个短期的进入令牌(token),用来代替密码,供第三方应用使用。
本次案例说的git授权登录
2.git具体操作
2.1登录git
www.gitee.com
2.2 绑定手机
2.3 设置地方应用
第一步:
第二步:
第三步:
第四步:
3 项目中开发
3.1 页面
页面代码增加一句话:
//通过浏览器将用户引导到码云三方认证页面上( GET请求 )
<a href=" https://gitee.com/oauth/authorize?client_id=d49bc833f5f8123de173f00b787abbd1cf25d32006dd1306581e0a1a7d972cba&redirect_uri=http://localhost:8080/auth&response_type=code">git登录</a><br>
3.2 controller
3.2.1 码云认证服务器地址码云认证服务器返回 access_token
//码云认证服务器通过回调地址{redirect_uri}将 用户授权码 传递给 应用服务器 或者直接在 Webview 中跳转到携带 用户授权码的回调地址上,
@RequestMapping("auth")
public String auth(String code, HttpResponse response){
//封装参数请求参数
Map<String,Object> map = new HashMap<String,Object>();
map.put("code",code);//返货哦的
map.put("client_id",client_id);//用户id
map.put("redirect_uri",redirect_uri);//回调地址
map.put("client_secret",client_secret);//秘钥
map.put("grant_type","authorization_code");
String url="https://gitee.com/oauth/token";
//码云认证服务器返回 access_token
String token = HttpUtil.post(url, map);
//应用通过 access_token 访问 Open API 使用用户数据。
return "redirect:search";
}
4 效果演示
4.1 点击登录
4.2 授权
4.3 点同意 进入 search页面
4.3 点拒绝进入login