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. OC 方法声明使用

    Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { int _age; } - (void) ...

  2. iOS 代码混淆--pch实现

    之前实现代码的混淆一般是使用sh脚本,不过也是需要把一写需要混淆的方法写到一个文件中(除非是使用特定的命名规范统一混淆), 现在发现,实现代码的混淆pch 文件就可以轻松完成! 1,在新建的工程中 创 ...

  3. 2018.12.22 Spring学习02

    Spring学习02 1.使用注解配置Spring 1.1 为主配置文件引入新的命名空间(约束) 添加约束文件xxx-xxx-context.xml 添加到主配置文件中 选择刚才的context.xm ...

  4. 2017.9.1 Java中的程序方法

    今日内容介绍 1.方法基础知识 2.方法高级内容 3.方法案例 01方法的概述 * A: 为什么要有方法 * 提高代码的复用性 * B: 什么是方法 * 完成特定功能的代码块. 02方法的定义格式 * ...

  5. centos7下javac:未找到命令的问题

    在linux下编译java程序,执行javac编译生成class文件时,在centos7终端输入如,javac hello.java    会提示未找到指令,但用java -verison测试环境变量 ...

  6. centOS 7 更改root密码

    Linux忘记密码怎么办,不用重装系统,进入emergency mode 更改root密码即可. 首先重启系统,按下 向下 按钮, 定位在第一个,摁 e ,进行编辑 找到 ro , 把ro改为 rw ...

  7. LINQ 方法

    过滤操作符 Where 运算符(Linq扩展方法)根据给定条件过滤集合. 在其中扩展方法有以下两个重载.一个过载需要Func <TSource,bool>输入参数和第二个重载方法需要Fun ...

  8. data-ng-show指令

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. java交换两个值的三种方法 经典

    1.中间变量(在开发中常用) int c=a; a=b; b=c; System.out.println("a的值: "+a+" b的值: "+b); 2.按位 ...

  10. python之selectors

    selectors是select模块的包装器,ptython文档建议大部分情况使用selectors而不是直接使用selectors 样例代码如下 # -*- coding: utf-8 -*- __ ...