AuthServiceProvider.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Providers;
  3. use App\Common\Facades\UserCenterFacade;
  4. use App\Exceptions\ApiException;
  5. use App\User;
  6. use App\User\Facades\SysAdminUserFacade;
  7. use App\User\Facades\UserFacade;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\ServiceProvider;
  10. class AuthServiceProvider extends ServiceProvider
  11. {
  12. /**
  13. * Register any application services.
  14. *
  15. * @return void
  16. */
  17. public function register()
  18. {
  19. //配合 auth providers driver
  20. /* $this->app['auth']->provider('custom_database', function() {
  21. return new AppUserProvider(DB::connection(), $this->app['hash'], 'user');
  22. });*/
  23. }
  24. /**
  25. * Boot the authentication services for the application.
  26. *
  27. * @return void
  28. */
  29. public function boot()
  30. {
  31. $auth = app('auth');
  32. $reflClass = new \ReflectionClass($auth);
  33. $reflProp = $reflClass->getProperty('guards');
  34. $reflProp->setAccessible(true);
  35. $reflProp->setValue($auth, []);
  36. $reflProp = $reflClass->getProperty('customCreators');
  37. $reflProp->setAccessible(true);
  38. $reflProp->setValue($auth, []);
  39. $this->app['auth']->viaRequest('api', function ($request) {
  40. checkRedisPing();
  41. $token = $request->header('token');
  42. $token_update_time= config('cache.token_update_time');
  43. if(empty($token)){
  44. $token = $request->input('token');
  45. }
  46. if ($token) {
  47. $data = SysAdminUserFacade::findOneByToken($token);
  48. if (!empty($data)) {
  49. //超过半小时 发送heartbeat
  50. $time=time();
  51. if ($data['expiration_time'] <($time + $token_update_time)) {
  52. $data = SysAdminUserFacade::updateUserInfoCache($token, $data);
  53. }
  54. return $data;
  55. }
  56. }
  57. return null;
  58. });
  59. }
  60. }