MJ7*_*MJ7 6 php laravel laravel-passport
我使用Laravel护照进行身份验证
在路线api.php中
Route::get('/todos', function(){
return 'hello';
})->middleware('auth:api');
Run Code Online (Sandbox Code Playgroud)
但是当打开localhost:8000 / api / todos时,我看到以下错误
InvalidArgumentException
Route [login] not defined.
* @return string
*
* @throws \InvalidArgumentException
*/
public function route($name, $parameters = [], $absolute = true)
{
if (! is_null($route = $this->routes->getByName($name))) {
return $this->toRoute($route, $parameters, $absolute);
}
throw new InvalidArgumentException("Route [{$name}] not defined.");
}
/**
* Get the URL for a given route instance.
*
* @param \Illuminate\Routing\Route $route
* @param mixed $parameters
* @param bool $absolute
* @return string
*
* @throws \Illuminate\Routing\Exceptions\UrlGenerationException
*/
protected function toRoute($route, $parameters, $absolute)
{
return $this->routeUrl()->to(
$route, $this->formatParameters($p
Run Code Online (Sandbox Code Playgroud)
如果用户未通过身份验证,我不要重定向到任何页面,而只能看到该页面
小智 17
使用Postman并设置Header Accept:application / json,否则Laravel Passport将永远不知道它是API客户端,因此重定向到Web的/ login页面。
你是直接在浏览器搜索栏中输入上述网址吗?如果你做错了,因为你还需要在你的请求中输入 API 令牌__!!
检查任一请求是否包含令牌或不制作自己的中间件。
创建中间件的命令
php artisan make:middleware CheckApiToken
Run Code Online (Sandbox Code Playgroud)
https://laravel.com/docs/5.6/middleware
将中间件句柄方法更改为
public function handle($request, Closure $next)
{
if(!empty(trim($request->input('api_token')))){
$is_exists = User::where('id' , Auth::guard('api')->id())->exists();
if($is_exists){
return $next($request);
}
}
return response()->json('Invalid Token', 401);
}
Run Code Online (Sandbox Code Playgroud)
这样你的网址应该是这样的
http://localhost:8000/api/todos?api_token=API_TOKEN_HERE
| 归档时间: |
|
| 查看次数: |
13514 次 |
| 最近记录: |