69 lines
2.2 KiB
PHP
69 lines
2.2 KiB
PHP
<?php
|
|
namespace app\middleware;
|
|
|
|
use support\Log;
|
|
use Webman\MiddlewareInterface;
|
|
use Webman\Http\Response;
|
|
use Webman\Http\Request;
|
|
|
|
class RequestLog implements MiddlewareInterface
|
|
{
|
|
/**
|
|
* 需要中间件拦截的控制器清单,全部小写
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $controllerScope = [
|
|
'app\controller\indexcontroller',
|
|
'app\controller\testcontroller',
|
|
];
|
|
|
|
/**
|
|
* 在拦截的控制器中不需要被拦截的例外方法,如用户登录
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $actionWhiteList = [
|
|
'app\controller\testcontroller\index', // oss里的download不校验
|
|
];
|
|
|
|
public function process(Request $request, callable $next) : Response
|
|
{
|
|
$controller = strtolower($request->controller);
|
|
$action = strtolower($request->action);
|
|
$path = "{$controller}\\{$action}"; // 完整的控制器\方法字符串
|
|
// 白名单方法的请求不拦截
|
|
if (in_array($path, $this->actionWhiteList)) {
|
|
return $next($request);
|
|
}
|
|
// 只拦截指定控制器的请求
|
|
if (!in_array($controller, $this->controllerScope)) {
|
|
return $next($request);
|
|
}
|
|
$info = [
|
|
'session_id' => $request->sessionId(),
|
|
'protocol_version' => $request->protocolVersion(),
|
|
'method' => $request->method(),
|
|
'ip' => $request->getRemoteIp(),
|
|
'real_ip' => $request->getRealIp(),
|
|
'host' => $request->host(),
|
|
'path' => $request->path(),
|
|
'uri' => $request->uri(),
|
|
'url' => $request->url(),
|
|
'full_url' => $request->fullUrl(),
|
|
'query_string' => $request->queryString(),
|
|
'get_params' => $request->get(),
|
|
'post_params' => $request->post(),
|
|
'local_ip' => $request->getLocalIp(),
|
|
'local_port' => $request->getLocalPort(),
|
|
'is_ajax' => $request->isAjax(),
|
|
'is_pjax' => $request->isPjax(),
|
|
'expects_json' => $request->expectsJson(),
|
|
'accept_json' => $request->acceptJson(),
|
|
];
|
|
Log::channel('request')->info('', $info);
|
|
return $next($request);
|
|
}
|
|
|
|
}
|