AuthMiddleware.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\middleware;
  4. use app\service\MenuService;
  5. use Firebase\JWT\JWT;
  6. use Firebase\JWT\Key;
  7. use think\facade\Config;
  8. use think\Response;
  9. class AuthMiddleware
  10. {
  11. /**
  12. * 处理请求
  13. */
  14. public function handle($request, \Closure $next)
  15. {
  16. // 获取当前控制器和操作
  17. $controller = $request->controller();
  18. $action = $request->action();
  19. // 检查是否在登录白名单中(不需要登录验证)
  20. if ($this->isLoginWhitelisted($controller, $action)) {
  21. return $next($request);
  22. }
  23. // 验证登录状态
  24. $userInfo = $this->checkLogin($request);
  25. if (!$userInfo) {
  26. return json_error([], '请先登录', 401);
  27. }
  28. // 将用户信息注入请求
  29. $request->userInfo = $userInfo;
  30. $request->userId = (int)$userInfo['user_id'];
  31. // 检查是否在权限白名单中(需要登录但不需要权限验证)
  32. if ($this->isPermissionWhitelisted($controller, $action)) {
  33. return $next($request);
  34. }
  35. // 验证权限
  36. if (!$this->checkPermission((int)$userInfo['user_id'], $controller, $action)) {
  37. return json_error([], '无权限访问', 403);
  38. }
  39. return $next($request);
  40. }
  41. /**
  42. * 检查登录状态
  43. */
  44. protected function checkLogin($request): ?array
  45. {
  46. $token = $request->cookie('auth_token');
  47. if (!$token) {
  48. return null;
  49. }
  50. $decoded = parseToken($token);
  51. if (!$decoded) {
  52. return null;
  53. }
  54. return $decoded;
  55. }
  56. /**
  57. * 检查权限
  58. */
  59. protected function checkPermission(int $userId, string $controller, string $action): bool
  60. {
  61. // 使用MenuService检查权限
  62. return MenuService::checkPermission($userId, $controller, $action);
  63. }
  64. /**
  65. * 检查是否在登录白名单中
  66. */
  67. protected function isLoginWhitelisted(string $controller, string $action): bool
  68. {
  69. $whitelist = Config::get('menu.login_whitelist', []);
  70. // 检查控制器级别白名单
  71. $controllers = $whitelist['controllers'] ?? [];
  72. if (in_array($controller, $controllers)) {
  73. return true;
  74. }
  75. // 检查具体方法白名单
  76. $actions = $whitelist['actions'] ?? [];
  77. $current = $controller . '/' . $action;
  78. if (in_array($current, $actions)) {
  79. return true;
  80. }
  81. return false;
  82. }
  83. /**
  84. * 检查是否在权限白名单中
  85. */
  86. protected function isPermissionWhitelisted(string $controller, string $action): bool
  87. {
  88. $whitelist = Config::get('menu.permission_whitelist', []);
  89. // 检查控制器级别白名单
  90. $controllers = $whitelist['controllers'] ?? [];
  91. if (in_array($controller, $controllers)) {
  92. return true;
  93. }
  94. // 检查具体方法白名单
  95. $actions = $whitelist['actions'] ?? [];
  96. $current = $controller . '/' . $action;
  97. if (in_array($current, $actions)) {
  98. return true;
  99. }
  100. return false;
  101. }
  102. }