使用github实现第三方登录

在这里插入图片描述
本例以github为例

先去github申请

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这时候你会拿到这两个参数
在这里插入图片描述

前端准备

   <mu-button
              @click="login"
              style="background-color:#448AFF;color:white;left:40px"
            >一键登录</mu-button>

    login() {
      alert('click')
      const authorize_uri = 'https://github.com/login/oauth/authorize'
      const client_id = 'a29c48c1e7c4f774c6c9'
      const redirect_uri = 'http://localhost:8080/login/oauth2/code/github'
      window.location.href = `${authorize_uri}?client_id=${client_id}&redirect_uri=${redirect_uri}`
    }

后端准备

项目结构
在这里插入图片描述

spring:
  security:
    oauth2:
      client:
        registration:
          github:
            clientId: a29c**************填自己的******774c6c9
            clientSecret: b610b184************填自己的******d75f143297
  thymeleaf:
    check-template-location: true
    enabled: true
    encoding: utf-8
    mode: HTML5
    prefix: classpath:/templates/
    suffix: .html
    template-resolver-order:
server:
  port:8080
package com.soft1851.spring.boot.oauth.controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description TODO
 * @Author 涛涛
 * @Date 2020/4/29 10:58
 * @Version 1.0
 **/
@RestController
public class TestController {
    @RequestMapping("/index")
    public String simpleIndex() {
        return "index";
    }
}

package com.soft1851.spring.boot.oauth;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" lang="">
<meta charset="UTF-8" />
<title></title>
</html>
<body>
<h3>hello</h3>
</body>

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>

        <!--模板引擎Thymeleaf依赖-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.11.RELEASE</version>
        </dependency>

后端运行起来
前端启动
这个时候就开始打卡 F12

在这里插入图片描述
遇到这个,就清空浏览器缓存,建议用插件 Click and Clean在这里插入图片描述
重试 向这样的长时间刷新的页面,别跟我客气,点击这个X,再重新刷新就好,当然你有耐心等就更好,全程不要关F12
在这里插入图片描述
第一次的时候应该进授权页面,同意后就跳到这里,就不马赛克了,除了小姐姐不许加我QQ
在这里插入图片描述
点击登录后
也许会跳到这里在这里插入图片描述
也许会跳到自己写的页面,当然这不重要,数据拿到了。

在这里插入图片描述
在这里插入图片描述
开开心心的打开Postman
红色那个有毛病,不用,用下面的蓝色的
在这里插入图片描述

模拟第三方登录

第一步简单点,一个参数,header也不许要给值
在这里插入图片描述
第一步返回的结果,就是模拟返回的 code,
在这里插入图片描述
去之前的地方把code复制过来
第二步用到
地址栏的框外内容就不用写了,就是下面的参数,写到 ? 就可以了,,算了我复制吧
https://github.com/login/oauth/access_token
注意那个Authrization
在这里插入图片描述
在这里插入图片描述
弄成这样

再次请求

会返回一个token,复制这一段
在这里插入图片描述
第三步:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
大概就是这样
运行后结果就是这样,就代表第三方登录成功
在这里插入图片描述
可能会报错
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
。。。
多于的都关了
再不成功,就是code或者token过期了,整个流程不要太慢,十分钟内完成。
点个赞啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值