SpringSecurity:通过OAuth2集成Github

简介: Spring Securtity OAuth2集成Github完成身份证认证

nuts-and-bolts-of-oauth

1. 背景

近期由于新建的项目,因为涉及到前后端分离以及单点登录,综合考虑,决定采用 SpringSecurity + Oauth2.0 ,我也正好趁此机会学习下此类知识,回想起最近一次学习安全框架还是 Shiro。 SSO 基础就不说了,敢兴趣的可以自行百度或者谷歌。

因为 SpringSecurity 本身提供了 GOOGLE GITHUB FACEBOOK OKTAOAuth2.0 接入支持,具体源码都在枚举类CommonOAuth2Provider 中。


public enum CommonOAuth2Provider {
    GOOGLE {
        public ClientRegistration.Builder getBuilder(String registrationId) {
            ClientRegistration.Builder builder = this.getBuilder(registrationId, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, "{baseUrl}/{action}/oauth2/code/{registrationId}");
            builder.scope(new String[]{"openid", "profile", "email"});
            builder.authorizationUri("https://accounts.google.com/o/oauth2/v2/auth");
            builder.tokenUri("https://www.googleapis.com/oauth2/v4/token");
            builder.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs");
            builder.issuerUri("https://accounts.google.com");
            builder.userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo");
            builder.userNameAttributeName("sub");
            builder.clientName("Google");
            return builder;
        }
    },
    GITHUB {
        public ClientRegistration.Builder getBuilder(String registrationId) {
            ClientRegistration.Builder builder = this.getBuilder(registrationId, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, "{baseUrl}/{action}/oauth2/code/{registrationId}");
            builder.scope(new String[]{"read:user"});
            builder.authorizationUri("https://github.com/login/oauth/authorize");
            builder.tokenUri("https://github.com/login/oauth/access_token");
            builder.userInfoUri("https://api.github.com/user");
            builder.userNameAttributeName("id");
            builder.clientName("GitHub");
            return builder;
        }
    },
    FACEBOOK {
        public ClientRegistration.Builder getBuilder(String registrationId) {
            ClientRegistration.Builder builder = this.getBuilder(registrationId, ClientAuthenticationMethod.CLIENT_SECRET_POST, "{baseUrl}/{action}/oauth2/code/{registrationId}");
            builder.scope(new String[]{"public_profile", "email"});
            builder.authorizationUri("https://www.facebook.com/v2.8/dialog/oauth");
            builder.tokenUri("https://graph.facebook.com/v2.8/oauth/access_token");
            builder.userInfoUri("https://graph.facebook.com/me?fields=id,name,email");
            builder.userNameAttributeName("id");
            builder.clientName("Facebook");
            return builder;
        }
    },
    OKTA {
        public ClientRegistration.Builder getBuilder(String registrationId) {
            ClientRegistration.Builder builder = this.getBuilder(registrationId, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, "{baseUrl}/{action}/oauth2/code/{registrationId}");
            builder.scope(new String[]{"openid", "profile", "email"});
            builder.userNameAttributeName("sub");
            builder.clientName("Okta");
            return builder;
        }
    };

这里仅对 Github 单点登录作为样例,作此说明,都是在 Windows 中的开发环境。

2. Client注册登记

  • 注册:在 Github 中注册一个 Client 应用,界面生成 client-idclient-secret

注册地址:https://github.com/settings/applications/new

20220813220500

注册,比较简单,自行。

3. SSL证书

演示环境下必须是 HTTPS 类型,否则会不成功。详细配置参考[[Java生成SSL证书]]

如果不安装证书,会有如下情况。

20220813232536

4. IDEA配置

4.1. pom


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-client</artifactId>
    </dependency>
</dependencies>

4.2. yml


server:
  ssl:
    key-store: https.keystore
    key-store-password: 123456
    key-alias: tomcat
  port: 9006
spring:
  security:
    oauth2:
      client:
        registration:
          github:
            #对应Github账号配置的Client ID
            client-id: 08bc4fb36fxx580a57c1
            #对应Github账号配置的Client secrets
            client-secret: df677b978decxefab1c95d4e28288b86913c323

5. 验证Github信息

输入 Github 的账号、密码,会进入我们之前配置的 Home

登录Github

5.1. Home主页

20220813231139

5.2. 查看注册信息

20220814003106

5.3. 查看Token

20220814003127

5.4. 查看用户信息

20220814003203

目录
相关文章
|
6月前
|
开发工具 git
Playwright系列(6):如何集成到GitHub
Playwright系列(6):如何集成到GitHub
137 0
Playwright系列(6):如何集成到GitHub
|
6月前
|
安全 Java 数据库
后端进阶之路——万字总结Spring Security与数据库集成实践(五)
后端进阶之路——万字总结Spring Security与数据库集成实践(五)
|
6月前
GitHub和Gitee的基本使用和在IDEA中的集成
GitHub和Gitee的基本使用和在IDEA中的集成
74 0
|
6月前
|
前端开发 安全 Java
SpringBoot 实现登录验证码(附集成SpringSecurity)
SpringBoot 实现登录验证码(附集成SpringSecurity)
405 0
|
2月前
|
Shell 网络安全 开发工具
Git,GitHub,Gitee&IDEA集成Git
Git提交项目到GitHub简洁版、版本控制、安装、常用命令、分支、团队协作机制、Github、Gitee远程仓库、IDEA集成Git、IDEA集成Github、IDEA集成Gitee
Git,GitHub,Gitee&IDEA集成Git
|
3月前
|
NoSQL 关系型数据库 MySQL
SpringBoot 集成 SpringSecurity + MySQL + JWT 附源码,废话不多直接盘
SpringBoot 集成 SpringSecurity + MySQL + JWT 附源码,废话不多直接盘
141 2
|
3月前
|
jenkins 持续交付
jenkins学习笔记之九:jenkins认证集成github
jenkins学习笔记之九:jenkins认证集成github
|
5月前
|
存储 安全 Java
Spring Security与OAuth2集成开发
Spring Security与OAuth2集成开发
|
4月前
|
存储 安全 Java
在Spring Boot中集成OAuth2
在Spring Boot中集成OAuth2
|
4月前
|
存储 安全 Java
在Spring Boot中集成OAuth2实现安全认证
在Spring Boot中集成OAuth2实现安全认证