OAuth
OAuth 2.0 是授权框架,使第三方应用在用户授权下访问其资源,无需共享密码,通过令牌实现。
用途
实现委托授权,使应用能访问用户在其他服务的资源而不获取密码。
机制
流程:客户端向授权服务器请求授权 → 用户授权 → 客户端获授权码 → 换取 access_token → 用 token 访问资源服务器。授权类型:authorization_code、implicit、password、client_credentials、refresh_token。
用例
- 第三方登录(用 Google 登录)
- API 授权访问
- SSO 跨服务
- 移动端授权
示例
- 输入
authorization code → access_token输出Call the API with the tokenThe authorization code flow
常见错误
- The implicit flow exposes the token to the browser
- Use authorization_code with PKCE