类 OAuth2Cache

java.lang.Object
net.sohelp.boot.admin.core.cache.OAuth2Cache
所有已实现的接口:
ISohelpCache

@Component public class OAuth2Cache extends Object implements ISohelpCache
OAuth2 认证缓存(TTL 10分钟)

缓存 app_id → app_secret 映射关系,支持 MEMORY / REDIS / NONE 三种模式。

  • NONE 模式:每次 get 直接查询 pb_user_oauth2 表。
  • MEMORY/REDIS:缓存未命中时自动 reload 全量数据。
从以下版本开始:
2024/1/4 11:15
作者:
AaronFung
  • 构造器详细资料

    • OAuth2Cache

      public OAuth2Cache()
  • 方法详细资料

    • init

      @PostConstruct public void init()
      初始化缓存提供者

      在 Spring Bean 初始化完成后自动调用,通过 SohelpCacheManager 创建名为 "oauth2" 的缓存提供者,并设置 10 分钟的过期时间。

    • cacheName

      public String cacheName()
      获取缓存名称
      指定者:
      cacheName 在接口中 ISohelpCache
      返回:
      缓存唯一标识名称 "oauth2"
    • cacheDescription

      public String cacheDescription()
      获取缓存描述信息
      指定者:
      cacheDescription 在接口中 ISohelpCache
      返回:
      缓存的中文描述 "OAuth2认证缓存"
    • reload

      public void reload() throws SQLException
      重载缓存数据

      清空当前缓存并从数据库重新加载所有启用的 OAuth2 客户端配置。

      指定者:
      reload 在接口中 ISohelpCache
      抛出:
      SQLException - 数据库查询异常
    • initial

      @Comment("\u521d\u59cb\u5316OAuth2") public void initial() throws SQLException
      全量加载所有启用的 OAuth2 客户端配置到缓存

      该方法为同步方法,执行时会先清空缓存,然后从 pb_user_oauth2 表中查询 所有状态为启用(app_status=1)的记录,将 app_idapp_secret 的映射关系写入缓存。

      抛出:
      SQLException - 数据库查询过程中发生异常
    • get

      @Comment("\u6839\u636eAppId\u83b7\u53d6\u5bc6\u94a5") public String get(@Comment("AppId") String appId)
      根据 AppId 获取对应的密钥

      该方法为同步方法,查找逻辑如下:

      1. 若缓存未启用(NONE 模式),直接查询数据库并返回结果。
      2. 若缓存已启用但未命中指定 appId,触发全量重载。
      3. 从缓存中获取对应值并返回。
      指定者:
      get 在接口中 ISohelpCache
      参数:
      appId - 客户端应用标识(App ID)
      返回:
      对应的 app_secret 密钥字符串;未找到时返回 null