Apereo CAS 的 YubiKey Permissive 注册:用静态配置映射管理 MFA 设备白名单

原创2026-10-14 09:27:411,703 阅读
文章标签:后端认证鉴权单点登录身份认证

Apereo CAS 的 YubiKey Permissive 注册:用静态配置映射管理 MFA 设备白名单

导读

Permissive(宽松式)YubiKey 注册是 Apereo CAS 多因素认证体系中一种零存储、纯静态的设备授权方式:管理员直接在 CAS 配置文件中,以「用户名 → YubiKey 设备 public id」的键值映射声明哪些用户可以使用哪些硬件令牌,无需引入数据库、JSON 文件或 REST 接口。读完本文,你将掌握 cas.authn.mfa.yubikey.allowed-devices 属性的完整写法、它在 YubiKeyAccountRegistry 装配链中的底层实现原理,以及它与 JSON、JDBC、MongoDB、Redis、REST 等其他注册方式的分工边界,可直接套用于生产环境的最小化 YubiKey MFA 部署。

一、什么是 Permissive YubiKey 注册

CAS 官方文档对 Permissive Registration 的定义非常直接:注册记录可以通过 CAS 设置静态指定(specified statically via CAS settings),其形态是一张把「已注册用户名」与「YubiKey 设备的 public id」关联起来的映射表(map)。

它的核心特点可以概括为三点:

  1. 零外部依赖:设备清单完全内嵌在 CAS 配置文件中,不依赖任何持久化存储;
  2. 声明式管理:设备授权等同于修改配置文件,部署即生效,天然适合设备数量少、变更不频繁的团队或内部系统;
  3. Permissive 语义:凡是出现在映射表中的用户名,即被认为「已注册 YubiKey」,认证时按 public id 精确匹配设备。

与之相对的,CAS 还支持 JSON 资源注册表、REST 注册表,以及通过 配置模型 暴露的 JPA、MongoDB、Redis、DynamoDB 等持久化注册方式;当上述全部未配置时,CAS 还会退化为 OpenYubiKeyAccountRegistry(任何凭据都被视为合格)。Permissive 注册正是这些选项中「最轻量、最静态」的一种。

二、核心配置项:cas.authn.mfa.yubikey.allowed-devices

2.1 属性定义与语义

该属性在源码中的定义位于 YubiKeyMultifactorAuthenticationProperties.java:

/**
 * Collection of allowed devices allowed per user.
 * This is done using a key-value structure where the key is the user
 * the value is the allowed collection of yubikey device ids.
 */
private Map<String, String> allowedDevices = new LinkedHashMap<>(1);

参数语义如下:

配置路径 类型 说明
cas.authn.mfa.yubikey.allowed-devices Map<String, String> 键为 CAS 用户名(principal id),值为该用户被允许使用的 YubiKey 设备 public id(即 OTP 中前 12 位设备标识符)

需要注意,该属性归属于 YubiKeyMultifactorAuthenticationProperties,整个属性树以 cas.authn.mfa.yubikey.* 为前缀,且该配置类标注了 @RequiresModule(name = "cas-server-support-yubikey"),即必须引入 cas-server-support-yubikey 模块才会生效。

2.2 完整配置示例

一个最小可用的 Permissive 注册配置如下(YAML 格式):

cas:
  authn:
    mfa:
      yubikey:
        client-id: 18423
        secret-key: zAIqhjui12mK8x82oe9qzBEb0As=
        allowed-devices:
          casuser: cccdbgvhhbivhhegjkdhbkbhgelukhdn
          casuseryubikey: device-identifier

其中:

  • client-id 与 secret-key 为必填项(源码中以 @RequiredProperty 标注,见 属性模型),用于构造 YubicoClient 并调用 YubiCloud 验证服务;
  • allowed-devices 的每个条目对应一个用户的静态授权设备;
  • 上例中的 casuseryubikey: device-identifier 正是仓库测试用例中的占位值,实际部署时应替换为真实的 YubiKey public id。

2.3 测试用例佐证

仓库中的集成测试 YubiKeyConfigurationAllowedDevicesTests.java 直接验证了这一配置路径的可用性:

@SpringBootTest(classes = BaseYubiKeyTests.SharedTestConfiguration.class,
    properties = {
        "cas.authn.mfa.yubikey.client-id=18423",
        "cas.authn.mfa.yubikey.secret-key=zAIqhjui12mK8x82oe9qzBEb0As=",
        "cas.authn.mfa.yubikey.allowed-devices.casuseryubikey=device-identifier"
    })
