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 ...
随机推荐
- 学 shell (1/5)
假设这是某脚本 x.sh 的内容,使用 sh x.sh arg1 来执行该脚本 #!/bin/bashcd `dirname $0`/..source scripts/status.shstart $ ...
- 启动mysqld报 mysql the server quit without updating pid file
查看mysql服务器的错误日志有一句: InnoDB: mmap(137363456 bytes) failed; errno 12 原来是内存不够用(需要131MB)呀,把my.cnf中的innod ...
- 强大的矩阵奇异值分解(SVD)
转:http://www.cnblogs.com/LeftNotEasy/archive/2011/01/19/svd-and-applications.html 前言: PCA的实现一般有两种,一种 ...
- 【Spring】Spring中用到的设计模式
1.简单工厂 又叫静态工厂方法模式,不属于23种设计模式之一. 简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类. Spring中的BeanFactory就是简单工厂模式的 ...
- http statusCode(状态码)含义
201-206都表示服务器成功处理了请求的状态代码,说明网页可以正常访问. 200(成功) 服务器已成功处理了请求.通常,这表示服务器提供了请求的网页. 201(已创建) 请求成功且服务器已创建了新的 ...
- 将数据 导出excel表格式
我的考试完提交生成的数据 这是我的考试题类型 //导出调查评议的数据 public function diaocha(){ $xlsName = '表格形式 调查评议 信息'; $xlsTitle = ...
- C需要中的static
转载 详细分析一下static关键字在编写程序时有的三大类用法: 一,static全局变量 我们知道,一个进程在内存中的布局如图1所示: 其中.text段保存进程所执行的程序二进制文件,.data段保 ...
- WebSphere Application Server V8.5.5.0
Downloadable files Abstract IBM WebSphere Application Server Version 8.5.5 Refresh Pack for all plat ...
- CentOS 7.4下使用yum安装MySQL5.7.20 最简单的 (引用)
引用 https://blog.csdn.net/z13615480737/article/details/78906598 CentOS7默认数据库是mariadb, 但是 好多用的都是mysql ...
- Entity Framework DbSet<T>之Include方法与IQueryable<T>扩展方法Include的使用
Entity Framework使用Code First方式时,实体之间已经配置好关系,根据实际情况某些情况下需要同时获取导航属性,比如获取商品的同时需要获取分类属性(导航属性),或者基于优化方面考虑 ...