使用 Web 路由登录,但无法使用 api 路由进行身份验证



重复:在所有 api 路由中使用 Web 身份验证的 Laravel 重定向到主页

我不知道在谷歌上搜索它的关键字,所以很抱歉一些愚蠢的事情。我使用 Web 路由加载视图和 api 路由来加载 json 数据,如下所示:

  • /shop/categories
  • /api/shop/categories

我使用 Web 路由登录,身份验证中间件检查没问题,我可以通过 auth((->user((->name 获取用户名。但是当我用/api/shop/category 调用 jquery ajax 时,我收到一个错误"已验证",我尝试添加中间件 auth:api,但没有任何变化。我阅读了文档,我看到我必须包含api_token网址。

使用 Web 路由、中间件检查登录时如何使用 api?

这是我的一些代码:

//web route
Route::group(['prefix' => $prefix, 'middleware' => ['auth']], function() {
Route::get('categories', 'CategoryController@getView')->name('category_get_view');
});
// api route
Route::group(['prefix' => $prefix, 'middleware' => ['auth:api']], function() {
Route::get('categories', 'CategoryController@getJson');
Route::get('categories/{category}', 'CategoryController@getDetail');
Route::put('categories/{category}', 'CategoryController@putSave');
Route::post('categories', 'CategoryController@postSave');
Route::delete('categories/{category}', 'CategoryController@deleteSave');
});
// dd($request)
Request {#42
#json: null
#convertedFiles: null
#userResolver: Closure {#399
class: "IlluminateAuthAuthServiceProvider"
this: AuthServiceProvider {#32 …}
parameters: {
$guard: {
default: null
}
}
use: {
$app: Application {#2 …}
}
file: "vendorlaravelframeworksrcIlluminateAuthAuthServiceProvider.php"
line: "85 to 87"
}
#routeResolver: Closure {#412
class: "IlluminateRoutingRouter"
this: Router {#25 …}
use: {
$route: Route {#181 …}
}
file: "vendorlaravelframeworksrcIlluminateRoutingRouter.php"
line: "649 to 651"
}
+attributes: ParameterBag {#44
#parameters: []
}
+request: ParameterBag {#50
#parameters: array:2 [
"key" => null
"page" => "1"
]
}
+query: ParameterBag {#50}
+server: ServerBag {#46
#parameters: array:40 [
"REDIRECT_STATUS" => "200"
"HTTP_HOST" => "backend.local"
"HTTP_CONNECTION" => "keep-alive"
"HTTP_ACCEPT" => "*/*"
"HTTP_X_CSRF_TOKEN" => "BNlGYoppmtl7fljKeQ4TGdNmbGoiuKWTeoLDbKln"
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"
"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"
"HTTP_REFERER" => "http://backend.local/shop/categories"
"HTTP_ACCEPT_ENCODING" => "gzip, deflate"
"HTTP_ACCEPT_LANGUAGE" => "en-US,en;q=0.9,ja;q=0.8"
"HTTP_COOKIE" => "XSRF-TOKEN=eyJpdiI6IjNcL0t5aEMxZHNBM2wrNkRvc0RCUUlBPT0iLCJ2YWx1ZSI6InlaTUlLdSt4UXVmdktNZk81ZzNpMWFNQUVqalZZTmNsRmpcLzZiQkkrdTRZZEphODVPYU5qYXdiSkRVMUUra0hJIiwibWFjIjoiMTdlNjUzYmVkYWRiM2JiNDNlMWYzMjQ1MmM0ZmIxMjQ0YzczOWE4NjQ2N2UzNzZmOTkxOGYxYTJjOTE5MmU0NiJ9; laravel_session=eyJpdiI6InpLOUw5SHR6S0RWU21lSEhZMWVEZHc9PSIsInZhbHVlIjoiN1hNZUlNWUJacm83bEdFd3pCR0R1R1d1UVZZdmRcL3g3aXQ3QlhQOHNZZXFJUGVqOFBXS2dKZVFvYjl3NGZoRUsiLCJtYWMiOiI1ZjNhZmFjM2Q1ZjlmYzUwZmQ3ODA0MTJkZGNiMzdlZDhjYTljODViMTY0MTE5ZDA0YTg0ZTJkY2EwYjJmOWJmIn0%3D"
"PATH" => ""
"SystemRoot" => "C:WINDOWS"
"COMSPEC" => "C:WINDOWSsystem32cmd.exe"
"PATHEXT" => ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
"WINDIR" => "C:WINDOWS"
"SERVER_SIGNATURE" => "<address>Apache/2.4.39 (Win64) PHP/7.2.18 Server at backend.local Port 80</address>n"
"SERVER_SOFTWARE" => "Apache/2.4.39 (Win64) PHP/7.2.18"
"SERVER_NAME" => "backend.local"
"SERVER_ADDR" => "::1"
"SERVER_PORT" => "80"
"REMOTE_ADDR" => "::1"
"DOCUMENT_ROOT" => "/public"
"REQUEST_SCHEME" => "http"
"CONTEXT_PREFIX" => ""
"CONTEXT_DOCUMENT_ROOT" => "/public"
"SERVER_ADMIN" => "wampserver@wampserver.invalid"
"SCRIPT_FILENAME" => "/public/index.php"
"REMOTE_PORT" => "64525"
"REDIRECT_URL" => "/api/shop/categories"
"REDIRECT_QUERY_STRING" => "key=&page=1"
"GATEWAY_INTERFACE" => "CGI/1.1"
"SERVER_PROTOCOL" => "HTTP/1.1"
"REQUEST_METHOD" => "GET"
"QUERY_STRING" => "key=&page=1"
"REQUEST_URI" => "/api/shop/categories?key=&page=1"
"SCRIPT_NAME" => "/index.php"
"PHP_SELF" => "/index.php"
"REQUEST_TIME_FLOAT" => 1570505297.533
"REQUEST_TIME" => 1570505297
]
}
+files: FileBag {#47
#parameters: []
}
+cookies: ParameterBag {#45
#parameters: array:2 [
"XSRF-TOKEN" => "eyJpdiI6IjNcL0t5aEMxZHNBM2wrNkRvc0RCUUlBPT0iLCJ2YWx1ZSI6InlaTUlLdSt4UXVmdktNZk81ZzNpMWFNQUVqalZZTmNsRmpcLzZiQkkrdTRZZEphODVPYU5qYXdiSkRVMUUra0hJIiwibWFjIjoiMTdlNjUzYmVkYWRiM2JiNDNlMWYzMjQ1MmM0ZmIxMjQ0YzczOWE4NjQ2N2UzNzZmOTkxOGYxYTJjOTE5MmU0NiJ9"
"laravel_session" => "eyJpdiI6InpLOUw5SHR6S0RWU21lSEhZMWVEZHc9PSIsInZhbHVlIjoiN1hNZUlNWUJacm83bEdFd3pCR0R1R1d1UVZZdmRcL3g3aXQ3QlhQOHNZZXFJUGVqOFBXS2dKZVFvYjl3NGZoRUsiLCJtYWMiOiI1ZjNhZmFjM2Q1ZjlmYzUwZmQ3ODA0MTJkZGNiMzdlZDhjYTljODViMTY0MTE5ZDA0YTg0ZTJkY2EwYjJmOWJmIn0="
]
}
+headers: HeaderBag {#48
#headers: array:10 [
"host" => array:1 [
0 => "backend.local"
]
"connection" => array:1 [
0 => "keep-alive"
]
"accept" => array:1 [
0 => "*/*"
]
"x-csrf-token" => array:1 [
0 => "BNlGYoppmtl7fljKeQ4TGdNmbGoiuKWTeoLDbKln"
]
"x-requested-with" => array:1 [
0 => "XMLHttpRequest"
]
"user-agent" => array:1 [
0 => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"
]
"referer" => array:1 [
0 => "http://backend.local/shop/categories"
]
"accept-encoding" => array:1 [
0 => "gzip, deflate"
]
"accept-language" => array:1 [
0 => "en-US,en;q=0.9,ja;q=0.8"
]
"cookie" => array:1 [
0 => "XSRF-TOKEN=eyJpdiI6IjNcL0t5aEMxZHNBM2wrNkRvc0RCUUlBPT0iLCJ2YWx1ZSI6InlaTUlLdSt4UXVmdktNZk81ZzNpMWFNQUVqalZZTmNsRmpcLzZiQkkrdTRZZEphODVPYU5qYXdiSkRVMUUra0hJIiwibWFjIjoiMTdlNjUzYmVkYWRiM2JiNDNlMWYzMjQ1MmM0ZmIxMjQ0YzczOWE4NjQ2N2UzNzZmOTkxOGYxYTJjOTE5MmU0NiJ9; laravel_session=eyJpdiI6InpLOUw5SHR6S0RWU21lSEhZMWVEZHc9PSIsInZhbHVlIjoiN1hNZUlNWUJacm83bEdFd3pCR0R1R1d1UVZZdmRcL3g3aXQ3QlhQOHNZZXFJUGVqOFBXS2dKZVFvYjl3NGZoRUsiLCJtYWMiOiI1ZjNhZmFjM2Q1ZjlmYzUwZmQ3ODA0MTJkZGNiMzdlZDhjYTljODViMTY0MTE5ZDA0YTg0ZTJkY2EwYjJmOWJmIn0%3D"
]
]
#cacheControl: []
}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/api/shop/categories"
#requestUri: "/api/shop/categories?key=&page=1"
#baseUrl: ""
#basePath: null
#method: "GET"
#format: null
#session: null
#locale: null
#defaultLocale: "en"
-isHostValid: true
-isForwardedValid: true
basePath: ""
format: "html"
}

也许您在请求的标头中缺少授权。

如果要从经过身份验证的用户获取数据,但希望从经过身份验证的 API 获取数据。你应该使用auth('guard-name')来获取它

相关内容

  • 没有找到相关文章

最新更新