这篇文章主要介绍了关于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="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
在/config/生成了一个jwt.php文件
3,生成key
php artisan jwt:generate
如果命令无法运行,可以在/config/jwt.php文件中修改changeme为自己设置的密匙
'secret' => env('JWT_SECRET', 'changeme'),
4,修改/app/Api/Controllers/HelloController.php为
<?php namespace App\Api\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; //添加jwt-auth认证 use JWTAuth; use Tymon\JWTAuth\Exceptions\JWTException; 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', 'App\Api\Controllers\HelloController@authenticate');
6,测试路由:php artisan api:routes,如果出现如下提示表示正确
访问url:***.com/api/auth显示错误,因为没加token
重新修改hellocontrol和loutes
<?php namespace App\Api\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use JWTAuth; use Tymon\JWTAuth\Exceptions\JWTException; class HelloController extends Controller { /** * Create a new controller instance. * * @return void */ /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ 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; } }
name('home'); $api = app('Dingo\Api\Routing\Router'); $api->version('v1', function ($api) { $api->get('helloworld', 'App\Api\Controllers\HelloController@index'); $api->post('auth', 'App\Api\Controllers\HelloController@authenticate'); $api->get('auth', 'App\Api\Controllers\HelloController@user'); });
用谷歌浏览器postman插件获取token,注意是post方法,步骤如下图所示
将获取的token复制,黏贴到第二步的用户验证token中,下图5中就是我们刚刚注册的用户
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
Laravel5.5中利用Passport实现Auth认证的方法讲解
以上是laravel dingo/api添加jwt-auth认证的详细内容。更多信息请关注PHP中文网其他相关文章!

本篇文章给大家带来了关于laravel的相关知识,其中主要介绍了关于单点登录的相关问题,单点登录是指在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于laravel的相关知识,其中主要介绍了关于Laravel的生命周期相关问题,Laravel 的生命周期从public\index.php开始,从public\index.php结束,希望对大家有帮助。

在laravel中,guard是一个用于用户认证的插件;guard的作用就是处理认证判断每一个请求,从数据库中读取数据和用户输入的对比,调用是否登录过或者允许通过的,并且Guard能非常灵活的构建一套自己的认证体系。

laravel中asset()方法的用法:1、用于引入静态文件,语法为“src="{{asset(‘需要引入的文件路径’)}}"”;2、用于给当前请求的scheme前端资源生成一个url,语法为“$url = asset('前端资源')”。

本篇文章给大家带来了关于laravel的相关知识,其中主要介绍了关于使用中间件记录用户请求日志的相关问题,包括了创建中间件、注册中间件、记录用户访问等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于laravel的相关知识,其中主要介绍了关于中间件的相关问题,包括了什么是中间件、自定义中间件等等,中间件为过滤进入应用的 HTTP 请求提供了一套便利的机制,下面一起来看一下,希望对大家有帮助。

laravel路由文件在“routes”目录里。Laravel中所有的路由文件定义在routes目录下,它里面的内容会自动被框架加载;该目录下默认有四个路由文件用于给不同的入口使用:web.php、api.php、console.php等。

ThinkPHP vs Laravel 当下国内最流行的两款PHP框架,孰好孰坏,争议最多!做为初学者,也很纠结,到底学哪个好呢?本文PHP中文网来认真盘点一下,不吹不黑,更不便偏颇哪一方。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3汉化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版