前面我们学了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. [Artoolkit] Android Sample of nftSimple

    结合:[Artoolkit] ARToolKit's SDK Structure on Android 重难点:aRBaseLib/, nftSimple/, libcpufeatures.a aRB ...

  2. LUA重难点解析

    1.元表 元表也是一个 table,它附加在另一个 table 上,可以扩展该 table 的某些行为. 拿 __index 来举例,它是用来扩展查找索引行为的.在查找一个 key 对应的值时,会依次 ...

  3. Failed to resolve: com.android.support:appcompat-v7:27.0.1问题解决

    今天,在毫无征兆的情况下AndroidStudio又抽风了,搞了大半天,试了网上众多方案,终于解决了这个问题.咱们一步一步来 第一步:这是最开始的bug Error:Failed to resolve ...

  4. WCF数据传输安全--数字证书

    WCF 的传输安全涉及认证(客户端与服务器端双向认证).消息一致性(签名)和机密性(加密)三个主题. 常用认证方式: 第一:用户名/密码认证:wcf提供三种认证模式:1.将用户名映射到windows账 ...

  5. NHibernate之旅(21):探索对象状态

    本节内容 引入 对象状态 对象状态转换 结语 引入 在程序运行过程中使用对象的方式对数据库进行操作,这必然会产生一系列的持久化类的实例对象.这些对象可能是刚刚创建并准备存储的,也可能是从数据库中查询的 ...

  6. linux命令之间的分号,&&, ||

    在用linux命令时候, 我们经常需要同时执行多条命令, 那么命令之间该如何分割呢? 分号: 顺序地独立执行各条命令, 彼此之间不关心是否失败, 所有命令都会执行. &&  : 顺序执 ...

  7. 正则表达式/(^\s*)|(\s*$)/g意思

    包含以空格.回车符等字符开头 或者 空格.回车符等字符结尾 的字符串,可过滤出所有空格.回车符的字符

  8. DataGridView实时提交

    自定义了一个工具,根据DataGridView中的值进行其他操作.在DataGridView中修改了值,直接做其他操作时, 结果DataGridview中的值显示为a,则操作的属性却是没修改后的值b. ...

  9. mysql 语句 字段 和结构主键外键的增删改

    primary key 主键  notnull 不为空 unique 唯一       foreign key(外键) references t1(id)        auto_increment ...

  10. 文本分类-TensorRT优化结果对比图

    做的文本二分类,使用tensorRT进行图优化和加速,输出预测概率结果对比如下: 从结果对比来看,概率值有微小的变化,但不影响最终的分类