IpCheckMiddleware.php 815 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-03-24
  7. *
  8. *
  9. */
  10. namespace app\common\middleware;
  11. use app\Request;
  12. use think\exception\ValidateException;
  13. use think\facade\Log;
  14. use think\Response;
  15. class IpCheckMiddleware extends BaseMiddleware
  16. {
  17. //地址列表,生产环境中通常会在redis中
  18. private $ipList = ['39.105.36.78','39.106.2.244'];
  19. public function before(Request $request)
  20. {
  21. //得到当前IP
  22. // $ip = $request->ip();
  23. $ip = $request->header('x-forwarded-for');
  24. Log::info("IP".$ip);
  25. //判断是否在列表中
  26. if(!in_array($ip,$this->ipList)){
  27. throw new ValidateException('IP非法23');
  28. }
  29. }
  30. public function after(Response $response)
  31. {
  32. // TODO: Implement after() method.
  33. }
  34. }