Laravel Sanctum 为 SPA (单页面应用程序)、移动应用程序和简单的、基于令牌的 API 提供轻量级身份验证系统。Sanctum 允许应用程序的每个用户为他们的帐户生成多个 API 令牌。这些令牌可能被授予指定允许令牌执行哪些操作的能力 / 范围
安装过程
你可以通过 Composer 安装 Laravel Sanctum
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com$ sudo composer require laravel/sanctum
https://repo.packagist.org could not be fully loaded (curl error 28 while downloading https://repo.packagist.org/packages.json: Failed to connect to repo.packagist.org port 443: Connection timed out), package information was loaded from the local cache and may be out of date
Cannot use laravel/sanctum's latest version v3.2.5 as it requires php ^8.0.2 which is not satisfied by your platform.
./composer.json has been created
Running composer update laravel/sanctum
Loading composer repositories with package information
https://repo.packagist.org could not be fully loaded (curl error 28 while downloading https://repo.packagist.org/p2/illuminate/contracts.json: Failed to connect to repo.packagist.org port 443: Connection timed out), package information was loaded from the local cache and may be out of date
Updating dependencies
Lock file operations: 27 installs, 0 updates, 0 removals
- Locking doctrine/inflector (2.0.8)
- Locking illuminate/collections (v8.83.27)
- Locking illuminate/console (v8.83.27)
- Locking illuminate/container (v8.83.27)
- Locking illuminate/contracts (v8.83.27)
- Locking illuminate/database (v8.83.27)
- Locking illuminate/macroable (v8.83.27)
- Locking illuminate/support (v8.83.27)
- Locking laravel/sanctum (v2.15.1)
- Locking nesbot/carbon (2.71.0)
- Locking psr/clock (1.0.0)
- Locking psr/container (1.1.1)
- Locking psr/simple-cache (1.0.1)
- Locking symfony/console (v5.4.31)
- Locking symfony/deprecation-contracts (v2.5.2)
- Locking symfony/polyfill-ctype (v1.28.0)
- Locking symfony/polyfill-intl-grapheme (v1.28.0)
- Locking symfony/polyfill-intl-normalizer (v1.28.0)
- Locking symfony/polyfill-mbstring (v1.28.0)
- Locking symfony/polyfill-php73 (v1.28.0)
- Locking symfony/polyfill-php80 (v1.28.0)
- Locking symfony/process (v5.4.28)
- Locking symfony/service-contracts (v2.5.2)
- Locking symfony/string (v5.4.31)
- Locking symfony/translation (v5.4.31)
- Locking symfony/translation-contracts (v2.5.2)
- Locking voku/portable-ascii (1.6.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 27 installs, 0 updates, 0 removals
- Downloading illuminate/macroable (v8.83.27)
- Downloading illuminate/contracts (v8.83.27)
- Downloading illuminate/collections (v8.83.27)
- Downloading illuminate/container (v8.83.27)
- Downloading illuminate/support (v8.83.27)
- Downloading illuminate/database (v8.83.27)
- Downloading illuminate/console (v8.83.27)
- Installing doctrine/inflector (2.0.8): Extracting archive
- Installing illuminate/macroable (v8.83.27): Extracting archive
- Installing psr/simple-cache (1.0.1): Extracting archive
- Installing psr/container (1.1.1): Extracting archive
- Installing illuminate/contracts (v8.83.27): Extracting archive
- Installing illuminate/collections (v8.83.27): Extracting archive
- Installing illuminate/container (v8.83.27): Extracting archive
- Installing voku/portable-ascii (1.6.1): Extracting archive
- Installing symfony/translation-contracts (v2.5.2): Extracting archive
- Installing symfony/polyfill-php80 (v1.28.0): Extracting archive
- Installing symfony/polyfill-mbstring (v1.28.0): Extracting archive
- Installing symfony/deprecation-contracts (v2.5.2): Extracting archive
- Installing symfony/translation (v5.4.31): Extracting archive
- Installing psr/clock (1.0.0): Extracting archive
- Installing nesbot/carbon (2.71.0): Extracting archive
- Installing illuminate/support (v8.83.27): Extracting archive
- Installing symfony/polyfill-intl-normalizer (v1.28.0): Extracting archive
- Installing symfony/polyfill-intl-grapheme (v1.28.0): Extracting archive
- Installing symfony/polyfill-ctype (v1.28.0): Extracting archive
- Installing symfony/string (v5.4.31): Extracting archive
- Installing symfony/service-contracts (v2.5.2): Extracting archive
- Installing symfony/polyfill-php73 (v1.28.0): Extracting archive
- Installing symfony/console (v5.4.31): Extracting archive
- Installing illuminate/database (v8.83.27): Extracting archive
- Installing symfony/process (v5.4.28): Extracting archive
- Installing illuminate/console (v8.83.27): Extracting archive
- Installing laravel/sanctum (v2.15.1): Extracting archive
23 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
16 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found
Using version ^2.15 for laravel/sanctum
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com$
发布配置
接下来,你需要使用 vendor:publish
Artisan 命令发布 Sanctum 的配置和迁移文件。Sanctum 的配置文件将会保存在 config
文件夹中,在做这个操作的时候需要链接数据库,如果不链接数据库则报下面错误。
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$ php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
ErrorException
file_put_contents(/www/wwwroot/laravel7.yshuq.com/laravel7/database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php): failed to open stream: Permission denied
at vendor/league/flysystem/src/Adapter/Local.php:129
125| {
126| $location = $this->applyPathPrefix($path);
127| $this->ensureDirectory(dirname($location));
128|
> 129| if (($size = file_put_contents($location, $contents, $this->writeFlags)) === false) {
130| return false;
131| }
132|
133| $type = 'file';
+24 vendor frames
25 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
如果数据库链接正常,则显示
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$ sudo php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
Copied Directory [/vendor/laravel/sanctum/database/migrations] To [/database/migrations]
Copied File [/vendor/laravel/sanctum/config/sanctum.php] To [/config/sanctum.php]
Publishing complete.
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$
迁移文件
需要执行数据库迁移文件。Sanctum 将创建一个数据库表用于存储 API 令牌:
php artisan migrate
完整代码流程
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$ sudo php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table (0.11 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated: 2019_08_19_000000_create_failed_jobs_table (0.04 seconds)
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated: 2019_12_14_000001_create_personal_access_tokens_table (0.1 seconds)
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$
假如你需要使用 Sanctum 来验证 SPA,你需要在 app/Http/Kernel.php
文件中引入
use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;
EnsureFrontendRequestsAreStateful::class,
