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

  • 相关阅读:
    代码随想录算法训练营第27天|39. 组合总和 40.组合总和II 131.分割回文串
    主机ping不通虚拟机,虚拟机可以ping同主机
    瞬态抑制二极管TVS的核心参数?|深圳比创达电子EMC(下)
    开发工程师必备————【Day09】数据库基础知识与基本SQL语句
    故障诊断 | 用于跨机器工况下故障诊断的深度判别迁移学习网络附Pytorch代码
    TCP三次握手
    Oracle dblink 跨库查询详解
    解决Flutter位于悬浮窗口时,应用Logo不更新问题
    【Leetcode每日一题:754.到达终点的数字~~~数学分析+奇偶性判断+二分查找】
    李宏毅机器学习笔记(2016年的课程):Support Vector Machine (SVM)
  • 原文地址:https://blog.csdn.net/weixin_40972073/article/details/126208537