class YubiKeyConfigurationAllowedDevicesTests extends BaseYubiKeyTests {
    @Test
    void verifyOperation() {
        assertTrue(yubiKeyAccountRegistry.isYubiKeyRegisteredFor("casuseryubikey"));
    }
}

该测试断言:只要在配置中声明了 allowed-devices.casuseryubikey=device-identifier,注入的 yubiKeyAccountRegistry 就会判定用户 casuseryubikey 已完成 YubiKey 注册。同样的配置模式还出现在 YubiKeyMultifactorAuthenticatorDeviceManagerTests.java 与 YubiKeyMultifactorAuthenticationDeviceProviderActionTests.java 中,说明静态设备映射贯穿「注册判定 → 设备管理 → Webflow 设备展示」全链路。

三、底层实现原理

3.1 注册表装配:配置优先级与 Permissive 分支

YubiKeyAccountRegistry 到底选择哪种实现,由自动配置类 YubiKeyAuthenticationEventExecutionPlanConfiguration.java 决定,其判断顺序为:

  1. JSON 优先:yubi.getJson().getLocation() != null 时使用 JsonYubiKeyAccountRegistry(支持 watchResource 热加载);
  2. REST 其次:yubi.getRest().getUrl() 非空时使用 RestfulYubiKeyAccountRegistry;
  3. Permissive 分支:allowedDevices 非空时,把配置映射转换为内存中的 YubiKeyAccount 集合,构建 PermissiveYubiKeyAccountRegistry;
  4. 兜底退化:以上均未配置时启用 OpenYubiKeyAccountRegistry,并输出警告:
LOGGER.warn("All credentials are considered eligible for YubiKey authentication. "
        + "Consider providing an account registry implementation via [{}]", ...);

Permissive 分支的核心装配代码(L123-L140)做了三件事:将每个用户名包装成带设备列表的 YubiKeyAccount、为每条设备记录生成随机的 name 与 UTC 注册时间、最后以 CipherExecutor.noOpOfSerializableToString() 作为加解密器(因为静态数据本身无需脱敏存储)。

if (yubi.getAllowedDevices() != null && !yubi.getAllowedDevices().isEmpty()) {
    val map = (Map<String, YubiKeyAccount>) yubi.getAllowedDevices()
        .entrySet().stream()
        .map(entry -> YubiKeyAccount.builder().id(System.currentTimeMillis()).username(entry.getKey())
            .devices(CollectionUtils.wrapList(YubiKeyRegisteredDevice.builder()
                .publicId(entry.getValue())
                .name(UUID.randomUUID().toString())
                .registrationDate(ZonedDateTime.now(Clock.systemUTC()))
                .id(System.currentTimeMillis())
                .build()))
            .build())
        .collect(Collectors.toMap(YubiKeyAccount::getUsername, Function.identity()));
    val registry = new PermissiveYubiKeyAccountRegistry(map, yubiKeyAccountValidator);
    registry.setCipherExecutor(CipherExecutor.noOpOfSerializableToString());
    return registry;
}

3.2 PermissiveYubiKeyAccountRegistry 的行为

注册表实现位于 PermissiveYubiKeyAccountRegistry.java,继承自 BaseYubiKeyAccountRegistry,内部持有一个 Map<String, YubiKeyAccount> devices。其关键判定逻辑:

  • 用户是否已注册(L29-L33):账户存在且设备列表非空即视为已注册;
  • 设备是否匹配(L35-L46):先对每条设备记录的 public id 做 decodeYubikeyRegisteredDevice 解码,再与本次提交的 public id 精确比对;
  • 增删改:save / update / delete / deleteAll 全部直接操作内存 Map,无任何持久化语义。

这也解释了为什么它被称为「Permissive」——只要配置里声明了该用户,CAS 就「允许」其使用 YubiKey,后续认证则完全依赖 YubiCloud 对 OTP 的在线校验来保证安全性。

3.3 认证链路:从 OTP 到 YubiCloud

静态设备清单只是「准入白名单」,真正的令牌校验由 YubiKeyAuthenticationHandler.java 完成,认证流程为:

  1. 校验 OTP 格式(YubicoClient.isValidOTPFormat),格式非法直接抛出 AccountNotFoundException;
  2. 从进行中的认证事件取出 principal,得到用户名 uid;
  3. 调用 registry.getAccountValidator().getTokenPublicId(otp) 解析设备 public id;
  4. 调用 registry.isYubiKeyRegisteredFor(uid, publicId) 检查静态白名单,未注册则拒绝;
  5. 调用 client.verify(otp) 提交 YubiCloud 验证,ResponseStatus.OK 才创建认证结果。

