大飞_dafei

导航

laravel--[ tymon/jwt-auth ] && 中间件

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

php artisan jwt:secret

CREATE TABLE `admin` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

生成admin  model
php artisan infyom:model Admin
然后修改这个model
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable implements JWTSubject{  }  //具体代码参考jwt-auth官方

02) 配置 config/auth.php

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
    'guards' => [
        'web' => [ //web端登录验证, auth('参数')->attempt() 不指定参数,默认使用web
            'driver' => 'session',
            'provider' => 'users',
        ],
        'admin' => [  //后面接口登录验证会用到
            'driver' => 'jwt',
            'provider' => 'feis',
        ],
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'feis' => [
            'driver' => 'eloquent',
            'model' => App\Models\Admin::class
        ],
    ],
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

 

 03) 修改 routes/api.php

Route::group([
    // 'middleware' => 'api',
    'middleware' => 'auth:admin', //这里配置后在controller 中不在配置
    'prefix' => "auth"
], function () {
    Route::get('login', 'AuthController@login');
    // Route::get('login', function () {  //http://demo.laravel.com/api/auth/login
    //     return 'API: hello world';
    // });
    // Route::post('logout', 'AuthController@logout');
    // Route::post('refresh', 'AuthController@refresh');
    Route::get('me', 'AuthController@me');
});

04) 创建 AuthController 

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
class AuthController extends Controller
{
    public function __construct()
    {
        //api.php 中使用了middleware,这里就不在使用
        $this->middleware('auth:admin', ['except' => ['login']]);
    }

    public function login()
    {
        // atuh()  中的参数默认使用auth.php  'guards' => ['web' => ['driver' => 'session', 'provider' => 'users']]
        // $webIsLogin = auth('web')->attempt(['email' => "dafei02@qq.com", 'password' => '123456']);
        $webIsLogin = auth()->attempt(['email' => "dafei02@qq.com", 'password' => '123456']);
        $adminToken = auth('admin')->attempt(['email' => 'dafei_01@qq.com', 'password' => '123456']);
        dd($webIsLogin,$adminToken);

    }

    public function me()
    {
        exit('me');
    }
}

输出结果.如图

 

tymon/jwt-auth 官方地址

Docs

 

laravel官网 中间件

laravel官网 登录认证--自定义Guard

JWT 官方-出处

 

posted on 2020-10-14 09:33  大飞_dafei  阅读(604)  评论(0编辑  收藏  举报