AuthServiceProvider.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Base\Providers;
  3. use App\Admin\Facades\AdminUserFacade;
  4. use App\Api\Facades\ApiFacade;
  5. use App\Base\Facades\ApiSysUserFacade;
  6. use Illuminate\Support\ServiceProvider;
  7. use App\Base\Exceptions\ApiException;
  8. class AuthServiceProvider extends ServiceProvider
  9. {
  10. /**
  11. * Register any application services.
  12. *
  13. * @return void
  14. */
  15. public function register()
  16. {
  17. }
  18. /**
  19. * Boot the authentication services for the application.
  20. *
  21. * @return void
  22. */
  23. public function boot()
  24. {
  25. // Here you may define how you wish users to be authenticated for your Lumen
  26. // application. The callback which receives the incoming request instance
  27. // should return either a User instance or null. You're free to obtain
  28. // the User instance via an API token or any other method necessary.
  29. //处理swoole框架中带来的认证重复问题
  30. $auth = app('auth');
  31. $reflClass = new \ReflectionClass($auth);
  32. $reflProp = $reflClass->getProperty('guards');
  33. $reflProp->setAccessible(true);
  34. $reflProp->setValue($auth, []);
  35. $reflProp = $reflClass->getProperty('customCreators');
  36. $reflProp->setAccessible(true);
  37. $reflProp->setValue($auth, []);
  38. $this->app['auth']->viaRequest('api', function ($request) {
  39. // checkRedisPing();
  40. $token = $request->header('token');
  41. if (empty($token)) {
  42. $token = $request->input('token');
  43. }
  44. $apiToken = $request->header('api_token');
  45. if(empty($apiToken)){
  46. $apiToken = $request->input('api_token');
  47. }
  48. if ($apiToken) {
  49. $data = ApiFacade::findUserInfoByApiToken($apiToken);
  50. if (!empty($data)) {
  51. //1天做一次缓存更新
  52. $time=time();
  53. $heartBeatTime= config('cache.heart_beat_time');//心跳时间
  54. if (!empty($data['heart_beat_time'])&&($data['heart_beat_time']+$heartBeatTime) <$time) {
  55. $res= ApiSysUserFacade::heartBeat($data['sys_api_token']);
  56. if(!$res){
  57. ApiFacade::forgotToken($apiToken);
  58. throw new ApiException('common.auth_fail', '认证失败');
  59. }
  60. $data['heart_beat_time']=$time;
  61. }
  62. $data = ApiFacade::updateUserInfoCache($apiToken, $data);
  63. return $data;
  64. }
  65. } else if ($token) {
  66. $data = AdminUserFacade::getTokenData($token);
  67. if (!empty($data)) {
  68. $expires = $data['expiration_time'];
  69. //token时间是否过期
  70. if ($expires && time() + (30 * 60) > $expires) {
  71. //快过期时,自动延长
  72. $cacheTokenTimeMinute = config('cache.token');
  73. $nowTime = time();
  74. $expiration_time = $nowTime + $cacheTokenTimeMinute;
  75. $userInfo['expiration_time'] = $expiration_time;//过期时间
  76. AdminUserFacade::setToken($token, $data);
  77. }
  78. }
  79. return $data;
  80. }
  81. return null;
  82. });
  83. }
  84. }