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 ...
随机推荐
- IOS ASI和AFN的 区别
一.底层实现 1> AFN的底层基于OC的NSURLConnection和NSURLSession2> ASI的底层基于纯C语言的CFNetwork框架3> ASI的运行性能 高于 ...
- mongorc.js文件
当启动的时候,mongo检查用户HOME目录下的一个JavaScript文件.mongorc.js.如果找到,mongo在首次显示提示信息前解析.mongorc.js的内容.如果你使用shell执行一 ...
- 泛型约束-swift
1.泛型定义本体有参量类型约束: 2.泛型扩展对参量类型约束: 3.函数参量约束: 泛型类型的访问控制: 1.与类型无关的通用函数,泛型的任何实例都可以访问: 2.与类型有关的函数(通过扩展约束实现) ...
- POJ 3635 Full Tank? 【分层图/最短路dp】
任意门:http://poj.org/problem?id=3635 Full Tank? Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- [19/03/21-星期四] 异常(Exception) (一)
一.引言 在实际工作中,我们遇到的情况不可能是非常完美的.比如:你写的某个模块,用户输入不一定符合你的要求;你的程序要打开某个文件, 这个文件可能不存在或者文件格式不对 ,你要读取数据库的数据,数据可 ...
- doppia代码支持
stixels_t在stixel.hpp里,存储class stixel的vector
- HashMap面试知识点
HashMap的工作原理是近年来常见的Java面试题. 几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道Hashtable和HashMap之间的区别,那么为何这道面试题如 ...
- http://blog.csdn.net/hhhccckkk/article/details/9313999
http://blog.csdn.net/hhhccckkk/article/details/9313999
- oracle导出/导入 expdp/impdp
Oracle使用EXPDP和IMPDP数据泵进行导出导入的方法(常用方法) 使用expdp和impdp时应该注重的事项: 1.exp和imp是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用 ...
- djano-模板层基础知识
########模板层######## 模板层其实就是templates文件夹里的html文件 其实这里的每个html不是真正意义的上html代码,只有经过模板渲染过后才算的上真正的html页面. 一 ...