php dingo和jwt,laravel dingo/api添加jwt-auth认证

这篇文章主要介绍了关于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为

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,如果出现如下提示表示正确

c3ab18c3cbff50ea97bbd8b016f2a46f.png

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

重新修改hellocontrol和loutes

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;

}

}

Route::get('/', function () {

return view('welcome');

});

Auth::routes();

Route::get('/home', 'HomeController@index')->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方法,步骤如下图所示

c0f9d1c829e46dcbe7fcfc24fb6e2e6a.png

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

c811c57deee56a75587d631f321416b6.png

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

相关推荐:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值