其中第 4 步正是 Permissive 注册表生效的位置:即便 OTP 本身是合法的,只要设备的 public id 不在该用户的静态映射中,认证同样会被拒绝,从而兼顾了「静态声明」与「在线核验」两道防线。

四、配套配置与验证策略

除了 allowed-devices,同一属性类还提供了多个影响 Permissive 注册行为的配套项(均可从 配置模型 中确认):

配置路径 默认值 作用
cas.authn.mfa.yubikey.client-id 0(必填) YubiCloud 客户端 ID,YubicoClient.getClient(clientId, secretKey) 的入参
cas.authn.mfa.yubikey.secret-key 空(必填) YubiCloud API 密钥
cas.authn.mfa.yubikey.api-urls 空列表 自定义 YubiKey WSAPI 验证端点列表,通过 client.setWsapiUrls(urls) 注入
cas.authn.mfa.yubikey.validator VERIFY 设备验证策略:VERIFY(走 YubiCloud 校验)、SKIP(接受一切设备)、REJECT(拒绝一切设备)
cas.authn.mfa.yubikey.multiple-device-registration-enabled false 是否允许单用户注册/切换多台设备
cas.authn.mfa.yubikey.trusted-device-enabled false 是否支持受信任设备免二次认证
cas.authn.mfa.yubikey.json.location 等 无 切换到 JSON 注册表的入口,优先级高于 allowed-devices

validator 的三种取值对应三个实现类,装配代码位于 YubiKeyAuthenticationEventExecutionPlanConfiguration.java:SKIP → AcceptAllYubiKeyAccountValidator,REJECT → DenyAllYubiKeyAccountValidator,VERIFY → DefaultYubiKeyAccountValidator(yubicoClient)。在 Permissive 模式下,若将 validator 设为 SKIP,意味着只要用户名在 allowed-devices 映射中、且设备 public id 匹配,即可跳过 YubiCloud 在线校验——适合离线演示或内部测试环境,生产环境务必保持默认的 VERIFY。

另外注意:认证处理器只有在 clientId > 0 且 secretKey 非空时才会被注册进认证执行计划(L244-L251),因此即便 allowed-devices 已配置,缺失这两个必填项也会导致 YubiKey MFA 处理器不生效。

五、适用边界与注意事项

结合源码可以总结出 Permissive 注册的适用前提与限制:

  1. 内存态,不可持久化:PermissiveYubiKeyAccountRegistry 的增删改全部落在进程内 Map 上,重启即恢复为配置声明的原始状态,因此它适合「配置即清单」的静态管理,不适合用户自助注册、频繁改绑设备的场景——后者应选择 JSON(可热加载)、JPA、MongoDB、Redis、DynamoDB 或 REST 注册表;
  2. 配置优先级明确:只要配置了 json.location 或 rest.url,allowed-devices 就会被忽略(见装配分支顺序);若三者皆无,CAS 会退化为 OpenYubiKeyAccountRegistry 并对所有凭据放行,并打印警告日志提醒补充注册表实现;
  3. 单用户多设备:属性类型为 Map<String, String>,每个用户名只能对应一个设备 public id;若需要一用户多设备,应结合 multiple-device-registration-enabled 并改用可动态写入的注册表;
  4. 凭据安全:allowed-devices 中的设备 id 属于公开标识符(YubiKey OTP 的前缀段),并非密钥,可安全写入配置文件;真正的密钥保护依赖 YubiCloud 校验(VERIFY 模式)与 secret-key 的妥善保管。

六、总结

Permissive YubiKey Registration 是 Apereo CAS 提供的最简 MFA 设备授权方案:一段 cas.authn.mfa.yubikey.allowed-devices 静态映射即可完成设备白名单声明,配合必填的 client-id / secret-key 与默认 VERIFY 的 validator,即可形成「静态白名单 + YubiCloud 在线校验」的双层防护。从 自动装配逻辑 到 注册表实现,再到 认证处理器,仓库源码完整印证了文档所述「注册记录以 map 形式静态指定」的设计,也划清了它与 JSON / REST / 数据库注册表的边界:追求零依赖与声明式管理选 Permissive,追求动态自助注册则选持久化注册表。

登录后查看全文
cas