a6f2284f51
# Conflicts: # app/common/controller/AuthController.php # app/common/logic/AuthLogic.php # app/common/model/User.php # app/controller/IndexController.php # app/functions.php # app/middleware/AuthCheck.php # app/view/index/qrcode.html # composer.json # composer.lock # config/middleware.php # config/route.php # config/whitelist.php
46 lines
1.6 KiB
PHP
46 lines
1.6 KiB
PHP
<?php
|
|
namespace app\middleware;
|
|
|
|
use app\common\utils\Jwt;
|
|
use ReflectionClass;
|
|
use Webman\MiddlewareInterface;
|
|
use Webman\Http\Response;
|
|
use Webman\Http\Request;
|
|
|
|
class AuthCheck implements MiddlewareInterface
|
|
{
|
|
public function process(Request $request, callable $handler) : Response
|
|
{
|
|
$controller = new ReflectionClass($request->controller);
|
|
$noNeedLogin = $controller->getDefaultProperties()['noNeedLogin'] ?? [];
|
|
|
|
if (in_array($request->action, $noNeedLogin)) {
|
|
// 白名单
|
|
return $handler($request);
|
|
} else {
|
|
try {
|
|
$authHeader = $request->header('Fdesk-Auth-Token');
|
|
if (!$authHeader || !preg_match('/Bearer\s(\S+)/', $authHeader, $matches)) {
|
|
$body = [
|
|
'status' => 'fail',
|
|
'code' => 401,
|
|
'message' => '未授权',
|
|
'data' => [],
|
|
];
|
|
return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']);
|
|
}
|
|
$token = $matches[1];
|
|
$request->user = (new Jwt())->verifyToken($token);
|
|
} catch (\Throwable $e) {
|
|
$body = [
|
|
'status' => 'fail',
|
|
'code' => 401,
|
|
'message' => $e->getMessage(),
|
|
'data' => [],
|
|
];
|
|
return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']);
|
|
}
|
|
return $handler($request);
|
|
}
|
|
}
|
|
} |