• SpringBoot 整合 JustAuth 实现第三方登录 | gitee登录


    我第一次想去实现gitee、github登录的时候,完全就是各种搜索,看到一个知识点去了解一个知识点,当时 JustAuth 其实已经存在了,但是远没有现在这么容易搜索到。

    一、第三方登录

    在实现第三方登录前,咱们先聊聊第三方登录背后大致的实现逻辑吧,代码实现其实很简单,思想在这种时候,可能比代码更为重要一些。


    现在只要稍微大点的网站或是app都会支持第三方登录吧,面对我们开发者居多的 gitee、github、google等,面对普通用户较多的qq、微信等第三方登录。

    第三方登录大都是基于OAuth2.0协议实现的,OAuth是一项协议,它为用户资源的授权提供了一个安全、开放而简易的标准,OAuth的授权不会使第三方触及到用户的账号信息(比如密码)。

    市面上也有很多例子,像我们平常肯定也是使用过第三方登录的。

    假如你注册一个平台,在你选择使用第三方登录时,一般都会跳转到你选择的登录平台的登录页面去,在那里进行密码的输入,以及用户信息的验证。你注册的那个平台,不会接触到你的密码账号信息等信息,一般都是授权一些昵称、头像等基本信息的使用,一定程度上保护了你本人的一些信息在网络上的扩散和泄露。

    二、项目中接入Gitee登录

    如果是自己从0到1实现第三方其实也是可以的,只不过要不过自己封装一些请求罢了。市面有并且不止一种方面帮我们造出了轮子,适当的学会偷懒还是可以的~

    这个适当的偷懒,并非说让大家依赖在使用这一层,在能够使用之后,你应该要有好奇和热爱探索的时候,你该去试着想一想还有没有更简便的方式,又或是你自己可以封装这样的一个工具吗?亦或是心中有那么一种想知道它是如何实现的想法。

    我觉得这才是学习时候的成长,并且就算是在以后突然多了一些定制化的需求,你也有足够大的把握去实现它。

    被动的推动,远不如自我的好奇来的实在和有趣~

    2.1、准备环境

    创建一个Springboot项目

    导入jar~

    拥有一个Gitee账号~

    2.1.1、gitee创建一个应用

    在个人设置中,找到数据管理中的第三方应用。

    然后点击创建应用:

    创建成功后,拿到Client ID、Client Secret两个信息

    2.1.2、准备项目

    创建springboot项目、导入依赖

    1.  <parent>
    2.      <groupId>org.springframework.boot</groupId>
    3.      <artifactId>spring-boot-starter-parent</artifactId>
    4.      <version>2.5.2</version>
    5.      <relativePath/> <!-- lookup parent from repository -->
    6.  </parent>
    7.  <properties>
    8.   <maven.compiler.source>8</maven.compiler.source>
    9.   <maven.compiler.target>8</maven.compiler.target>
    10.  </properties>
    11.  <dependencies>
    12.      <dependency>
    13.          <groupId>org.springframework.boot</groupId>
    14.          <artifactId>spring-boot-starter</artifactId>
    15.          <version>2.5.2</version>
    16.      </dependency>
    17.      <dependency>
    18.          <groupId>org.springframework.boot</groupId>
    19.          <artifactId>spring-boot-starter-web</artifactId>
    20.          <version>2.5.2</version>
    21.      </dependency>
    22.      <dependency>
    23.          <groupId>org.springframework.boot</groupId>
    24.          <artifactId>spring-boot-starter-test</artifactId>
    25.      </dependency>
    26.      <dependency>
    27.          <groupId>junit</groupId>
    28.          <artifactId>junit</artifactId>
    29.          <version>4.12</version>
    30.      </dependency>
    31.      <dependency>
    32.          <groupId>com.alibaba</groupId>
    33.          <artifactId>fastjson</artifactId>
    34.          <version>1.2.72</version>
    35.      </dependency>
    36.      <dependency>
    37.          <groupId>org.projectlombok</groupId>
    38.          <artifactId>lombok</artifactId>
    39.          <version>1.18.20</version>
    40.      </dependency>
    41.      <dependency>
    42.          <groupId>cn.hutool</groupId>
    43.          <artifactId>hutool-all</artifactId>
    44.          <version>5.1.4</version>
    45.      </dependency>
    46.      <dependency>
    47.          <groupId>com.xkcoding.justauth</groupId>
    48.          <artifactId>justauth-spring-boot-starter</artifactId>
    49.          <version>1.4.0</version>
    50.      </dependency>
    51.  </dependencies>
    52. 复制代码

    yml配置文件

    1.  server:
    2.   port: 8089
    3.  spring:
    4.   application:
    5.     name: springboot-redis
    6.  justauth:
    7.   enabled: true
    8.   type:
    9.     GITEE:
    10.       client-id: 创建成功的应用的id
    11.       client-secret: 创建成功的应用的密钥
    12.       redirect-uri: http://127.0.0.1:8089/oauth/gitee/callback #项目中的回调地址
    13.   cache:
    14.     type: default
    15. 复制代码

    另外还有个主启动类,无需特殊注解,普通的SpringBoot启动类即可。

    2.2、Controller类的编写

    1.  package com.nzc.controller;
    2.  ​
    3.  import cn.hutool.json.JSONUtil;
    4.  import com.xkcoding.justauth.AuthRequestFactory;
    5.  import lombok.RequiredArgsConstructor;
    6.  import lombok.extern.slf4j.Slf4j;
    7.  import me.zhyd.oauth.model.AuthCallback;
    8.  import me.zhyd.oauth.model.AuthResponse;
    9.  import me.zhyd.oauth.request.AuthRequest;
    10.  import me.zhyd.oauth.utils.AuthStateUtils;
    11.  import org.springframework.beans.factory.annotation.Autowired;
    12.  import org.springframework.web.bind.annotation.GetMapping;
    13.  import org.springframework.web.bind.annotation.PathVariable;
    14.  import org.springframework.web.bind.annotation.RequestMapping;
    15.  import org.springframework.web.bind.annotation.RestController;
    16.  ​
    17.  import javax.servlet.http.HttpServletResponse;
    18.  import java.io.IOException;
    19.  import java.util.List;
    20.  ​
    21.  /**
    22.   * @description:
    23.   * @author: Yihui Wang
    24.   * @date: 2022年07月22日 18:47
    25.   */
    26.  @Slf4j
    27.  @RestController
    28.  @RequestMapping("/oauth")
    29.  public class AuthController {
    30.  ​
    31.      @Autowired
    32.      private  AuthRequestFactory factory;
    33.  ​
    34.      @GetMapping
    35.      public List list() {
    36.          return factory.oauthList();
    37.     }
    38.  ​
    39.      @GetMapping("/login/{type}")
    40.      public void login(@PathVariable String type, HttpServletResponse response) throws IOException {
    41.          AuthRequest authRequest = factory.get(type);
    42.          response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
    43.     }
    44.  ​
    45.      @GetMapping("/{type}/callback")
    46.      public AuthResponse login(@PathVariable String type, AuthCallback callback) {
    47.          AuthRequest authRequest = factory.get(type);
    48.          AuthResponse response = authRequest.login(callback);
    49.          log.info("【response】= {}", JSONUtil.toJsonStr(response));
    50.          return response;
    51.     }
    52.  ​
    53.  }
    54. 复制代码

    咋说勒,到这一步,你就成功引入了Gitee的第三方登录~

    justAuth 用两个字来形容就是

    我先来说一说这个controller类的一些操作,

    我们首先访问localhost:8089/login/{type}接口,这里的type是实现哪个第三方登录,这里就是填什么类型,像我们实现了 gitee,此处便填写gitee

    进入方法后,AuthRequestFactory通过 type,get出一个AuthRequest,这个AuthRequest是一个接口,里面是一早就封装了相关第三方登录的请求接口,我们是直接拿来用的。稍微看一下源码就知道了~

    response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));是重定向到第三方登录的接口,正确授权后,会跳转到我们先前在应用中写好的回调方法中 。


    第二个接口/oauth/{type}/callback是就是正确授权后,请求的回调接口,即是当 gitee 授权通过后,将会调用的接口。

    2.3、测试实现

    如果是没登录的情况下,会跳转到登录页,如果是已登录的情况,就是一个授权的界面。

    成功登录后就会调用我们写好的回调接口,返回登录用户的相关信息。

    这一段json数据其实就含有我们想要的信息,也有着我之后想继续写文章的知识点。

    1.  {
    2.    "code": 2000,
    3.    "msg": null,
    4.    "data": {
    5.      "username": "crushlxb",
    6.      "nickname": "宁在春",
    7.      "gender": "UNKNOWN",
    8.      "source": "GITEE",
    9.      "token": {
    10.        "accessToken": "33087141a9f029f0ad647b720653104e",
    11.        "expireIn": 86400,
    12.        "refreshToken": "c5d35725a443d62deb106febb99f2e1c534cc394dfb39c00c7e9d1ccf3a35b4e",
    13.        "refreshTokenExpireIn": 0,
    14.     },
    15.      "rawUserInfo": {
    16.        "gists_url": "https://gitee.com/api/v5/users/crushlxb/gists{/gist_id}",
    17.        "repos_url": "https://gitee.com/api/v5/users/crushlxb/repos",
    18.     }
    19.   }
    20.  }
    21. 复制代码

    一些我个人信息的东西,我都去掉了,这里面我比较感兴趣的还是accessTokenrefreshToken的实现,并不是说是多难,而是觉得这是值得写上一篇文章的知识点,至少它是比较实用的的~

    大家感兴趣也可以去看一看~

    后记

    JustAuth 的 gitee地址justauth-spring-boot-starter在这里,如果对大家有帮助,希望大家能去点个Star,我觉得每一个正向反馈,都是对于开源作者的鼓励,也是他们坚持下去的动力。

    我觉得技术的发展从来都不是因为某个人而发展庞大的,而是我们这一整个群体

    我对技术充满浪漫主义的想法,也只有在文章中能写一写,所以偶尔我就喜欢上说上这样的一番废话,其实我每次在写到开源的项目或技术时,我都会提一嘴点star这样的事情,倒不是打广告,而是我感觉这其实和我写文章是一样的感受,希望能够接收到大家的正向反馈。

    若是不喜,望各位见谅,并竟我的创作除了希望自己的文字能产生对他人的帮助外,就是希望能够满足自己的愉悦和诉说自己的言语


     

  • 相关阅读:
    Java枚举类(Enum)和注解(Annotation)讲解——Java第十二讲
    C#知识总结 基础篇(上)
    java计算机毕业设计苗木购销系统源码+系统+mysql数据库+lw文档
    9月7日扒面经
    echarts制作关系图谱
    QT+OSG/osgEarth编译之五十二:osgVolume+Qt编译(一套代码、一套框架,跨平台编译,版本:OSG-3.6.5工具库osgVolume)
    Vue再次入门<一>互动教程
    【云原生】SpringCloud系列之客户端负载均衡Ribbon
    Javascript V8引擎与Blob对象
    grpc学习分享
  • 原文地址:https://blog.csdn.net/java_beautiful/article/details/126462735