第一部分 安装jwt第一步. 使用composer安装 tymon/jwt-auth :
`composer require tymon/jwt-auth 1.0.0-rc.3
第二步. 添加服务提供商(laravel5.4及以下版本,5.5及以上版本无需添加),
将下面这行添加至 config/app.php 文件 providers 数组中:
<?php// 文件:app.php'providers' => [ // other code tymon\jwtauth\providers\laravelserviceprovider::class,]
第三步. 发布配置文件,
运行如下命令发布 jwt-auth 的配置文件:
php artisan vendor:publish --provider=tymon\jwtauth\providers\laravelserviceprovider
第四步. 生成密钥,
此命令会在你的 .env 文件中新增一行 jwt_secret=secret。
php artisan jwt:secret
第二部分 开始配置第五步. 配置 auth guard,`
在 config/auth.php 文件中,你需要将 guards/driver 更新为 jwt,
只有在使用 laravel 5.2 及以上版本的情况下才能使用。
<?php 'defaults' => [ 'guard' => 'api', 'passwords' => 'users',],// other code'guards' => [ 'api' => [ 'driver' => 'jwt', 'provider' => 'users', ],],
第六步. 更改 user model,
在user model上实现tymonjwtauthcontractsjwtsubject接口,
实现getjwtidentifier() and getjwtcustomclaims()两个方法。
<?phpnamespace app;use tymon\jwtauth\contracts\jwtsubject;class user extends authenticatable implements jwtsubject{ // other code // 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 []; }}
第三部分 快速创建demo测试第七步. 添加一些基本身份验证路由:
<?phproute::group([ 'middleware' => 'api', 'prefix' => 'auth'], function ($router) { route::post('login', 'authcontroller@login'); route::post('register', 'authcontroller@register'); route::post('logout', 'authcontroller@logout'); route::post('refresh', 'authcontroller@refresh'); route::post('me', 'authcontroller@me');});
第八步. 创建authcontroller控制器 => php artisan make:controller authcontroller:<?phpnamespace app\http\controllers;use app\user;use illuminate\http\request;use illuminate\support\facades\validator;class authcontroller extends controller{ /** * create a new authcontroller instance. * * @return void */ public function __construct() { $this->middleware('auth:api', ['except' => ['login', 'register']]); } /** * 用户使用邮箱密码获取jwt token. * * @return \illuminate\http\jsonresponse */ public function login() { $credentials = request(['email', 'password']); if (! $token = auth()->attempt($credentials)) { return response()->json(['error' => 'unauthorized'], 401); } return $this->respondwithtoken($token); } /** * 注册新用户 */ public function register(request $request) { // 数据校验 // 数据验证 $validator = validator::make($request->all(), [ 'name' => 'required', 'email' => 'required|email', 'password' => 'required', 'c_password' => 'required|same:password' ]); if ($validator->fails()) { return response()->json(['error'=>$validator->errors()], 401); } // 读取参数并保存数据 $input = $request->all(); $input['password'] = bcrypt($input['password']); $user = user::create($input); // 创建token并返回 return $user; } /** * 获取经过身份验证的用户. * * @return \illuminate\http\jsonresponse */ public function me() { return response()->json(auth()->user()); } /** * 刷新token. * * @return \illuminate\http\jsonresponse */ public function refresh() { return $this->respondwithtoken(auth()->refresh()); } /** * get the token array structure. * * @param string $token * * @return \illuminate\http\jsonresponse */ protected function respondwithtoken($token) { return response()->json([ 'access_token' => $token, 'token_type' => 'bearer', 'expires_in' => auth()->factory()->getttl() * 60 ]); }}
第九步. 使用postman测试api:
测试api数据获取,需要在headers中添加token; 格式
key=authorization,value=bearer空格token
token刷新:
以上就是laravel使用jwt实现api用户授权的详细步骤的详细内容。
