laravel5.5 + jwt-auth:dev-develop

  1. 安装扩展
    composer require tymon/jwt-auth:dev-develop --prefer-source
  1. 添加服务提供器

config/app.php中增加provider者和aliases,写入对应的数组

//provider
'Tymon\JWTAuth\Providers\LaravelServiceProvider' //aliases
'JWTAuth' => 'Tymon\JWTAuth\Facades\JWTAuth'
'JWTFactory' => 'Tymon\JWTAuth\Facades\JWTFactory'
  1. 发布配置文件
    php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider" --force

命令会生成 config/jwt.php 配置文件

  1. 生成key
    php artisan jwt:secret

会在.env 添加JWT_SECRET

JWT_SECRET=z4Pv7YXnOOodpuGO7FOy1vLsxxxxicmoU
  1. 更改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 [];
}
}
  1. 配置auth看守器

config/auth.php中修改看守器

'defaults' => [
'guard' => 'api',
'passwords' => 'users',
], ... 'guards' => [
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
  1. 配置路由
Route::get('/signin', "AuthController@signin");

//这里分配了中间件,验证是否登录
Route::group(['middleware' => ['auth:api']], function(){
Route::get('menu', 'MenuController@index');
});
  1. 登录验证
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

由于使用老版产生的问题

  1. Class Tymon\JWTAuth\Providers\JWT\Namshi does not exist

    php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider" --force

  2. 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的使用的更多相关文章

  1. laravel5通过auth.attempt事件加入登陆验证码

    <?php namespace WangDong\Http\Controllers\Auth; use Illuminate\Http\Exception\HttpResponseExcepti ...

  2. laravel5.8 Auth::guide

    // 使用下面这个命令Laravel会自动为我们生成Auth路由和认证模块.跟着代码往下解读. php artisan make:auth // Http/Controllers/Auth/Login ...

  3. laravel5.5 调用系统自带登陆认证auth

    1执行命令 php artisan make:auth 2 编辑文件 config/auth guardes 'admin' => [ 'driver' => 'session', 'pr ...

  4. laravel5.8ajax请求auth认证返回302的解决方法。

    注册 /app/Http/Controller/Auth/RegisterController.php <?php namespace App\Http\Controllers\Auth; us ...

  5. Laravel5.0学习--02 实例进阶

    本文以laravel5.0.22为例. 本节以新建一个简单的博客作为实例. 准备工作 数据库配置 .env文件(也可以直接修改config/database.php) DB_HOST=localhos ...

  6. Laravel5.0学习--01 入门

    本文以laravel5.0.22为例. 生产环境建议使用laravel5.1版本,因为该版本是长期支持版本.5.1文档更详细:http://laravel-china.org/docs/5.1. 环境 ...

  7. Linux搭建smtp服务器+laravel5.2发邮件配置

    /** * 这里主要是想通过自己搭建smtp服务器,配置laravel5.2框架,实现邮箱发邮件功能, * 主要内容是搭建smtp服务器,laravel5.2发邮件顺手提一下 */ /** * 1.l ...

  8. Laravel5中集成Jasig cas统一认证系统

    CAS : CAS(Central Authentication Service)是一款不错的针对 Web 应用的单点登录框架,这里介绍下我刚在laravel5上搭建成功的cas.提前准备工作:可运行 ...

  9. laravel5.3 笔记一

    laravel5.3 笔记 安装环境 laravel环境,laravel中文学习论坛上面有相关的教程 创建应用 laravel new blog 其中blog就是你的应用的名字 数据迁移 php ar ...

  10. laravel5.2之logout注销账号无效

    问题描述:laravel5.2的框架,使用框架auth用户认证后,进行账号注销退出的时候,无法实现. 只有清除浏览器缓存,才能实现账号退出. 解决办法: 改变路由 Route::get('auth/l ...

随机推荐

  1. codeforces 611D New Year and Ancient Prophecy

    f[i = 以i结尾][j = 长度为j] = 方案数. f[i][j] = sum{ f[i-j][k] , k < j || (k == j && s(i-j+1,j) &g ...

  2. 玩转web之ligerui(一)---ligerGrid又一次指定url

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012116457/article/details/27109227 请珍惜小编劳动成果.该文章为小 ...

  3. RHEL6.5和RHEL7 的区别(转)

    Rhel6.5实验环境搭建 1)操作系统安装 RHEL7是一站式安装   2)网卡配置文件 RHEL6: /etc/sysconfig/network-scripts/ifcfg-eth0 RHEL7 ...

  4. git bush 代码提交

    # git add . # git commit -m"init project" # git push

  5. 【luogu P3884 [JLOI2009]二叉树问题】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3884 对方不想和你说话并向你扔了一个lca模板. #include <cstdio> #incl ...

  6. 复合词(Compound Words, UVa 10391)(stl set)

    You are to find all the two-word compound words in a dictionary. A two-word compound word is a word i ...

  7. LeetCode5.最长回文子串 JavaScript

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &qu ...

  8. 自动化测试selenium教程

    什么是自动化测试: 自动帮我们测试一个系统里面的主要功能,一个app.电脑网站.网页,每个系里面许多的功能,好比一个淘宝页面,里面N多功能,登录.注册,推荐,商品详情.评论等等:软件生命周期:需求调研 ...

  9. 【oracle使用笔记3】sql查询遇到的若干问题总结

    在整个开发过程中,sql查询操作的频率比较高,在不同的业务场景下会出现不同的查询需求,以下是我在项目中遇到的查询需求,总结一下. [查询一]:取查询出的第一条数据 select * from (sel ...

  10. JAVA如何跨项目调用接口

    public String load(String url, String query) throws Exception { URL restURL = new URL(url); /* * 此处的 ...