github的oauth登陆的基本流程,oauth2.0原理解析

1、在github上申请OAuth App     https://github.com/settings/developers

各输入字段的解释:
Application name               应用名
Homepage URL                   主页URL,这里我填写的是本地测试的URL地址
Application description        应用描述
Authorization callback URL    后端回调URL

2、注册Oauth application后,会得到Client ID和Client Secret,如下图,注意不要随便公布自己的这两个信息 

 3、编写后台代码,并发布到 http://106.12.95.***

@SpringBootApplication
@Controller
public class ConfigServer5010 {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServer5010.class, args);
    }

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @Autowired
    RestTemplate restTemplate;


    @RequestMapping("/")
    public String step1() {
        String s = "redirect:https://github.com/login/oauth/authorize?client_id=c624d2db46996072baa3&scope=user";
        return s;
    }

    @RequestMapping("/callback")
    @ResponseBody
    public Object step2(String code) {
        System.out.println("code=" + code);
        JSONObject jsonObject = restTemplate.getForObject("https://github.com/login/oauth/access_token?client_id=c624d2db46996072baa3&client_secret=b4ef1e52bb143e30e149bb426916f911111111111&code=" + code, JSONObject.class);
        System.out.println(jsonObject.toJSONString());
        JSONObject user = restTemplate.getForObject("https://api.github.com/user?access_token=" + jsonObject.get("access_token"), JSONObject.class);
        System.out.println(user.toJSONString());
        return user;
    }

}

 

测试:

在浏览器输入:http://106.12.95.***:80/ 会重向到该页面。

登录后出现授权页面: 

 授权后会调用callback接口

用户的信息

{
  "gists_url": "https://api.github.com/users/rewtreytrytu/gists{/gist_id}",
  "repos_url": "https://api.github.com/users/rewtreytrytu/repos",
  "two_factor_authentication": false,
  "following_url": "https://api.github.com/users/rewtreytrytu/following{/other_user}",
  "bio": null,
  "created_at": "2016-09-22T06:39:44Z",
  "login": "rewtreytrytu",
  "type": "User",
  "blog": "",
  "private_gists": 0,
  "total_private_repos": 0,
  "subscriptions_url": "https://api.github.com/users/rewtreytrytu/subscriptions",
  "updated_at": "2019-03-13T05:42:40Z",
  "site_admin": false,
  "disk_usage": 0,
  "collaborators": 0,
  "company": null,
  "owned_private_repos": 0,
  "id": 223632323868,
  "public_repos": 1,
  "gravatar_id": "",
  "plan": {
    "name": "free",
    "space": 97656233499,
    "collaborators": 0,
    "private_repos": 10000
  },
  "email": null,
  "organizations_url": "https://api.github.com/users/rewtreytrytu/orgs",
  "hireable": null,
  "starred_url": "https://api.github.com/users/rewtreytrytu/starred{/owner}{/repo}",
  "followers_url": "https://api.github.com/users/rewtreytrytu/followers",
  "public_gists": 0,
  "url": "https://api.github.com/users/rewtreytrytu",
  "received_events_url": "https://api.github.com/users/rewtreytrytu/received_events",
  "followers": 0,
  "avatar_url": "https://avatars3.githubusercontent.com/u/223638638?v=4",
  "events_url": "https://api.github.com/users/rewtreytrytu/events{/privacy}",
  "html_url": "https://github.com/rewtrey4trytu",
  "following": 0,
  "name": null,
  "location": null,
  "node_id": "MDQ65VXNlc32jIyMzYzODY4"
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值