laravel5.5jwt-auth的使用
laravel5.5 + jwt-auth:dev-develop
- 安装扩展
composer require tymon/jwt-auth:dev-develop --prefer-source
- 添加服务提供器
config/app.php中增加provider者和aliases,写入对应的数组
//provider
'Tymon\JWTAuth\Providers\LaravelServiceProvider'
//aliases
'JWTAuth' => 'Tymon\JWTAuth\Facades\JWTAuth'
'JWTFactory' => 'Tymon\JWTAuth\Facades\JWTFactory'
- 发布配置文件
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider" --force
命令会生成 config/jwt.php 配置文件
- 生成key
php artisan jwt:secret
会在.env 添加JWT_SECRET
JWT_SECRET=z4Pv7YXnOOodpuGO7FOy1vLsxxxxicmoU
- 更改user model
<?php
namespace App;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
// Rest omitted for brevity
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}
- 配置auth看守器
config/auth.php中修改看守器
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
...
'guards' => [
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
- 配置路由
Route::get('/signin', "AuthController@signin");
//这里分配了中间件,验证是否登录
Route::group(['middleware' => ['auth:api']], function(){
Route::get('menu', 'MenuController@index');
});
- 登录验证
public function signin(Request $request)
{
if($token = JWTAuth::getToken()){
try{
JWTAuth::invalidate($token);
}catch(\Exception $e){
}
}
$credentials = $request->only('name', 'password');
if (! $token = JWTAuth::attempt($credentials)) {
return $this->error('用户名或密码错误');
}
return $this->success(['token' => $token]);
}
参考资料
https://github.com/tymondesigns/jwt-auth/wiki/Installation
http://jwt-auth.readthedocs.io/en/docs/quick-start/#update-your-user-model
https://github.com/tymondesigns/jwt-auth/issues/1298
由于使用老版产生的问题
Class Tymon\JWTAuth\Providers\JWT\Namshi does not exist
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider" --force
Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject
更改user model
<?php
namespace App;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
// Rest omitted for brevity
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}
laravel5.5jwt-auth的使用的更多相关文章
- laravel5通过auth.attempt事件加入登陆验证码
<?php namespace WangDong\Http\Controllers\Auth; use Illuminate\Http\Exception\HttpResponseExcepti ...
- laravel5.8 Auth::guide
// 使用下面这个命令Laravel会自动为我们生成Auth路由和认证模块.跟着代码往下解读. php artisan make:auth // Http/Controllers/Auth/Login ...
- laravel5.5 调用系统自带登陆认证auth
1执行命令 php artisan make:auth 2 编辑文件 config/auth guardes 'admin' => [ 'driver' => 'session', 'pr ...
- laravel5.8ajax请求auth认证返回302的解决方法。
注册 /app/Http/Controller/Auth/RegisterController.php <?php namespace App\Http\Controllers\Auth; us ...
- Laravel5.0学习--02 实例进阶
本文以laravel5.0.22为例. 本节以新建一个简单的博客作为实例. 准备工作 数据库配置 .env文件(也可以直接修改config/database.php) DB_HOST=localhos ...
- Laravel5.0学习--01 入门
本文以laravel5.0.22为例. 生产环境建议使用laravel5.1版本,因为该版本是长期支持版本.5.1文档更详细:http://laravel-china.org/docs/5.1. 环境 ...
- Linux搭建smtp服务器+laravel5.2发邮件配置
/** * 这里主要是想通过自己搭建smtp服务器,配置laravel5.2框架,实现邮箱发邮件功能, * 主要内容是搭建smtp服务器,laravel5.2发邮件顺手提一下 */ /** * 1.l ...
- Laravel5中集成Jasig cas统一认证系统
CAS : CAS(Central Authentication Service)是一款不错的针对 Web 应用的单点登录框架,这里介绍下我刚在laravel5上搭建成功的cas.提前准备工作:可运行 ...
- laravel5.3 笔记一
laravel5.3 笔记 安装环境 laravel环境,laravel中文学习论坛上面有相关的教程 创建应用 laravel new blog 其中blog就是你的应用的名字 数据迁移 php ar ...
- laravel5.2之logout注销账号无效
问题描述:laravel5.2的框架,使用框架auth用户认证后,进行账号注销退出的时候,无法实现. 只有清除浏览器缓存,才能实现账号退出. 解决办法: 改变路由 Route::get('auth/l ...
随机推荐
- (转)C++类库开发之导出类设计原则
上一篇博客详细陈述了类库开发的各个知识点(http://blog.csdn.net/z702143700/article/details/45989993),本文将进一步陈述,对于类库开发过程中导出类 ...
- 浅析内存对齐与ANSI C中struct型数据的内存布局-内存对齐规则
这些问题或许对不少朋友来说还有点模糊,那么本文就试着探究它们背后的秘密. 首先,至少有一点可以肯定,那就是ANSI C保证结构体中各字段在内存中出现的位置是随它们的声明顺序依次递增的,并且第一个字段的 ...
- Android——Activity生命周期
启动: 触发 onCreate() onStart() onResume() Home键: 触发 onPause() onStop() back键退出: 触发 onPause() o ...
- ACM-ICPC (10/19)
这两天在看虚树,的确很难理解. 不过大致的思路就是说删掉一些没有用的点,但是仍然保持树的相对结构,树上只有两种点,一个是集合点,和一些LCA,这些LCA是为了保持树的相对结构,才留下的. 具体做法网上 ...
- ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(三) 激动人心的时刻到啦,实现1v1聊天
看起来挺简单,细节还是很多的,好,接上一篇,我们已经成功连接singalR服务器了,那么剩下的内容呢,就是一步一步实现聊天功能. 我们先看看缺什么东西 点击好友弹框之后,要给服务器发消息,进入组Gro ...
- stn,spatial transformer network总结
对整篇paper的一个总结:https://blog.csdn.net/xbinworld/article/details/69049680 github:1.https://github.com/D ...
- SignalR中的依赖注入
什么是依赖注入? 如果你已经熟悉依赖注入可以跳过此节. 依赖注入 (DI) 模式下,对象并不为自身的依赖负责. 下边的例子是一个主动 DI. 假设你有个对象需要消息日志.你可能定义了一个日志接口: C ...
- 手机验证码免费10条\java、C#、html....
使用互亿无线短信接口网址:http://www.ihuyi.com/. 首先第一步,进行注册 第二步:注册成功后进来的页面 第三步:实名认证 第四步:个人信息 等待认证成功后才能继续操作 第五步:进行 ...
- 【题解】洛谷P3166 [CQOI2014] 数三角形(组合+枚举)
洛谷P3166:https://www.luogu.org/problemnew/show/P3166 思路 用组合数求出所有的3个点组合(包含不合法的) 把横竖的3个点共线的去掉 把斜的3个点共线的 ...
- vue watch数组或者对象
1.普通的watch data() { return { frontPoints: 0 } }, watch: { frontPoints(newValue, oldValue) { console. ...