0Pricing
OAuth2 & OpenID Connect Deep Dive · 课时

访问令牌、刷新令牌与作用域

了解 OAuth2 签发哪些令牌、刷新令牌如何维持会话,以及作用域如何限制令牌的权限。

访问令牌、刷新令牌与作用域 是 CoddyKit 上的免费 OAuth2 & OpenID Connect Deep Dive 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 OAuth2 & OpenID Connect Deep Dive 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 OAuth2 & OpenID Connect Deep Dive 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Tokens Are the Currency of OAuth2

After a successful flow, OAuth2 hands the client a token instead of the user's credentials. The client presents this token to the resource server to access protected data.

The Access Token

An access token is a short-lived credential proving the client may call an API on the user's behalf. It is sent on each request, usually in the Authorization header.

GET /api/profile
Authorization: Bearer eyJhbGciOi...

Why Tokens Expire

Access tokens are deliberately short-lived (minutes to an hour). If one leaks, the window of misuse is small. But forcing the user to re-login constantly would be painful.

The Refresh Token

A refresh token is a longer-lived credential the client uses to obtain new access tokens without bothering the user. It is stored securely and never sent to APIs.

Refreshing in Practice

When the access token expires, the client posts the refresh token to the token endpoint and receives a fresh access token.

POST /oauth/token
grant_type=refresh_token
&refresh_token=def502...
&client_id=my-app

What Scopes Are

Scopes declare exactly what a token may do, such as read:profile or write:orders. They implement least privilege at the token level.

scope=read:profile write:orders

Requesting Scopes

The client requests scopes during authorization. The user consents, and the issued token is limited to the granted scopes, no more.

GET /authorize?response_type=code
  &client_id=my-app
  &scope=read:profile
  &redirect_uri=https://app/cb

Enforcing Scopes

The resource server checks that the token carries the scope an endpoint requires. A token with only read:profile is rejected from a write endpoint.

if (!token.getScopes().contains("write:orders")) {
    return forbidden();
}

Reference vs Self-Contained

An access token may be a random reference the server looks up, or a self-contained JWT carrying claims the server validates by signature without a lookup.

Revoking Tokens

Refresh tokens can be revoked to end a session, for example on logout or suspected compromise. This invalidates the ability to mint new access tokens.

POST /oauth/revoke
token=def502...
&token_type_hint=refresh_token

Token Storage Matters

Store tokens carefully: never in plain localStorage for sensitive apps, keep refresh tokens server-side or in secure storage, and always use HTTPS in transit.

Quick Check

An access token has expired but you do not want the user to log in again. What does the client use to get a new one?

Recap

You learned how OAuth2 tokens work:

  • Access tokens are short-lived and sent to APIs
  • Refresh tokens silently obtain new access tokens
  • Scopes enforce least privilege per token
  • Tokens can be reference-based or JWTs
  • Revocation and secure storage protect sessions

These concepts underpin every OAuth2 grant type you will study next.

常见问题解答

「访问令牌、刷新令牌与作用域」课时是免费的吗?

是的 — 「访问令牌、刷新令牌与作用域」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 OAuth2 & OpenID Connect Deep Dive 课程的其余内容,请升级到 CoddyKit PRO。 OAuth2 & OpenID Connect Deep Dive 课程共包含 4 节课。

「访问令牌、刷新令牌与作用域」这节课中我会学到什么?

了解 OAuth2 签发哪些令牌、刷新令牌如何维持会话,以及作用域如何限制令牌的权限。 你通过在浏览器中直接运行的动手代码来练习 OAuth2 & OpenID Connect Deep Dive,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 OAuth2 & OpenID Connect Deep Dive 需要有经验吗?

无需任何先前经验。CoddyKit 上的 OAuth2 & OpenID Connect Deep Dive 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「访问令牌、刷新令牌与作用域」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 OAuth2 & OpenID Connect Deep Dive 课中编写并运行代码吗?

能。每节 OAuth2 & OpenID Connect Deep Dive 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. OAuth2:委托协议
  2. OAuth2 角色与术语
  3. 核心授权类型概览
  4. 访问令牌、刷新令牌与作用域
← 返回 OAuth2 & OpenID Connect Deep Dive