嗨,我正在使用 laravel sanctum(laravel 9),我想使用令牌來訪問需要身份驗證的路由組。然而,我使用以 laravel 作為后端框架和獨立客戶端的分布式應用程式架構。我想訪問以下路由組:
Route::group(['middleware' => ['auth:sanctum']], function() {
Route::get('/templates', [TemplateController::class, 'index']);
});
在客戶端,我使用 axios 發送 http 請求。客戶端已經擁有一個令牌,我嘗試使用 get 請求發送它:
import axios from "axios"
const basePath = "http://localhost:8000/api/"
const token = localStorage.getItem('test_token')
const headers = {headers :{ Authorization: `Bearer ${token}` }};
axios.get(basePath 'templates',headers)
.then(resp => {console.log(resp)})
這種方法基于以下帖子:如何使用 axios 發送授權標頭
然而,這導致了狀態 401,并帶有以下默認未經授權的錯誤:
Failed to load resource: the server responded with a status of 401 (Unauthorized)
有誰知道如何將令牌發送到 laravel sanctum 以便中間件可以檢測到它?提前致謝。
uj5u.com熱心網友回復:
嗨,我找到了答案,我需要做的就是在標題中添加一個接受屬性,如下所示:
const headers = {headers :
{ Authorization: `Bearer ${token}`,
Accept :'application/json',
}
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/460637.html
標籤:javascript 拉拉维尔 验证 axios laravel-sanctum
下一篇:如何在django中更新用戶密碼
