• Spring Security OAuth实现GitHub快捷登录


    活动地址:CSDN21天学习挑战赛

    前言

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

    1. spring-security-oauth2-core
      OAuth授权框架和OIDC的核心数据结构及接口,被Client、 Resource Server和Authorization Server所依赖。
    2. spring-security-oauth2-jose
      支持JOSE协议组,具体包含:JSON Web Token(JWT)、JSON Web Signature(JWS)、JSON Web Encryption(JWE)、JSON Web Key(JWK)。
    3. spring-security-oauth2-client
      Spring Security支持OAuth和OIDC的客户端功能实现包。

    实现GitHub快捷登录

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

    新建工程

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-oauth2-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
    
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.securitygroupId>
            <artifactId>spring-security-testartifactId>
            <scope>testscope>
        dependency>
    
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    注意: 必须引入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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    其中:
    (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

    @RestController
    public class HelloController {
        
        @GetMapping("/hello")
        public String hello(Principal principal) {
            return "Hello," + principal.getName();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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进行集成登录。

  • 相关阅读:
    Windows下使用Bat文件杀死进程
    我的 Kafka 旅程 - SASL+ACL 认证授权 · 配置 · 创建账号 · 用户授权 · .NET接入
    诺和诺德斥资2亿美元打造首台面向生命科学的量子计算机
    基于微信小程序的奶茶在线预定点单管理系统
    微服务拆分总结(一)
    serveless 思想 Midway.js 框架使用教程(四)
    4.【刷爆LeetCode】组队竞赛(多方法、多思路)
    java类的初始化
    Gridea,一个小而美的博客梦想桥梁
    用半天时间从零开始复习前端之html
  • 原文地址:https://blog.csdn.net/weixin_40972073/article/details/126208537