Laravel 使用 JWT 做 API 认证之tymon/jwt-auth 1.0.0-beta.1实践 - moell - SegmentFault
安装
将"tymon/jwt-auth": "1.0.0-beta.1" 添加到 composer.json 中,执行 composer update
Providers
config/app.php 中在 providers 里添加 Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
Class Aliases
config/app.php 中在 aliases 里添加 'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class
修改认证驱动
修改config/auth.php,将 api 的 driver 修改为 jwt。如下:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
]
添加路由
在 routes/api.php 中添加以下路由:
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', ['namespace' => 'App\Http\Controllers\Api\V1'], function($api) {
$api->post('token', 'UserController@token'); //获取token
$api->post('refresh-token', 'UserController@refershToken'); //刷新token
$api->group(['middleware' => ['auth:api']], function($api) {
$api->post('logout', 'UserController@logout'); //登出
$api->get('me', 'UserController@me'); //关于我
});
});
AppUser.php
添加 getJWTIdentifier 和 getJWTCustomClaims 实现 AuthenticatableUserContract
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject as AuthenticatableUserContract;
class User extends Authenticatable implements AuthenticatableUserContract
{
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey(); // Eloquent model method
}
/**
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}
实现路由所需要的控制器
<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Controllers\Api\V1\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;
use Auth;
class UserController extends Controller
{
protected $guard = 'api';
/**
* 获取token
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function token(Request $request)
{
$credentials=[
'email' => $request->email,
'password' => $request->password,
'status' => 0,
];
try {
if (! $token = Auth::guard($this->guard)->attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json(compact('token'));
}
/**
* @return mixed
*/
public function refershToken()
{
$token = Auth::guard($this->guard)->refresh();
return $this->response->array(compact('token'));
}
/**
* 个人信息
*
* @return User|null
*/
public function me()
{
return Auth::guard('api')->user();
}
/**
* 退出
*
* @return \Illuminate\Http\JsonResponse
*/
public function logout()
{
Auth::guard($this->guard)->logout();
return response()->json(['status' => 'ok']);
}
}
原文地址
http://moell.cn/article/37
Laravel 使用 JWT 做 API 认证之tymon/jwt-auth 1.0.0-beta.1实践 - moell - SegmentFault的更多相关文章
- laravel使用JWT做API认证
最近项目做API认证,最终技术选型决定使用JWT,项目框架使用的是laravel,laravel使用JWT有比较方便使用的开源包:jwt-auth.php 后端实现JWT认证方法 使用composer ...
- laravel jwt 做API 退出登录(注销) 该怎么弄? 如何让token失效
laravel jwt 做API 退出登录(注销) 该怎么弄? 如何让token失效 php框架 laravel 2.1k 次浏览 问题对人有帮助,内容完整,我也想知道答案0问题没有实际价值,缺少关键 ...
- 前后端分离下用jwt做用户认证
0 前后端分离下的用户信息认证 前端使用Vue+axios,后端使用SpringBoot+SpringSecurity. 为了解决http无状态的问题,我采用jwt(json web token)保存 ...
- springboot+jwt做api的token认证
本篇和大家分享jwt(json web token)的使用,她主要用来生成接口访问的token和验证,其单独结合springboot来开发api接口token验证很是方便,由于jwt的token中存储 ...
- Laravel 5.2 使用 JWT 完成多用户认证 | Laravel China 社区 - 高品质的 Laravel 开发者社区 - Powered by PHPHub
Json Web Token# JWT代表Json Web Token.JWT能有效地进行身份验证并连接前后端. 降地耦合性,取代session,进一步实现前后端分离 减少服务器的压力 可以很简单的实 ...
- beego应用做纯API后端如何使用jwt实现无状态权限验证
jwt是什么,可以百度下其它文章,我原来看到一个讲的详细的,现在找不到了.先简单介绍下我个人的理解,就是一个token,只不过通过加密解密的手段,能让这一串字符带有一些简单的信息.这样解密jwt后不用 ...
- [ Laravel 5.3 文档 ] 安全 ―― API认证(Passport)保障安全性。
1.简介 Laravel通过传统的登录表单已经让用户认证变得很简单,但是API怎么办?API通常使用token进行认证并且在请求之间不维护session状态.Laravel使用LaravelPassp ...
- Laravel Passport API 认证使用小结
Laravel Passport API 认证使用小结 八月 4, 2017 发布在 Laravel 看到Laravel-China 社区常有人问 Laravel Passport 用于密码验证方式来 ...
- 【laravel】基于jwt实现用户认证
安装及基础配置 使用 composer 安装 # 建议使用1.0以上版本 composer require tymon/jwt-auth .*@rc 进行一些配置 有些文档会说要添加 Tymon\JW ...
随机推荐
- Linux下c开发 之 线程通信
Linux下c开发 之 线程通信 1.Linux“线程” 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linux本身 ...
- MySQL-Utilities:mysqldbcompare及跳过复制错误
mysqldbcompare也是MySQL-Utilities工具集的一个脚本.mysqldbcompare从两个数据库比较对象和数据的不同.数据库中的对象包括:表.视图.触发器.存储过程.函数和事件 ...
- Grep- Linux必学的60个命令
1.作用 grep命令可以指定文件中搜索特定的内容,并将含有这些内容的行标准输出.grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所 ...
- windows 服务下搭建jsp运行环境
此处搭建的是运行环境,不是开发环境. 1, 下载sdk 并安装 1.8 http://rj.baidu.com/soft/detail/14459.html?ald 2, 配置环境变量 步 ...
- [转]Windows钩子
Windows钩子 Windows应用程序的运行模式是基于消息驱动的,任何线程只要注册了窗口类就会有一个消息队列来接收用户的输入消息和系统消息.为了取得特定线程接收或发送的消息,就要 Windows提 ...
- MYSQL实现列拼接,即同一个字段,多条记录拼接成一条
一.首先,新建三张表 DROP TABLE IF EXISTS `article`; CREATE TABLE `article` ( `id` ) unsigned NOT NULL AUTO_IN ...
- java填坑记录
一.The absolute uri: [http://java.sun.com/jsp/jstl/core] cannot be resolved in either web.xml or the ...
- 【DM642】ICELL Interface—Cells as Algorithm Containers
ICELL Interface—Cells as Algorithm Containers: DSP的算法标准(XDAIS)为算法提供了一个标准的接口.这样我们就可以使用第三方的算法.For tech ...
- 使用MySQL会话变量实现窗口函数
一.MySQL窗口函数 (1) 序号函数 row_number()在相等的两条记录上随机排序,但序号按照1.2递增,然后后面的序号继续递增为3,中间不会产生序号间隙: rank()/dense_ran ...
- linux下用eclipse开发mapreduce遇到的问题
Unable to create the selected preference page.org/apache/hadoop/eclipse/preferences/MapReducePrefere ...