写点什么

Spring Security OAuth 实现 GitHub 快捷登录

作者:阿提说说
  • 2022 年 8 月 07 日
  • 本文字数:1613 字

    阅读完需:约 5 分钟

Spring Security OAuth实现GitHub快捷登录

前言

Spring Security 5 中集成了 OAuth 的客户端模块,该模块包含以下三个子模块:


  1. spring-security-oauth2-core

  2. OAuth 授权框架和 OIDC 的核心数据结构及接口,被 Client、 Resource Server 和 Authorization Server 所依赖。

  3. spring-security-oauth2-jose

  4. 支持 JOSE 协议组,具体包含:JSON Web Token(JWT)、JSON Web Signature(JWS)、JSON Web Encryption(JWE)、JSON Web Key(JWK)。

  5. spring-security-oauth2-client

  6. Spring Security 支持 OAuth 和 OIDC 的客户端功能实现包。

实现 GitHub 快捷登录

源代码地址:https://github.com/jujunchen/21Study

新建工程

新创建一个 Spring Boot 工程,pom 依赖如下


<dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-oauth2-client</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
<!--单元测试--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency>
</dependencies>
复制代码


注意: 必须引入spring-boot-starter-oauth2-client依赖

注册 OAuth 应用

在 GitHub 上注册一个 OAuth 应用


地址是:https://github.com/settings/applications/new


注册界面如下:



  • Application name:必填,应用名称

  • Homepage URL:必填,主页的 URL 地址,本地开发,我们将其设置为 http://localhost:8080

  • Application description:非必填,应用描述

  • Authorization callback URL:必填,OAuth 认证的重定向地址,本地开发环境设置为 http://localhost:8080/login/oauth2/code/github


点击 Register application 按钮注册,注册完成得到 clientId 和 clientSecret

配置 application.yml

接下来在配置文件中增加对于的配置


spring:  security:    oauth2:      client:        registration:          github:            client-id: github-client-id            client-secret: github-client-secret
复制代码


其中:


(1)spring.security.oauth2.client.registration 是 OAuth 客户端所有属性的基础前缀。


(2)registration 下面的 github 是 ClientRegistration 的唯一 ID。


Spring Security OAuth 默认的重定向模板是{baseUrl}/login/oauth2/code/{registrationId},registrationId 是 ClientRegistration 的唯一 ID,通常以接入的 OAuth 服务提供商的简称来命名即可,所以此处设置为 github。


github-client-id 和 github-client-secret 需要替换为前面在 GitHub 上注册得到的 clientId 和 clientSecret。

新建 Controller

@RestControllerpublic class HelloController {        @GetMapping("/hello")    public String hello(Principal principal) {        return "Hello," + principal.getName();    }}
复制代码


principal 对象由 Spring 框架自动注入,表示当前登录的用户。

演示

  1. 启动 Spring Boot 应用

  2. 访问 http://localhost:8080/hello 的时候,会跳转到 github 的登录页



3. 点击 Authorize xxx(授权)按钮后,回调到http://localhost:8080/login/oauth2/code/github?code=dc3a0c2d3a77a8ade7dc&state=Pq69SdJxwxeV-nY4aLkX-DGu31qWNFl-5EMRml1sRdM%3D地址,该地址就为前面配置的 OAuth 认证的重定向地址,code 和 state 为 github 自动拼接。最终浏览器显示"Hello,xxx"


由于 github 在国内经常超时,后面将介绍如何使用 Gitee 进行集成登录。


发布于: 刚刚阅读数: 3
用户头像

阿提说说

关注

一年太久,只争朝夕 2017.10.19 加入

还未添加个人简介

评论

发布
暂无评论
Spring Security OAuth实现GitHub快捷登录_Spring Security OAuth_阿提说说_InfoQ写作社区