2

拥抱Spring全新OAuth解决方案 - 冷冷zz

 1 year ago
source link: https://www.cnblogs.com/leng-leng/p/16352311.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

以下全文 Spring Authorization Server 简称为: SAS

  • Spring 团队正式宣布 Spring Security OAuth 停止维护,该项目将不会再进行任何的迭代
1755711-20220607161719001-1254707895.png
  • 目前 Spring 生态中的 OAuth2 授权服务器是 Spring Authorization Server 已经可以正式生产使用
  • 作为 SpringBoot 3.0 的过渡版本 SpringBoot 2.7.0 过期了大量关于 SpringSecurity 的配置类,如沿用旧版本过期配置无法向上升级。

本文以PIG 微服务开发平台为演示,适用于 Spring Security OAuth 2.3 <-> 2.5 的认证中心迁移

① Java 1.8 支持

目前最新的 SAS 0.3 基于 Java 11 构建,低版本 Java 无法使用

经过和 Spring Security 官方团队的沟通 0.3.1 将继续兼容 Java 1.8

1755711-20220607161719652-1487714051.png

我们联合 springboot 中文社区编译了适配 java 1.8 的版本坐标如下

  <dependency>
      <groupId>io.springboot.security</groupId>
      <artifactId>spring-security-oauth2-authorization-server</artifactId>
      <version>0.3.0</version>
  </dependency>

② 授权模式扩展

1755711-20220607161719347-1605979752.png
  • 扩展支持密码模式,SAS 基于 oauth 2.1 协议不支持密码模式

  • 扩展支持短信登录

③ Redis 令牌存储

支持Redis存储 令牌

  • 官方目前没有提供基于 Redis 令牌持久化方案

  • PIG 扩展 PigRedisOAuth2AuthorizationService 支持

④ Token 输出格式化

  • 使用自省令牌的情况下 默认实现为
ku4R4n7YD1f584KXj4k_3GP9o-HbdY-PDIIh-twPVJTmvHa5mLIoifaNhbBvFNBbse6_wAMcRoOWuVs9qeBWpxQ5zIFrF1A4g1Q7LhVAfH1vo9Uc7WL3SP3u82j0XU5x

默认实现

  • 为方便结合 redis 高效检索 token ,结合 RDM 分组也可以更方便的图形化观察
统一前缀::令牌类型::客户端ID::用户名::uuid
@Bean
public OAuth2TokenGenerator oAuth2TokenGenerator() {
  CustomeOAuth2AccessTokenGenerator accessTokenGenerator = new CustomeOAuth2AccessTokenGenerator();
  // 注入Token 增加关联用户信息
  accessTokenGenerator.setAccessTokenCustomizer(new CustomeOAuth2TokenCustomizer());
  return new DelegatingOAuth2TokenGenerator(accessTokenGenerator, new OAuth2RefreshTokenGenerator());
}

⑤ Token 输出增强

  • 使用自省令牌,默认情况下输出的 Token 格式
{
    "access_token": "xx",
    "refresh_token": "xx",
    "scope": "server",
    "token_type": "Bearer",
    "expires_in": 43199
}
  • Token 增强输出关联用户信息
{
    "sub": "admin",
    "clientId": "test",
    "access_token": "xx",
    "refresh_token": "xx",
    "license": "https://pig4cloud.com",
    "user_info": {
        "username": "admin",
        "accountNonExpired": true,
        "accountNonLocked": true,
        "credentialsNonExpired": true,
        "enabled": true,
        "id": 1,
        "deptId": 1,
        "phone": "17034642999",
        "name": "admin",
        "attributes": {}
    }
}

⑥ 授权码模式个性化

注入自定义confirm

基于授权码的开发平台

⑦ 资源服务器

  • 自省方案扩展支持资源资源服务器本地查询
默认的资源服务器自省模式
  • 扩展资源服务器本地自省

扩展资源服务器本地自省

- 优势: 1. 用户状态实时更新 2. 减少网络调用提升性能


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK