isSecurity.php 942 B

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