CheckIp.php 718 B

123456789101112131415161718192021222324252627
  1. <?php
  2. namespace app\common\middleware;
  3. use Exception;
  4. use app\admin\model\IpConfig;
  5. //验证Ip
  6. class CheckIp
  7. {
  8. public function handle($request, \Closure $next)
  9. {
  10. try {
  11. $ip = $request->ip();
  12. $whitelist = IpConfig::getWhitelist();
  13. if(!in_array($ip, $whitelist)){
  14. $blacklist = IpConfig::getBlacklist();
  15. if(in_array($ip, $blacklist)){
  16. return shutdown(lang('system.ipError'), 10002);
  17. }
  18. }
  19. } catch (Exception $exception) {
  20. return shutdown($errorMsgArr[$exception->getMessage()] ?? $exception->getMessage(), 10002);
  21. }
  22. return $next($request);
  23. }
  24. }