laravel sanctum 只生成 access_token 吗?
Does laravel sanctum only generate access_token?
我正在尝试了解 SPA(vuejs) 应用程序中 laravel sanctum
的基本流程。到目前为止我的理解是:
#它为API认证创建一个中间件
#当用户尝试登录时,它会向前端生成 access_tokens
和 returns。前端然后 remembers
这个令牌编号使用它的 frontend storages
像 localStorage()
, sessionStorage()
等
#当用户尝试 logout
时,我们只是 delete
来自 frontend storage
和 database table
的 access_tokens
如有遗漏或错误,请指正。
Sanctum 不仅使用令牌进行身份验证,而且还进行常规 session 身份验证。身份验证方法根据请求自动确定,如果来自同一域,则使用 cookie/session 进行身份验证,如果请求来自不同域或未找到 session cookie,则使用 tokens 身份验证尝试过。
通常对于同一域中的 SPA 应用程序不需要使用令牌。 Sanctum 将仅使用使用 cookie 的标准身份验证。
https://laravel.com/docs/9.x/sanctum#how-it-works-spa-authentication
For this feature, Sanctum does not use tokens of any kind. Instead, Sanctum uses Laravel's built-in cookie based session authentication services.
如果您的前端位于不同的域或移动设备上,sanctum 将检查请求的 headers 中的令牌。
要使用令牌身份验证,您需要按照此处的说明手动颁发令牌:https://laravel.com/docs/9.x/sanctum#issuing-mobile-api-tokens
Typically, you will make a request to the token endpoint from your mobile application's "login" screen. The endpoint will return the plain-text API token which may then be stored on the mobile device and used to make additional API requests
return $user->createToken($request->device_name)->plainTextToken
登录后,将令牌存储在 localStorage 或本地 session,您需要将此令牌包含在授权中 headers 用于移动或第三方前端。
When the mobile application uses the token to make an API request to your application, it should pass the token in the Authorization header as a Bearer token.
基本上,如果您的 SPA 在同一个域中,则无需关心令牌。您可以继续使用 cookie 和 session.
如果您的前端是移动的或在不同的域上,首选方法是使用令牌,您需要手动调用登录 API 并将令牌响应存储在 localStorage 或本地 session 上。
请注意,如果您不想,实际上您甚至不需要为第三方应用程序使用令牌。您也可以使用 cookie 和 session,但这可能会导致缩放问题,因为 Laravel 会向每个人、访客或经过身份验证的人发出 session cookie,因此您的后端 session 例如redis 第 3 方 apps/APIs 会增长得非常快。但是,对于令牌,它仅在登录时生成它们,因此规模要小得多。
我正在尝试了解 SPA(vuejs) 应用程序中 laravel sanctum
的基本流程。到目前为止我的理解是:
#它为API认证创建一个中间件
#当用户尝试登录时,它会向前端生成 access_tokens
和 returns。前端然后 remembers
这个令牌编号使用它的 frontend storages
像 localStorage()
, sessionStorage()
等
#当用户尝试 logout
时,我们只是 delete
来自 frontend storage
和 database table
access_tokens
如有遗漏或错误,请指正。
Sanctum 不仅使用令牌进行身份验证,而且还进行常规 session 身份验证。身份验证方法根据请求自动确定,如果来自同一域,则使用 cookie/session 进行身份验证,如果请求来自不同域或未找到 session cookie,则使用 tokens 身份验证尝试过。
通常对于同一域中的 SPA 应用程序不需要使用令牌。 Sanctum 将仅使用使用 cookie 的标准身份验证。
https://laravel.com/docs/9.x/sanctum#how-it-works-spa-authentication
For this feature, Sanctum does not use tokens of any kind. Instead, Sanctum uses Laravel's built-in cookie based session authentication services.
如果您的前端位于不同的域或移动设备上,sanctum 将检查请求的 headers 中的令牌。
要使用令牌身份验证,您需要按照此处的说明手动颁发令牌:https://laravel.com/docs/9.x/sanctum#issuing-mobile-api-tokens
Typically, you will make a request to the token endpoint from your mobile application's "login" screen. The endpoint will return the plain-text API token which may then be stored on the mobile device and used to make additional API requests
return $user->createToken($request->device_name)->plainTextToken
登录后,将令牌存储在 localStorage 或本地 session,您需要将此令牌包含在授权中 headers 用于移动或第三方前端。
When the mobile application uses the token to make an API request to your application, it should pass the token in the Authorization header as a Bearer token.
基本上,如果您的 SPA 在同一个域中,则无需关心令牌。您可以继续使用 cookie 和 session.
如果您的前端是移动的或在不同的域上,首选方法是使用令牌,您需要手动调用登录 API 并将令牌响应存储在 localStorage 或本地 session 上。
请注意,如果您不想,实际上您甚至不需要为第三方应用程序使用令牌。您也可以使用 cookie 和 session,但这可能会导致缩放问题,因为 Laravel 会向每个人、访客或经过身份验证的人发出 session cookie,因此您的后端 session 例如redis 第 3 方 apps/APIs 会增长得非常快。但是,对于令牌,它仅在登录时生成它们,因此规模要小得多。