# 检测到OAuth2令牌重放：在短时间内从两个不同的IP地址使用了相同的访问令牌

- **ID:** `security/oauth2-token-replay-detected`
- **领域:** security
- **类别:** auth_error
- **错误码:** `OAUTH2_TOKEN_REPLAY`
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

攻击者窃取了有效的访问令牌并从不同的IP地址使用它，或者合法用户的令牌因代理或VPN配置错误而从多个位置使用。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Keycloak 22.0.0 | active | — | — |
| Okta 16.0.0 | active | — | — |
| Auth0 3.0.0 | active | — | — |
| Spring Security OAuth2 6.2.0 | active | — | — |

## 解决方案

1. ```
   通过将令牌绑定到客户端的TLS会话或设备指纹来实现令牌绑定。例如，使用带有证书指纹的JWT 'cnf'声明。
   ```
2. ```
   使用短生命周期的访问令牌（例如5分钟）和带轮换的刷新令牌，以最小化重放攻击的窗口。相应地配置身份提供者。
   ```
3. ```
   监控并告警可疑的令牌使用模式，例如IP快速变化，并自动撤销令牌并要求重新认证。
   ```

## 无效尝试

- **** — Blocking the IP address of the second request is ineffective because attackers often use rotating IP addresses or proxies, and it may also block legitimate users behind NAT. (50% 失败率)
- **** — Increasing the time window for replay detection may reduce false positives but also gives attackers more time to use the stolen token, increasing the risk. (60% 失败率)
- **** — Disabling replay detection entirely removes the security control, leaving the system vulnerable to token theft and replay attacks. (90% 失败率)
