前面我们学了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认证的更多相关文章

  1. 用laravel dingo/api创建产品api

    沿着上一篇来讲,我们来创建一个简单的item产品api,也是用到laravel dingo/api来实现,对dingo/api不熟的朋友可以翻看前面的文章.好,我们随着ytkah一起来创建产品api ...

  2. 为Asp.Net Web Api添加Http基本认证

    Asp.net Web Api提供了RESTFul web服务的编程接口.默认RESTFul 服务没有提供任何验证或者基于角色的验证,这显然不适合Put.Post.Delete这些操作.Aps.net ...

  3. 用laravel dingo api插件库创建api的一些心得笔记

    用laravel创建api是很多大型项目正在使用的方法,一般他们都是用dingo api插件库来开发自己的api.以下是ytkah用dingo api的一些心得,有需要的朋友可以关注一下 1.安装 因 ...

  4. 用laravel dingo/api创建简单的api

    1,修改.env配置文件添加 API_STANDARDS_TREE=vnd API_SUBTYPE=myapp API_PREFIX=api API_DOMAIN=null API_VERSION=v ...

  5. Laravel Passport API 认证使用小结

    Laravel Passport API 认证使用小结 八月 4, 2017 发布在 Laravel 看到Laravel-China 社区常有人问 Laravel Passport 用于密码验证方式来 ...

  6. laravel5.5 dingo/api+jwt-auth

    因为laravel5.5 具有发现包功能,只要包做了兼容laravel5.5就可以不用在config/app.php添加额外代码了. 集成dingo/api github:https://github ...

  7. lumen 使用 dingo API 在 phpunit 中 404 的解决方法, 以及鉴权问题

    1. phpunit.xml 中添加 dingo 相关配置 <env name="API_STANDARDS_TREE" value="x"/> & ...

  8. 如何使用Swagger为.NET Core 3.0应用添加JWT授权说明文档

    简介 本教程采用WHY-WHAT-HOW黄金圈思维模式编写,黄金圈法则强调的是从WHY为什么学,到WHAT学到什么,再到HOW如何学.从模糊到清晰的学习模式.大家的时间都很宝贵,我们做事前先想清楚为什 ...

  9. 精讲RestTemplate第9篇-如何通过HTTP Basic Auth认证

    本文是精讲RestTemplate第9篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...

随机推荐

  1. [Tensorflow] Cookbook - CNN

    Convolutional Neural Networks (CNNs) are responsible for the major breakthroughs in image recognitio ...

  2. iOS开发-- 使用TFHpple解析html

    占坑 http://www.raywenderlich.com/14172/how-to-parse-html-on-ios

  3. 对osg节点添加glsl特效(片断着色器FragmentShader)

    读取一个模型到节点node,然后想对node施加一些特效,这时可以只使用片段着色器 其中: gl_Color表示固定管线计算出来的颜色,包含光照效果 gl_TexCoord[]表示纹理坐标 unifo ...

  4. iOS 判断两个日期之间的间隔

    本文转载至 http://www.cnblogs.com/Ewenblog/p/3891791.html   两个时间段,判断之间的相差,做一些时间范围限制使用 NSDateFormatter * d ...

  5. 什么是IOC为什么要使用IOC

    概念: 作用: 结论:借助于“第三方”实现具有依赖关系的对象之间的解耦 在使用IOC之前的情况 如果有一个齿轮出了问题,就可能会影响到整个齿轮组的正常运 使用IOC之后 对象A获得依赖对象B的过程,由 ...

  6. Spark Multilayer perceptron classifier (MLPC)多层感知器分类器

    多层感知器分类器(MLPC)是基于前馈人工神经网络(ANN)的分类器. MLPC由多个节点层组成. 每个层完全连接到网络中的下一层. 输入层中的节点表示输入数据. 所有其他节点,通过输入与节点的权重w ...

  7. ubuntu如何设置开机启动默认命令行界面

    图形模式下,首先进入终端: 1. 运行 sudo vi/etc/default/grub 2. 找到 GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash” 3.改为 GR ...

  8. 泡泡一分钟:Exploiting Points and Lines in Regression Forests for RGB-D Camera Relocalization

    Exploiting Points and Lines in Regression Forests for RGB-D Camera Relocalization 利用回归森林中的点和线进行RGB-D ...

  9. CCPC-Wannafly Winter Camp Day4 Div1 - 夺宝奇兵 - [简单思维题]

    题目链接:https://zhixincode.com/contest/18/problem/A?problem_id=259 题目描述 wls正在玩一个寻宝游戏. 宝藏一共有 $n$ 种,都藏在一个 ...

  10. tensorflow 一维卷积 tf.layers.conv1()使用

    在自然语言处理中,主要使用一维的卷积. API tf.layers.conv1d( inputs, filters, kernel_size, strides=1, padding='valid', ...