标签:boot out 设置 load emc cep 本地 sch 无法
composer create-project laravel/lumen jwt-test --prefer-dist
composer require tymon/jwt-auth ^1.0.0-rc.2
3.1.首先模拟 Laravel 目录结构,复制‘vender/laravel/lumen-framework‘下的 ‘config 目录到 ‘jwt-test‘ 根路径。复制完成以后 ‘jwt-test‘ 的根目录结构如下:
/app
......others.......
/config <<<<<< 配置文件目录
/vendor
......others.......
3.2. 以后的配置文件,都只需要在根路径下的 ‘config目录操作即可,所以接着修改目录中的 cache.php 文件:
# Dir: /jwt-test/config/cache.php
<?php
return [
#### 修改为文件缓存
‘default‘ => env(‘CACHE_DRIVER‘, ‘file‘),
#### 同时删除了下面的Memcached配置
‘stores‘ => [
‘apc‘ => [
‘driver‘ => ‘apc‘,
],
‘array‘ => [
‘driver‘ => ‘array‘,
],
‘database‘ => [
‘driver‘ => ‘database‘,
‘table‘ => env(‘CACHE_DATABASE_TABLE‘, ‘cache‘),
‘connection‘ => env(‘CACHE_DATABASE_CONNECTION‘, ‘mysql_a‘),
],
‘file‘ => [
‘driver‘ => ‘file‘,
‘path‘ => storage_path(‘framework/cache‘),
],
‘redis‘ => [
‘driver‘ => ‘redis‘,
‘connection‘ => env(‘CACHE_REDIS_CONNECTION‘, ‘cache‘),
]
],
‘prefix‘ => env(‘CACHE_PREFIX‘, ‘wz‘),
];
同时修改根路径下的 ‘.env 文件:
# Dir: /jwt-test/.env
......others.......
APP_KEY=9TBF8FrZZgYBoM0AzKjkii/yb6TJVm11 #### Lumen默认没有设置APP_KEY
CACHE_DRIVER=file #### 修改为文件缓存
......others (包括MySQL的配置项) .......
JWT_SECRET=Bi43uQQTHxLSnUaIOgTEUT1SkGHiOc1o #### JWT编码时需要的Key
本文使用以下指令快速启动服务。
# Dir: /jwt-test/
php -S localhost:8080 public/index.php
......others.......
/app
..........others.......
..../Models <<<<<< 模型文件目录
/config <<<<<< 配置文件目录
/vendor
......others.......
5.1 修改 ‘bootstrap‘ 文件夹下的 ‘app.php‘ 如下所示:
<?php
require_once __DIR__.‘/../vendor/autoload.php‘;
try {
(new Dotenv\Dotenv(__DIR__.‘/../‘))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
//
}
$app = new Laravel\Lumen\Application(
realpath(__DIR__.‘/../‘)
);
// 取消注释
$app->withFacades();
$app->withEloquent();
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
// 取消注释
$app->routeMiddleware([
‘auth‘ => App\Http\Middleware\Authenticate::class,
]);
// 取消注释
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
// 新增JWT的注册
$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
$app->router->group([
‘namespace‘ => ‘App\Http\Controllers‘,
], function ($router) {
require __DIR__.‘/../routes/web.php‘;
});
return $app;
5.2. 修改 ‘config‘ 文件夹下的 ‘auth.php‘ 如下所示:
<?php
return [
‘defaults‘ => [
‘guard‘ => ‘api‘,
‘passwords‘ => ‘users‘,
],
‘guards‘ => [
‘api‘ => [
‘driver‘ => ‘jwt‘, #### 更改为JWT驱动
‘provider‘ => ‘users‘,
],
],
‘providers‘ => [
‘users‘ => [
‘driver‘ => ‘eloquent‘,
‘model‘ => \App\Models\User::class, #### 指定用于token验证的模型类
],
],
‘passwords‘ => [ #### Lumen默认无session,所以该字段无意义
//
],
];
5.3. 修改 ‘app/Providers‘ 文件夹下的 ‘AuthServiceProvider.php‘ 如下所示:
<?php
namespace App\Providers;
use App\Models\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Boot the authentication services for the application.
*
* @return void
*/
public function boot()
{
// 当使用auth中间件的api门卫的时候验证请求体
$this->app[‘auth‘]->viaRequest(‘api‘, function ($request)
{
return app(‘auth‘)->setRequest($request)->user();
});
}
}
5.4. 修改 ‘app/Models‘ 文件夹下的 ‘User.php‘ 如下所示:
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Laravel\Lumen\Auth\Authorizable;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
{
use Authenticatable, Authorizable;
protected $table = ‘users‘;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
‘username‘, ‘email‘,
];
/**
* The attributes excluded from the model‘s JSON form.
*
* @var array
*/
protected $hidden = [
‘password‘,
];
/**
* JWT
*
* @author AdamTyn
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* JWT
*
* @author AdamTyn
*/
public function getJWTCustomClaims()
{
return [];
}
}
5.5. 在 ‘app/Http/Controller‘ 文件夹下新建 ‘UserController.php‘,内容如下所示:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class AuthController extends Controller
{
/**
* 登录
*
* @author AdamTyn
*
* @param \Illuminate\Http\Request;
* @return \Illuminate\Http\Response;
*/
public function login(Request $request)
{
$response = array(‘code‘ => ‘0‘);
try {
$user = \App\Models\User::where(‘username‘, $request->input(‘username‘))
->where(‘password‘, $request->input(‘password‘))->first();
if (!$token = Auth::login($user)) {
$response[‘code‘] = ‘5000‘;
$response[‘errorMsg‘] = ‘系统错误,无法生成令牌‘;
} else {
$response[‘data‘][‘user_id‘] = strval($user->id);
$response[‘data‘][‘access_token‘] = $token;
$response[‘data‘][‘expires_in‘] = strval(time() + 86400);
}
} catch (QueryException $queryException) {
$response[‘code‘] = ‘5002‘;
$response[‘msg‘] = ‘无法响应请求,服务端异常‘;
}
return response()->json($response);
}
/**
* 用户登出
*
* @author AdamTyn
*
* @return \Illuminate\Http\Response;
*/
public function logout()
{
$response = array(‘code‘ => ‘0‘);
Auth::invalidate(true);
return response()->json($response);
}
/**
* 更新用户Token
*
* @author AdamTyn
*
* @param \Illuminate\Http\Request;
* @return \Illuminate\Http\Response;
*/
public function refreshToken()
{
$response = array(‘code‘ => ‘0‘);