From 109ad233be4a9aeeed5edcd950ac32b63fa27408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Tue, 7 Dec 2021 10:57:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=91=E5=90=8D=E5=8D=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Exceptions/Handler.php | 47 +++++++++++-------- app/Helper/functions.php | 35 -------------- .../Http/Middleware/CheckIpBlacklist.php | 44 +++++++++++++++++ app/Modules/Admin/Routes/web.php | 2 + config/cnpscy.php | 1 + 5 files changed, 74 insertions(+), 55 deletions(-) create mode 100644 app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index e3e9edc..d3e7c0f 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -46,7 +46,6 @@ class Handler extends ExceptionHandler $this->reportable(function (Throwable $e) { }); - } /** @@ -60,30 +59,38 @@ class Handler extends ExceptionHandler */ public function render($request, Throwable $exception) { - // 路由404异常监听 - if($exception instanceof NotFoundHttpException){ - $this->setHttpCode(404); - return $this->errorJson("路由{{$request->path()}}不存在!"); - } + if ($request->isJson()){ + // 路由404异常监听 + if($exception instanceof NotFoundHttpException){ + $this->setHttpCode(404); + return $this->errorJson("路由{{$request->path()}}不存在!"); + } - // 模型不存在 - if ($exception instanceof ModelNotFoundException){ - return $this->errorJson($exception->getMessage()); - } + // 模型不存在 + if ($exception instanceof ModelNotFoundException){ + return $this->setJsonReturn($exception); + } - // 验证器类的错误监听 - if($exception instanceof ValidationException){ - return $this->errorJson($exception->validator->errors()->first()); - } + // 验证器类的错误监听 + if($exception instanceof ValidationException){ + return $this->errorJson($exception->validator->errors()->first()); + } - // Exception类的错误监听 - if($exception instanceof \Exception){ - return $this->errorJson($exception->getMessage(), $exception->getCode(), [], [ - 'file' => $exception->getFile(), - 'line' => $exception->getLine(), - ]); + // Exception类的错误监听 + if($exception instanceof \Exception){ + return $this->setJsonReturn($exception); + } } return parent::render($request, $exception); } + + private function setJsonReturn($exception) + { + $APP_DEBUG = env('APP_DEBUG'); + return $this->errorJson($exception->getMessage(), $exception->getCode(), [], $APP_DEBUG ? [ + 'file' => $exception->getFile(), + 'line' => $exception->getLine(), + ] : []); + } } diff --git a/app/Helper/functions.php b/app/Helper/functions.php index 6673d82..dac1e76 100644 --- a/app/Helper/functions.php +++ b/app/Helper/functions.php @@ -913,41 +913,6 @@ if ( !function_exists('config') ) { } } -if ( !function_exists('abort') ) { - /** - * Throw an HttpException with the given data. - * - * @param $code - * @param string $msg - * @param array $headers - * - * @return mixed - */ - function abort($code, $msg = '', array $headers = []) - { - return \Cnpscy\Embedded\Router::throwException($msg, $code); - exit; - //return http_response_code($code); - - try { - if ( $code instanceof Response ) { - throw new \Cnpscy\Exceptions\HttpResponseException($code); - } elseif ( $code instanceof Responsable ) { - throw new \Cnpscy\Exceptions\HttpResponseException($code->toResponse(request())); - } - if ( $code == 404 ) { - throw new \Cnpscy\Exceptions\NotFoundHttpException($msg); - } - throw new \Cnpscy\Exceptions\HttpException($code, $msg, null, $headers); - } catch (\Cnpscy\Exceptions\HttpExceptionInterface $e) { - http_response_code($e->getStatusCode()); - - \Cnpscy\Embedded\Response::new() - ->failMsg(\app\lib\code::EMAIL_NO_EXIST, $e->getMessage() ?? $msg); - } - } -} - if ( !function_exists('http_response_code') ) { function http_response_code($code = null) { diff --git a/app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php b/app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php new file mode 100644 index 0000000..bfd47b0 --- /dev/null +++ b/app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php @@ -0,0 +1,44 @@ +getClientIp(); + if (!$client_ip){ + $client_ip = get_ip(); + } + + // 获取黑名单组 + $ip_blacklists = cnpscy_config('ip_blacklists'); + if ($ip_blacklists){ + $ip_blacklists_array = explode(',', $ip_blacklists); + // 键值翻转,检测是否存在数组key + $ip_blacklists_array = array_flip($ip_blacklists_array); + if (isset($ip_blacklists_array[$client_ip])){ + $msg = '您的IP段在系统黑名单中,禁止访问!'; + abort(403, $msg); + } + } + + + return $next($request); + } +} diff --git a/app/Modules/Admin/Routes/web.php b/app/Modules/Admin/Routes/web.php index 035bb6c..455b2f6 100644 --- a/app/Modules/Admin/Routes/web.php +++ b/app/Modules/Admin/Routes/web.php @@ -15,8 +15,10 @@ use Illuminate\Support\Facades\Route; use App\Modules\Admin\Http\Middleware\CheckAuth; use App\Modules\Admin\Http\Middleware\CheckRabc; use App\Modules\Admin\Http\Middleware\AdminLog; +use App\Modules\Admin\Http\Middleware\CheckIpBlacklist; Route::prefix(cnpscy_config('admin_prefix')) + ->middleware(CheckIpBlacklist::class) ->group(function() { // Route::get('/', 'AdminController@index'); //后台管理路由 diff --git a/config/cnpscy.php b/config/cnpscy.php index 2199c15..263f5d3 100644 --- a/config/cnpscy.php +++ b/config/cnpscy.php @@ -8,6 +8,7 @@ 'START_API_LOGS' => '1', 'ARTICLE_CACHE_TIME' => '60', 'WEB_SITE_CLOSE' => '1', + 'ip_blacklists' => '', 'config_group_list' => array ( 0 => '不分组', -- Gitee