如何基于 WebAuthn 扩展 Spring Security 登录流程?
解读
在国内金融、政务、电商等高安全场景,“无密码登录” 已成为合规刚需。WebAuthn(FIDO2)作为国密算法之外的国际通行标准,已被中国信通院纳入《可信身份白皮书》。Spring Security 5.7+ 仅提供基础 OAuth2/OIDC 集成,对 WebAuthn 零内置支持。面试官想验证候选人能否在不破坏原有 UsernamePasswordAuthenticationFilter 链路的前提下,把 WebAuthn 做成可插拔、国密可替换、双因子可降级的扩展,同时兼顾国密 SM2/SM3 的合规改造空间。
知识点
- WebAuthn 双阶段协议:注册(attestation)与认证(assertion)必须分别映射到 Spring Security 的
Authentication对象。 - Spring Security 5.8 过滤器架构:
SecurityFilterChain采用 DSL 配置,自定义过滤器插入位置需精确到UsernamePasswordAuthenticationFilter之前或之后。 - RelyingParty 领域模型:
com.yubico.webauthn.RelyingParty需与 Spring 的UserDetailsService解耦,避免直接依赖 Yubico 实现,方便后续国密替换。 - Session 与缓存一致性:国内多数系统仍用** Redis 共享会话**,
PublicKeyCredentialCreationOptions与PublicKeyCredentialRequestOptions的临时随机数(challenge)必须一次性、防重放,需通过Spring Session或自定义ChallengeRepository实现。 - 双因子降级策略:当用户丢失 authenticator 时,需回退到短信/邮件/国密 USBKey,这要求
AuthenticationProvider支持多因子链式认证,并在SecurityContext中标记AUTHENTICATION_LEVEL供下游业务做细粒度授权。 - 国密改造点:若客户要求SM2 签名算法,需重写
COSEAlgorithmIdentifier与AttestationStatement验证逻辑,保持与 Spring Security 的 Algorithm 协商接口隔离,以便通过国密验收。
答案
步骤一:领域建模
定义 WebAuthnUserDetails 继承 UserDetails,新增 CredentialRecord 列表(credentialId、publicKeyCose、counter、transports)。WebAuthnAuthenticationToken 继承 AbstractAuthenticationToken,封装 AuthenticatorAssertionResponse。
步骤二:注册阶段
- 暴露
/webauthn/register/start端点,使用RelyingParty.startRegistration(...)生成PublicKeyCredentialCreationOptions,把 challenge 写入 Redis 并绑定当前 sessionId,TTL 120 s。 - 前端回调后,调用
/webauthn/register/finish,自定义WebAuthnRegistrationFilter继承OncePerRequestFilter,在successfulAuthentication()里把CredentialRecord持久化到国密数据库加密字段,并发布WebAuthnCredentialCreatedEvent,供审计系统监听。
步骤三:认证阶段
- 暴露
/webauthn/login/start,返回PublicKeyCredentialRequestOptions,同样Redis 缓存 challenge。 - 创建
WebAuthnAuthenticationFilter,插入到UsernamePasswordAuthenticationFilter之前,order = -120。 - 在
attemptAuthentication()内构造WebAuthnAuthenticationToken,交由自定义WebAuthnAuthenticationProvider验证:- 使用
RelyingParty.finishAssertion(...)验证签名; - 检查
signatureCounter > dbCounter,防克隆; - 验证通过后生成
Jwt访问令牌,在 claim 中加入 auth_type=webauthn,供资源服务器做细粒度权限控制。
- 使用
步骤四:双因子与降级
在 WebSecurityConfigurer 中配置
.and().formLogin().loginPage("/login").permitAll()
同时注册 DaoAuthenticationProvider 与 WebAuthnAuthenticationProvider 到 ProviderManager,用 AuthenticationTrustResolver 判断当前是否已满足二级认证。若用户选择“短信找回”,则通过 SwitchUserFilter 临时提升权限,重新绑定新 authenticator。
步骤五:国密合规改造
定义 GmRelyingParty 实现,把 COSEAlgorithmIdentifier.ES256 映射到 SM2,把 SHA256 替换为 SM3,保持与 Yubico API 同签名,仅替换底层 Crypto 实现类。通过 Spring Profiles sm-crypto 切换,零侵入业务代码。
步骤六:测试与压测
使用 Testcontainers 启动 Redis + MySQL 环境,编写 @WebMvcTest 验证 /webauthn/register/finish 的防重放逻辑;用 JMH 压测 单节点 2000 QPS 场景下 Redis challenge 命中率 > 99%,GC 停顿 < 20 ms。
拓展思考
- 跨域与小程序场景:国内主流小程序不支持 WebAuthn,需通过原生桥接+自定义 scheme 把断言结果回传 H5,此时要额外校验
origin字段与微信域名校验文件,防止中间人劫持。 - 隐私合规:WebAuthn 的
credentialId属于可识别个人信息,需遵循《个人信息保护法》第 38 条,加密存储于境内服务器,跨境传输需通过安全评估。 - 零信任架构:在 Spring Cloud Gateway 层,可结合
WebAuthn + mTLS + SPIFFE做工作负载身份三因子,把auth_type=webauthn作为动态授权标签,通过 Open Policy Agent 实时决策。 - 国密双证书:若客户要求双证书模式(加密证书+签名证书),需把 WebAuthn 的
publicKey拆分为 SM2 加密公钥与验证公钥,在 Spring Security 的 PreAuthenticatedAuthenticationToken 中分别存储,供下游国密网关调用。