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;
}
}
<?php
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方法,步骤如下图所示

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

laravel dingo/api添加jwt-auth认证的更多相关文章
- 用laravel dingo/api创建产品api
沿着上一篇来讲,我们来创建一个简单的item产品api,也是用到laravel dingo/api来实现,对dingo/api不熟的朋友可以翻看前面的文章.好,我们随着ytkah一起来创建产品api ...
- 为Asp.Net Web Api添加Http基本认证
Asp.net Web Api提供了RESTFul web服务的编程接口.默认RESTFul 服务没有提供任何验证或者基于角色的验证,这显然不适合Put.Post.Delete这些操作.Aps.net ...
- 用laravel dingo api插件库创建api的一些心得笔记
用laravel创建api是很多大型项目正在使用的方法,一般他们都是用dingo api插件库来开发自己的api.以下是ytkah用dingo api的一些心得,有需要的朋友可以关注一下 1.安装 因 ...
- 用laravel dingo/api创建简单的api
1,修改.env配置文件添加 API_STANDARDS_TREE=vnd API_SUBTYPE=myapp API_PREFIX=api API_DOMAIN=null API_VERSION=v ...
- Laravel Passport API 认证使用小结
Laravel Passport API 认证使用小结 八月 4, 2017 发布在 Laravel 看到Laravel-China 社区常有人问 Laravel Passport 用于密码验证方式来 ...
- laravel5.5 dingo/api+jwt-auth
因为laravel5.5 具有发现包功能,只要包做了兼容laravel5.5就可以不用在config/app.php添加额外代码了. 集成dingo/api github:https://github ...
- lumen 使用 dingo API 在 phpunit 中 404 的解决方法, 以及鉴权问题
1. phpunit.xml 中添加 dingo 相关配置 <env name="API_STANDARDS_TREE" value="x"/> & ...
- 如何使用Swagger为.NET Core 3.0应用添加JWT授权说明文档
简介 本教程采用WHY-WHAT-HOW黄金圈思维模式编写,黄金圈法则强调的是从WHY为什么学,到WHAT学到什么,再到HOW如何学.从模糊到清晰的学习模式.大家的时间都很宝贵,我们做事前先想清楚为什 ...
- 精讲RestTemplate第9篇-如何通过HTTP Basic Auth认证
本文是精讲RestTemplate第9篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...
随机推荐
- [Understanding] Compressive Sensing and Deep Model
低维模型与深度模型的殊途同归 有助理解核心,陌生概念需要加强理解. 对于做机器学习,和做图像视觉的研究者来说,过去的十年是非常激动人心的十年.以我个人来讲,非常有幸接触了两件事情: 第一件是压缩感知( ...
- [JS] ECMAScript 6 - Prototype : compare with c#
开胃菜 prototype 对象 JavaScript 语言的继承则是通过“原型对象”(prototype). function Cat(name, color) { // <----构造函数 ...
- SpringMVC 文件上传配置,多文件上传,使用的MultipartFile
一.配置文件:SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们首先要配置MultipartResolver:用于处理表单中的file <!-- 配置Multipa ...
- 【代码审计】大米CMS_V5.5.3 后台多处存储型XSS漏洞分析
0x00 环境准备 大米CMS官网:http://www.damicms.com 网站源码版本:大米CMS_V5.5.3试用版(更新时间:2017-04-15) 程序源码下载:http://www ...
- Redis JdkSerializationRedisSerializer,stringRedisSerializer,ProtoBuf 体积,性能简单比较.
/** * User: laizhenwei * Date: 2018-04-10 Time: 14:17 * Description: */ @RunWith(SpringRunner.class) ...
- 解决在html中引入font-awesome的css文件后, 图标显示不出来
今天小颖在做项目时,需要在html文件中引入font-awesome.min.css,但是引入后: 以前小颖在用font-awesome库里的图标时,都是直接从node中下包,然后在main.js中引 ...
- cordova(安卓)(腾讯信鸽注册绑定与反绑定) 插件开发
腾讯信鸽快速开发指南 http://developer.xg.qq.com/index.php/Android_SDK%E5%BF%AB%E9%80%9F%E6%8C%87%E5%8D%97 本文参考 ...
- [原]Jenkins(八)---jenkins构建项目报错时发送错误报告邮件
/** * lihaibo * 文章内容都是根据自己工作情况实践得出. * 版权声明:本博客欢迎转发,但请保留原作者信息! http://www.cnblogs.com/horizonli/p/533 ...
- [LintCode] Max Points on a Line 共线点个数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 洛谷P1316 丢瓶盖【二分】【贪心】
题目:https://www.luogu.org/problemnew/show/P1316 题意: 给定a个点的坐标(在一条直线上),现在要选b个点,问这b个点的最近距离的最大值是多少. 思路: 感 ...