Files
EasyAdmin8/app/admin/middleware/SystemLog.php
wolfcode ed0e14cb32 fix(admin): use direct IP retrieval for system logs
Direct IP retrieval method has been updated to improve accuracy in the admin system log.
This change involves modifying the IP address retrieval logic from considering the
`HTTP_X_FORWARDED_FOR` header to directly using the `ip()` method, potentially enhancing
the security and reliability of IP logging.
2024-08-12 15:58:23 +08:00

82 lines
3.3 KiB
PHP

<?php
namespace app\admin\middleware;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnnotation;
use app\admin\service\SystemLogService;
use app\common\traits\JumpTrait;
use Closure;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\DocParser;
class SystemLog
{
use JumpTrait;
/**
* 敏感信息字段,日志记录时需要加密
* @var array
*/
protected array $sensitiveParams = [
'password',
'password_again',
'phone',
'mobile',
];
public function handle($request, Closure $next)
{
$params = $request->param();
if (isset($params['s'])) unset($params['s']);
foreach ($params as $key => $val) {
in_array($key, $this->sensitiveParams) && $params[$key] = "***********";
}
$method = strtolower($request->method());
$url = $request->url();
if (env('APP_DEBUG')) {
trace(['url' => $url, 'method' => $method, 'params' => $params,], 'requestDebugInfo');
}
$response = $next($request);
if ($request->isAjax()) {
if (in_array($method, ['post', 'put', 'delete'])) {
$title = '';
try {
$pathInfo = $request->pathinfo();
$pathInfoExp = explode('/', $pathInfo);
$_action = end($pathInfoExp) ?? '';
$pathInfoExp = explode('.', $pathInfoExp[0] ?? '');
$_name = $pathInfoExp[0] ?? '';
$_controller = ucfirst($pathInfoExp[1] ?? '');
if ($_name && $_controller && $_action) {
$className = "app\admin\controller\\{$_name}\\{$_controller}";
$reflectionClass = new \ReflectionClass($className);
$parser = new DocParser();
$parser->setIgnoreNotImportedAnnotations(true);
$reader = new AnnotationReader($parser);
$controllerAnnotation = $reader->getClassAnnotation($reflectionClass, ControllerAnnotation::class);
$reflectionAction = $reflectionClass->getMethod($_action);
$nodeAnnotation = $reader->getMethodAnnotation($reflectionAction, NodeAnnotation::class);
$title = $controllerAnnotation->title . ' - ' . $nodeAnnotation->title;
}
}catch (\Throwable $exception) {
}
$ip = $request->ip();
$data = [
'admin_id' => session('admin.id'),
'title' => $title,
'url' => $url,
'method' => $method,
'ip' => $ip,
'content' => json_encode($params, JSON_UNESCAPED_UNICODE),
'response' => json_encode($response->getData(), JSON_UNESCAPED_UNICODE),
'useragent' => $request->server('HTTP_USER_AGENT'),
'create_time' => time(),
];
SystemLogService::instance()->save($data);
}
}
return $response;
}
}