laraveldingo/api添加jwt-auth认证

laraveldingo/api添加jwt-auth认证

内容导读

收集整理的这篇技术教程文章主要介绍了laraveldingo/api添加jwt-auth认证,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3599字,纯文字阅读大概需要6分钟

内容图文

这篇文章主要介绍了关于laravel dingo/api添加jwt-auth认证,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

前面我们学了laravel dingo/api创建简单的api,这样api是开放给所有人的,如何查看和限制api的调用呢?可以用jwt-auth来验证,JSON Web Token Authentication

  1,首先安装jwt-auth插件,在命令行中用composer安装

composer require tymon/jwt-auth '0.5.*'

  2,然后发布

php artisan vendor:publish --provider="TymonJWTAuthProvidersJWTAuthServiceProvider"

  在/config/生成了一个jwt.php文件

  3,生成key

php artisan jwt:generate

  如果命令无法运行,可以在/config/jwt.php文件中修改changeme为自己设置的密匙

'secret' => env('JWT_SECRET', 'changeme'),

  4,修改/app/Api/Controllers/HelloController.php为

<?phpnamespace AppApiControllers;use IlluminateHttpRequest;use AppHttpControllersController;//添加jwt-auth认证use JWTAuth;use TymonJWTAuthExceptionsJWTException;class HelloController extends Controller{

public function index()

{



return '{content:Helloworld!}';

}//添加jwt-auth认证
	public function authenticate(Request $request)

{



// grab credentials from the request



$credentials = $request->only('email', 'password');



try {





// attempt to verify the credentials and create a token for the user





if (! $token = JWTAuth::attempt($credentials)) {







return response()->json(['error' => 'invalid_credentials'], 401);





}



} catch (JWTException $e) {





// something went wrong whilst attempting to encode the token





return response()->json(['error' => 'could_not_create_token'], 500);



}



// all good so return the token



return response()->json(compact('token'));

}}

  5,添加路由(/routes/web.php)

$api->post('auth', 'AppApiControllersHelloController@authenticate');

  6,测试路由:php artisan api:routes,如果出现如下提示表示正确

  访问url:***.com/api/auth显示错误,因为没加token

重新修改hellocontrol和loutes

<?phpnamespace AppApiControllers;use IlluminateHttpRequest;use AppHttpControllersController;use JWTAuth;use TymonJWTAuthExceptionsJWTException;class HelloController extends Controller{

/**

 * Create a new controller instance.

 *

 * @return void

 */

/**

 * Show the application dashboard.

 *

 * @return IlluminateHttpResponse

 */

public function index()

{



return '{content:Helloworld!}';

}
	public function authenticate(Request $request)

{



// grab credentials from the request



$credentials = $request->only('email', 'password');



try {





// attempt to verify the credentials and create a token for the user





if (! $token = JWTAuth::attempt($credentials)) {







return response()->json(['error' => 'invalid_credentials'], 401);





}



} catch (JWTException $e) {





// something went wrong whilst attempting to encode the token





return response()->json(['error' => 'could_not_create_token'], 500);



}



// all good so return the token



return response()->json(compact('token'));

}
//添加user
	public function user()

{


JWTAuth::parseToken();


$user = JWTAuth::parseToken()->authenticate();


return $user;

}}

<?phpRoute::get('/', function () {

return view('welcome');});Auth::routes();Route::get('/home', 'HomeController@index')->name('home');$api = app('DingoApiRoutingRouter');$api->version('v1', function ($api) {

$api->get('helloworld', 'AppApiControllersHelloController@index');
$api->post('auth', 'AppApiControllersHelloController@authenticate');
$api->get('auth', 'AppApiControllersHelloController@user');});

  用谷歌浏览器postman插件获取token,注意是post方法,步骤如下图所示

  将获取的token复制,黏贴到第二步的用户验证token中,下图5中就是我们刚刚注册的用户

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

Laravel5.5中利用Passport实现Auth认证的方法讲解

Laravel框架中composer自动加载的实现详解

以上就是laravel dingo/api添加jwt-auth认证的详细内容,更多请关注Gxl网其它相关文章!

内容总结

以上是为您收集整理的laraveldingo/api添加jwt-auth认证全部内容,希望文章能够帮你解决laraveldingo/api添加jwt-auth认证所遇到的程序开发问题。 如果觉得技术教程内容还不错,欢迎将网站推荐给程序员好友。

内容备注

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。


本文关键词:

联系我们

在线咨询:点击这里给我发消息

邮件:w420220301@qq.com