OAuth 2.0 安全最佳实践 (RFC 9700) password 授权类型已经不推荐使用了,将在计划中移除
在 OAuth 2.0 安全最佳实践 (RFC 9700) 中,已经不推荐使用 password 授权类型(资源所有者密码凭证授权)。根据你的代码注释,这个类型已经被标记为 @Deprecated,并计划在未来的版本中移除。
替代方案取决于你的具体使用场景:
对于 Web 应用:应该使用 AUTHORIZATION_CODE(授权码流程) + PKCE
AuthorizationGrantType.AUTHORIZATION_CODE
对于单页应用(SPA)或移动应用:同样推荐使用授权码流程 + PKCE
对于服务间通信:可以使用 CLIENT_CREDENTIALS
AuthorizationGrantType.CLIENT_CREDENTIALS
对于设备授权:可以使用 DEVICE_CODE
AuthorizationGrantType.DEVICE_CODE
对于需要交换令牌的场景:可以使用 TOKEN_EXCHANGE
AuthorizationGrantType.TOKEN_EXCHANGE
选择哪种替代方案取决于你的具体应用场景和安全需求。授权码流程 (AUTHORIZATION_CODE) 是目前最推荐的方式,因为它提供了更好的安全特性,包括防止中间人攻击和令牌泄漏。
OAuth 2.0 官方规范
RFC 6749 - OAuth 2.0 核心框架
官方文档: https://datatracker.ietf.org/doc/html/rfc6749
授权类型定义(Section 1.3):
authorization_code(授权码模式)
implicit(隐式模式,已不推荐)
password(密码模式,已不推荐)
client_credentials(客户端凭证模式)
refresh_token(刷新令牌)
RFC 8252 - OAuth 2.0 for Native Apps(移动端 & SPA 最佳实践)
官方文档: https://datatracker.ietf.org/doc/html/rfc8252
推荐使用 authorization_code + PKCE(Proof Key for Code Exchange)替代 password 和 implicit 模式。
RFC 9068 - JWT Profile for OAuth 2.0 Access Tokens
官方文档: https://datatracker.ietf.org/doc/html/rfc9068
JWT Bearer Token Flow(urn:ietf:params:oauth:grant-type:jwt-bearer)适用于某些企业级身份验证场景。
RFC 8628 - Device Authorization Grant(设备授权模式)
官方文档: https://datatracker.ietf.org/doc/html/rfc8628
适用于 IoT、智能电视等无浏览器设备,使用 urn:ietf:params:oauth:grant-type:device_code。
RFC 8693 - Token Exchange(令牌交换)
官方文档: https://datatracker.ietf.org/doc/html/rfc8693
适用于跨服务令牌交换,使用 urn:ietf:params:oauth:grant-type:token-exchange。
OAuth 2.0 安全最佳实践(BCP)
RFC 6819 - OAuth 2.0 Threat Model and Security Considerations
官方文档: https://datatracker.ietf.org/doc/html/rfc6819
安全威胁:password 模式容易受到凭据泄露攻击。
RFC 9700 - OAuth 2.0 Security Best Current Practice (BCP)
官方文档: https://datatracker.ietf.org/doc/html/rfc9700
明确反对 password 模式(Section 2.4):
“The Resource Owner Password Credentials grant (password grant) MUST NOT be used.”
推荐替代方案:
Web/SPA/Mobile: authorization_code + PKCE
Service-to-Service: client_credentials
Devices (IoT/TV): device_code
Token Exchange: token-exchange
Spring Security OAuth 2.0 文档
Spring Authorization Server 文档
官方文档: https://docs.spring.io/spring-authorization-server/docs/current/reference/html/
支持的授权类型:
authorization_code(推荐)
client_credentials(服务间通信)
refresh_token(令牌刷新)
urn:ietf:params:oauth:grant-type:device_code(设备授权)
urn:ietf:params:oauth:grant-type:token-exchange(令牌交换)
已弃用:
password(Spring Security 5.8+ 标记为 @Deprecated)
Spring Security OAuth 2.0 迁移指南
官方文档: https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter
推荐使用 OAuth2AuthorizedClientManager 替代旧式 PasswordGrant 方式。
替代 password 授权的方案
场景 | 推荐授权类型 | 适用情况 |
---|---|---|
Web 应用 | authorization_code + PKCE | 用户登录,最安全的方式 |
单页应用 (SPA) | authorization_code + PKCE | 防止令牌泄露 |
移动应用 | authorization_code + PKCE | 避免存储密码 |
服务间通信 | client_credentials | 机器对机器(M2M) |
IoT/智能设备 | device_code | 无浏览器设备 |
令牌交换 | token-exchange | 跨服务身份委托 |
说明:
authorization_code + PKCE
适用于所有前端应用(Web/SPA/移动端),取代 password 授权。
PKCE(Proof Key for Code Exchange)增强安全性,防止授权码拦截攻击。
client_credentials
适用于后端服务之间的认证(如微服务调用)。
device_code
适用于智能电视、IoT 设备等无法直接输入密码的场景。
token-exchange
用于在不同服务之间交换令牌(如将 Google Token 换成内部系统的 Token)。