isSecurity.php 869 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Utils\IP;
  4. use Cache;
  5. use Closure;
  6. use Log;
  7. use Response;
  8. class isSecurity
  9. {
  10. /**
  11. * 是否需要安全码才访问(仅用于登录页).
  12. *
  13. * @return mixed
  14. */
  15. public function handle($request, Closure $next)
  16. {
  17. $ip = IP::getClientIP();
  18. $code = $request->securityCode;
  19. $cacheKey = 'SecurityLogin_'.ip2long($ip);
  20. $websiteSecurityCode = sysConfig('website_security_code');
  21. if ($websiteSecurityCode && ! Cache::has($cacheKey)) {
  22. if ($code !== $websiteSecurityCode) {
  23. Log::warning(trans('errors.unsafe_enter').$ip);
  24. return Response::view('auth.safe');
  25. }
  26. Cache::put($cacheKey, $ip, 7200); // 2小时之内无需再次输入安全码访问
  27. }
  28. return $next($request);
  29. }
  30. }