生成安装中间命令:php artisan make:middleware 中间件名称
php artisan make:middleware RoutingApiMiddleware
路经 :中间件控制器生成目录位置 App\Http\Middleware的中间件名称控制器.php
<?php
namespace App\Http\Middleware;
use App\server\TokenServer;
use Closure;
class RoutingApiMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
//第一步先取token
$token=$request->header('token');
//jwt 进行校验token
$res=(new TokenServer())->chekToken($token);
if ($res['code']!=1){
return json_encode(['error_code'=>999,'msg'=>$res['msg'],'data'=>''],400);
}
$request->uid=$res['data']->uid;
//如果没有停止则向后执行
return $next($request);
}
}
配置 路由中间件
路经 :配置路由中间件目录位置 App\Http\Kernel.php下的
给路由分组绑定
api路由
//路由分组 频率限制
Route::group(['middleware'=>'throttle:100'],function (){
//发送验证码
Route::post('SendVerificationCode','LoginController@SendVerificationCode');
//用户绑定手机号或验证登录
Route::post('userBindPhoneLogin','LoginController@userBindPhoneLogin');
// JWT中间件token安全校验
Route::group(['middleware'=>['RoutingMiddleware']],function (){
//获取好友列表信息
//获取关注列表信息
Route::get('getFocusList','FocusController@getFocusList');
//发布信息
Route::post('addFocusList','FocusController@addFocusList');
});
});
|