isSecurity.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Components\IP;
  4. use Cache;
  5. use Closure;
  6. use Log;
  7. use Response;
  8. class isSecurity
  9. {
  10. /**
  11. * 是否需要安全码才访问(仅用于登录页).
  12. *
  13. * @param $request
  14. * @param Closure $next
  15. *
  16. * @return mixed
  17. */
  18. public function handle($request, Closure $next)
  19. {
  20. $ip = IP::getClientIP();
  21. $code = $request->securityCode;
  22. $cacheKey = 'SecurityLogin_'.ip2long($ip);
  23. $websiteSecurityCode = sysConfig('website_security_code');
  24. if ($websiteSecurityCode && ! Cache::has($cacheKey)) {
  25. if ($code !== $websiteSecurityCode) {
  26. Log::info(trans('error.unsafe_enter').$ip);
  27. return Response::view(
  28. 'auth.error',
  29. [
  30. 'message' => trans('error.unsafe_enter').','.trans('error.visit').'<a href="'.route('login').'?securityCode=" target="_self">'.trans('error.safe_enter').'</a>',
  31. ],
  32. 403
  33. );
  34. }
  35. Cache::put($cacheKey, $ip, 7200); // 2小时之内无需再次输入安全码访问
  36. }
  37. return $next($request);
  38. }
  39. }