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

2023-05-28 0 421

一、服务器端登入

在同时实现服务器端登入前,咱们先谈谈服务器端登入另一面大体的同时实现方法论吧,标识符同时实现或者说很简单,思想在这种这时候,可能比标识符更重要许多。

现在只要稍稍小的中文网站或者app单厢支持服务器端登入吧,直面他们开发者为主的 gitee、github、google等,直面普通使用者非常多的qq、QQ等服务器端登入。

服务器端登入大多是如前所述OAuth2.0协定同时实现的,OAuth是几项协定,它为使用者资源的许可提供了两个安全、对外开放而固定式的标准,OAuth的许可不能使服务器端冲破到使用者的帐号重要信息(比如公钥)。

市售上也有很多范例,像他们平时肯定也是采用过服务器端登入的。

倘若你注册登记两个互联网平台,在你优先选择采用服务器端登入时,通常单厢重定向到你优先选择的登入互联网平台的登入网页去,在那里进行公钥的输出,以及使用者重要信息的校正。你注册登记的那个互联网平台,不能碰触到你的公钥帐号重要信息等重要信息,通常都是许可许多绰号、肖像等基本重要信息的采用,一定程度上保护了你生前的许多重要信息在互联网上的蔓延和外泄。

二、工程项目中互联网连接Gitee登入

如果是他们从0到1同时实现服务器端或者说也是能的,或者说要不过他们PCB许多允诺而已。市售有因此闻所未闻一类方面帮他们制出了车轮,适度地专业委员会贪心还是能的~

这个适度的贪心,绝非说让大家倚赖在采用这几层,在能够采用之后,你应该要有疑惑和爱好积极探索的这时候,你该去打声想想还有没有更方便快捷的方式,又或者你他们能PCB这样的两个辅助工具吗?亦或者心中有那么一类想知道它是如何同时实现的设想。

我觉得这才是学习这时候的高速成长,因此即使是在以后突然多了许多订制化的需求,你也有足够多大的把握住去同时实现它。

消极的推动,远远不如人格的疑惑相比之下实在和有意思~

2.1、准备环境

建立两个Springboot工程项目,

引入jar~

拥有两个Gitee帐号~

2.1.1、gitee建立两个应用

在个人设置中,找到数据管理中的服务器端应用。

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

然后点击建立应用:

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

建立成功后,拿到Client ID、Client Secret两个重要信息

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

2.1.2、准备工程项目

建立springboot工程项目、引入倚赖

SpringBoot 整合 JustAuth 实现第三方登录 | gitee登录
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.2</version> <relativePath/> <!– lookup parent from repository –> </parent> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.72</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.1.4</version> </dependency> <dependency> <groupId>com.xkcoding.justauth</groupId> <artifactId>justauth-spring-boot-starter</artifactId> <version>1.4.0</version> </dependency> </dependencies> 复制标识符

yml配置文件

server: port: 8089 spring: application: name: springboot-redis justauth: enabled: true type: GITEE: client-id: 建立成功的应用的id client-secret: 建立成功的应用的密钥 redirect-uri: http://127.0.0.1:8089/oauth/gitee/callback #工程项目中的回调地址 cache: type: default 复制标识符

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

2.2、Controller类的编写

package com.nzc.controller; import cn.hutool.json.JSONUtil; importcom.xkcoding.justauth.AuthRequestFactory;import lombok.RequiredArgsConstructor; importlombok.extern.slf4j.Slf4j;import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthResponse; importme.zhyd.oauth.request.AuthRequest;import me.zhyd.oauth.utils.AuthStateUtils; importorg.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; importorg.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; ​ importjavax.servlet.http.HttpServletResponse;import java.io.IOException; import java.util.List; ​ /** * @description: *@author: Yihui Wang * @date: 2022年07月22日 18:47 */ @Slf4j @RestController @RequestMapping(“/oauth”) public class AuthController { ​ @Autowired private AuthRequestFactory factory; ​ @GetMapping public List<String> list() { return factory.oauthList(); } ​ @GetMapping(“/login/{type}”) public void login(@PathVariableString type, HttpServletResponse response) throws IOException { AuthRequest authRequest = factory.get(type); response.sendRedirect(authRequest.authorize(AuthStateUtils.createState())); } ​@GetMapping(“/{type}/callback”) public AuthResponse login(@PathVariableString type, AuthCallback callback) { AuthRequest authRequest = factory.get(type); AuthResponse response = authRequest.login(callback); log.info(“【response】= {}”, JSONUtil.toJsonStr(response));return response; } ​ } 复制标识符

咋说勒,到这一步,你就成功引入了Gitee的服务器端登入~

justAuth 用两个字来形容就是简和全。

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

他们首先访问localhost:8089/login/{type}接口,这里的type是同时实现哪个服务器端登入,这里就是填什么类型,像他们同时实现了 gitee,此处便填写gitee。

进入方法后,AuthRequestFactory通过 type,get出两个AuthRequest,这个AuthRequest是两个接口,里面是一早就PCB了相关服务器端登入的允诺接口,他们是直接拿来用的。稍稍看一下源码就知道了~

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

response.sendRedirect(authRequest.authorize(

AuthStateUtils.createState()));是重定向到服务器端登入的接口,正确许可后,会重定向到他们先前在应用中写好的回调方法中 。

第二个接口/oauth/{type}/callback是就是正确许可后,允诺的回调接口,即是当 gitee 许可通过后,将会调用的接口。

2.3、测试同时实现

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

如果是没登入的情况下,会重定向到登入页,如果是已登入的情况,就是两个许可的界面。

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

成功登入后就会调用他们写好的回调接口,返回登入使用者的相关重要信息。

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

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

{ “code”: 2000, “msg”: null, “data”: { “username”: “crushlxb”, “nickname”: “宁在春”, “gender”: “UNKNOWN”, “source”: “GITEE”, “token”: { “accessToken”: “33087141a9f029f0ad647b720653104e”, “expireIn”: 86400, “refreshToken”: “c5d35725a443d62deb106febb99f2e1c534cc394dfb39c00c7e9d1ccf3a35b4e”, “refreshTokenExpireIn”: 0, }, “rawUserInfo”: { “gists_url”: “https://gitee.com/api/v5/users/crushlxb/gists{/gist_id}”, “repos_url”: “https://gitee.com/api/v5/users/crushlxb/repos”, } } } 复制标识符

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

大家感兴趣也能去看一看~

原文:

https://juejin.cn/post/7133617235792232462

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务