Laravel 5.3 Passport JWT身份验证
在我使用laravel 5.2之前,我使用了第三方软件包 https://github.com /tymondesigns/jwt-auth/用于进行基于JWT的身份验证.我们只需要传递用户名和密码即可获得令牌.
Earlier when I was using laravel 5.2, i used a third party package https://github.com/tymondesigns/jwt-auth/ for making JWT based authentication. Where we just had to pass the username and password to get a token.
现在在laravel 5.3中引入了通行证,我想进行基于JWT的身份验证,但是通行证要求我指定client_id和client_secret以及用户名和密码. tymondesigns/jwt-auth中不存在.
Now in laravel 5.3 with the introduction of passport I want to make a JWT based authentication but passport requires me to specify the client_id and client_secret along with the username and password. which was not there in tymondesigns/jwt-auth.
如果我在没有client_id的情况下发出请求,则会引发错误 http://pix.toile-libre.org/upload/original/1482908288.png ,但是当我通过client_id和client_secret时,它可以正常工作
If I make a request without the client_id then it throws an error http://pix.toile-libre.org/upload/original/1482908288.png but when I pass the client_id and client_secret then it works fine http://pix.toile-libre.org/upload/original/1482908143.png
如何在laravel 5.3和护照中仅使用用户名和密码而不指定client_id和client_secret来发出JWT请求.
How can I make a JWT request in laravel 5.3 and passport with just the username and password and without specifying client_id and client_secret.
所以,最后,我在回答我自己的问题.希望这可以帮助面临类似问题的人.
So, finally I am answering my own question. Hopefully this will help someone facing the similar problem.
JWT身份验证可以使用Laravel 5.3护照来完成,只需执行以下步骤:
JWT authentication can be done using Laravel 5.3 passport, just follow the following steps:
- 按照此链接所述正常安装Passport https://laravel.com/docs/master/护照#安装
或按照以下步骤操作:
- 撰写者需要laravel/passport
- 将
Laravel\Passport\PassportServiceProvider::class,
添加到您的应用程序提供商中 - php artisan迁移
- php artisan护照:安装
- 在您的用户模型中添加
HasApiTokens
特征 - Passport :: routes();在AppServiceProvider中
- 将api驱动程序配置为护照
- composer require laravel/passport
- add
Laravel\Passport\PassportServiceProvider::class,
to your app providers - php artisan migrate
- php artisan passport:install
- Add
HasApiTokens
trait to your user model - Passport::routes(); in AppServiceProvider
- Configure api driver to passport
完成后,创建一个UserController并在其中添加以下方法:
Once done, create a UserController and add the following methods in it:
public function auth(Request $request)
{
$params = $request->only('email', 'password');
$username = $params['email'];
$password = $params['password'];
if(\Auth::attempt(['email' => $username, 'password' => $password])){
return \Auth::user()->createToken('my_user', []);
}
return response()->json(['error' => 'Invalid username or Password']);
}
public function index(Request $request)
{
return $request->user();
}
在routes/api.php中,添加以下路由:
In routes/api.php, add the following routes:
Route::post('auth', 'UserController@auth');
Route::group(['middleware' => 'auth:api'], function(){
Route::resource('user', 'UserController@index');
});
现在使用电子邮件地址和密码向http://localhost:8000/auth
发出POST请求,如屏幕截图所示( http://pix.toile-libre.org/upload/original/1483094937.png )这将为您提供 accessToken ,您可以使用此令牌进行操作应用程序中带有Authorization
标头和Bearer XXX
的其他请求,其中xxx是您从/api/auth 端点收到的 accessToken .
Now make a POST request to http://localhost:8000/auth
with the email address and password as shown in the screenshot (http://pix.toile-libre.org/upload/original/1483094937.png) This will get you the accessToken, you can use this token to make other requests in your application with the Authorization
header and Bearer XXX
where xxx is the accessToken you received from /api/auth endpoint.
现在,使用Authorization
标头和令牌值向/api/user
发出GET请求,这将返回经过身份验证的用户的详细信息.
(例如: http://pix.toile-libre.org/upload/original/1483095018.png )
Now, make a GET request to /api/user
with the Authorization
header and the token value, this will return the authenticated user's details.
(eg: http://pix.toile-libre.org/upload/original/1483095018.png)
我还在我的博客上 http://chatterjee.pw/larvel上发布了这些步骤-passport-jwt-authentication/
我希望这会有所帮助!
I hope this helps!