OAuth 2.0 认证流程详解

A
admin1
# #

OAuth 2.0 认证流程

第三方登录的标准协议。

授权码模式

  1. 用户跳转到授权服务器
  2. 用户同意授权
  3. 获取授权码
  4. 用授权码换取 access token

代码示例

// 1. 跳转授权
window.location.href = `https://auth.example.com/oauth/authorize?client_id=xxx&redirect_uri=xxx&response_type=code`;

// 2. 交换 token
const res = await fetch('/oauth/token', {
  method: 'POST',
  body: new URLSearchParams({ code, client_secret })
});

安全注意事项

  • 使用 PKCE
  • 验证 state 参数
  • 短有效期 token

你用过哪些 OAuth 提供商?

已有 0 人投票

评论 (2)

加载中...