container = $container; $this->response = $response; $this->request = $request; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { //日志统一写入 LogUtil::getInstance(self::PROJECT . "/");//设置日志存入通道 Coroutine::defer(function () { LogUtil::close();// 协程结束后统一写入 }); $this->action = $action = ActionUtil::actions($request,self::PROJECT); $params = $this->request->all(); // 记录用户请求参数 LogUtil::info('请求参数', $action['controller'], $action['action'], $params); //接口限流,写到中间件中 if (!RedisUtil::getInstance(RedisKeyEnum::API_REQUEST_TRAFFIC)->requestLimit("{$action['controller']}/{$action['action']}", 1, 10)) { LogUtil::info('请求次数过多', $action['controller'], $action['action']); return $this->response206(); } $token = $this->request->header('token'); if (empty($token)) $token = $params['token'] ?? ''; if (!empty($token) && !in_array("{$action['controller']}/{$action['action']}", self::tokenWhiteList())) { LogUtil::info('token 验证开始', self::LOG_MODULE, self::LOG_ACTION, (string)$token); // 校验token if (!$driver_id = $this->checkToken($token)){ return $this->response401(); } // 查询并记录用户信息 $user = (new DriverModel())->authUserInfo($driver_id); if (!$user) { LogUtil::warning('账号不存在', self::LOG_MODULE, self::LOG_ACTION, ['driver_id', $driver_id]); return $this->response401(); } LogUtil::info('用户编号', $action['controller'], $action['action'], $driver_id); AuthUser::getInstance()->set(json_decode(json_encode($user), true)); } return $handler->handle($request); } /** * 校验token * * @param string $token * @return false|int */ public function checkToken(string $token): false|int { //方便测试 if (strpos($token, 'testuid_') !== false && config('app_debug',false)) { $uid = substr($token, 8); return intval($uid); } //方便测试 $checkToken = TokenUtil::verifyToken($token); if (!$checkToken) { LogUtil::warning('token 验证失败', self::LOG_MODULE, self::LOG_ACTION, $checkToken); return false; } //签发时间大于当前服务器时间验证失败 if (!isset($checkToken['iat']) || $checkToken['iat'] > time()) { LogUtil::warning('token 未生效', self::LOG_MODULE, self::LOG_ACTION, $checkToken); return false; } //过期时间小于当前服务器时间验证失败 if (!isset($checkToken['exp']) || $checkToken['exp'] < time()) { LogUtil::warning('token 已失效', self::LOG_MODULE, self::LOG_ACTION, $checkToken); return false; } //验证参数信息 if (empty($checkToken['data']['driver_id'])) { LogUtil::warning('token 参数不全', self::LOG_MODULE, self::LOG_ACTION, $checkToken); return false; } return $checkToken['data']['driver_id']; } /** * 令牌失效 * * @param string $message * @param $result * @return ResponseInterface */ private function response401(string $message = '令牌失效,请稍后重试!', $result = null): ResponseInterface { // 记录令牌校验 $action = $this->action; LogUtil::info($message, $action['controller'], $action['action'], $result); return AppResult::response401($message, $result); } /** * 请求频繁 * * @param string $message * @param $result * @return ResponseInterface */ private function response206(string $message = '当前访问人数过多,请稍后再试!', $result = null): ResponseInterface { // 记录令牌校验 $action = $this->action; LogUtil::info($message, $action['controller'], $action['action'], $result); return AppResult::response206($message, $result); } /** * token校验黑名单 * @return string[] */ public function tokenWhiteList(): array { return [ 'v1/Common/PassportController/login', 'v1/Common/PassportController/wxLogin', ]; } }