首頁 > 後端開發 > php教程 > 記錄Laravel中的API身份驗證

記錄Laravel中的API身份驗證

Emily Anne Brown
發布: 2025-03-05 15:45:15
原創
576 人瀏覽過

>本教程展示了使用現代API文檔工具Scramble的常見Laravel API身份驗證方法及其文檔。 CRAMBLE完全支持OpenAPI 3.1.0安全規範,啟用全面的身份驗證方法文檔。

聖經身份驗證

一種流行的Laravel身份驗證方法標題發送了一個攜帶者令牌:。 使用服務提供商的Authorization方法:Authorization: Bearer *** boot
use Dedoc\Scramble\Scramble;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\SecurityScheme;

public function boot()
{
    Scramble::configure()
        ->withDocumentTransformers(function (OpenApi $openApi) {
            $openApi->secure(
                SecurityScheme::http('bearer')
            );
        });
}
登入後複製
登入後複製

Documenting API authentication in Laravel with Scramble>這為所有端點設置了一個默認的載體令牌安全方案。 如果正確配置了Cramble的文檔路由,則狀態身份驗證將在“嘗試”功能中自動運行。

>

>護照身份驗證

>對於OAuth2身份驗證,OpenAPI使用了

>安全方案。 爭奪允許精確配置:

oauth2

範圍可以在全球或粒狀對照中定義。 雖然stoplight Elements(Scramble的默認UI)顯示
use Dedoc\Scramble\Scramble;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\SecurityScheme;
use Dedoc\Scramble\Support\Generator\SecuritySchemes\OAuthFlow;

public function boot()
{
    Scramble::configure()
        ->withDocumentTransformers(function (OpenApi $openApi) {
            $openApi->secure(
                SecurityScheme::oauth2()
                    ->flow('authorizationCode', function (OAuthFlow $flow) {
                        $flow
                            ->authorizationUrl(config('app.url').'/oauth/authorize')
                            ->tokenUrl(config('app.url').'/oauth/token')
                            ->addScope('*', 'all');
                    })
            );
        });
}
登入後複製
要求,但其他UIS(標量,搖動)有助於從文檔中直接獲取。

oauth2

記錄Sanctum的Documenting API authentication in Laravel with Scramble端點

>記錄Sanctum's oauth/token端點是可以通過社區基礎的爭奪擴展而實現的:>。 切記將其包含在Cramble的可檔案路線中:

oauth/token

自定義身份驗證
Scramble::configure()
    ->routes(function (Route $r) {
        return Str::startsWith($r->uri, 'api/') || $r->uri === 'oauth/token';
    });
登入後複製

多個標頭身份驗證Documenting API authentication in Laravel with Scramble

>對於需要多個標頭的身份驗證,請在API安全要求中指定它們:

這同時授權

標題。

> 在查詢參數中
use Dedoc\Scramble\Scramble;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\SecurityScheme;

public function boot()
{
    Scramble::configure()
        ->withDocumentTransformers(function (OpenApi $openApi) {
            $openApi->components->securitySchemes['tenant'] = SecurityScheme::apiKey('header', 'X-Tenant');
            $openApi->components->securitySchemes['bearer'] = SecurityScheme::http('bearer');

            $openApi->security[] = new SecurityRequirement([
                'tenant' => [],
                'bearer' => [],
            ]);
        });
}
登入後複製
api令牌

X-Tenant如果API令牌是一個查詢參數,請使用:> Authorization

>有關更多安全方案詳細信息,請參閱Scramble的文檔:。 排除身份驗證的路線

phpdoc註釋不包括身份驗證的路由。 操作變壓器提供了更大的靈活性。 例如,豁免缺少

中間件的路由:
use Dedoc\Scramble\Scramble;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\SecurityScheme;

public function boot()
{
    Scramble::configure()
        ->withDocumentTransformers(function (OpenApi $openApi) {
            $openApi->secure(
                SecurityScheme::apiKey('query', 'api_token')
            );
        });
}
登入後複製
use Dedoc\Scramble\Scramble;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\SecurityScheme;

public function boot()
{
    Scramble::configure()
        ->withDocumentTransformers(function (OpenApi $openApi) {
            $openApi->secure(
                SecurityScheme::http('bearer')
            );
        });
}
登入後複製
登入後複製

這將自動指定沒有身份驗證中間件的路由為公共。 這種方法還允許將特定的安全要求分配給操作。

>

結論

> OpenAPI和CRAMBLE提供了可靠的解決方案,用於記錄各種API身份驗證方法,包括基於中間件的自動處理,最大程度地減少手動註釋。 在> https://www.php.cn/link/0fcbc3c3c0cf262c771001930af2406bbc

中,探索爭奪爭奪戰。

以上是記錄Laravel中的API身份驗證的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板