auth.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of qbhy/hyperf-auth.
  5. *
  6. * @link https://github.com/qbhy/hyperf-auth
  7. * @document https://github.com/qbhy/hyperf-auth/blob/master/README.md
  8. * @contact qbhy0715@qq.com
  9. * @license https://github.com/qbhy/hyperf-auth/blob/master/LICENSE
  10. */
  11. use Qbhy\SimpleJwt\Encoders;
  12. use Qbhy\SimpleJwt\EncryptAdapters as Encrypter;
  13. use function Hyperf\Support\env;
  14. use function Hyperf\Support\make;
  15. return [
  16. 'default' => [
  17. 'guard' => 'jwt',
  18. 'provider' => 'users',
  19. ],
  20. 'guards' => [
  21. 'sso' => [
  22. // 支持的设备,env配置时用英文逗号隔开
  23. 'clients' => explode(',', env('AUTH_SSO_CLIENTS', 'pc')),
  24. // hyperf/redis 实例
  25. 'redis' => function () {
  26. return make(\Hyperf\Redis\Redis::class);
  27. },
  28. // 自定义 redis key,必须包含 {uid},{uid} 会被替换成用户ID
  29. 'redis_key' => 'u:token:{uid}',
  30. 'driver' => Qbhy\HyperfAuth\Guard\SsoGuard::class,
  31. 'provider' => 'users',
  32. /*
  33. * 以下是 simple-jwt 配置
  34. * 必填
  35. * jwt 服务端身份标识
  36. */
  37. 'secret' => env('SSO_JWT_SECRET'),
  38. /*
  39. * 可选配置
  40. * jwt 默认头部token使用的字段
  41. */
  42. 'header_name' => env('JWT_HEADER_NAME', 'Authorization'),
  43. /*
  44. * 可选配置
  45. * jwt 生命周期,单位秒,默认一天
  46. */
  47. 'ttl' => (int) env('SIMPLE_JWT_TTL', 60 * 60 * 24),
  48. /*
  49. * 可选配置
  50. * 允许过期多久以内的 token 进行刷新,单位秒,默认一周
  51. */
  52. 'refresh_ttl' => (int) env('SIMPLE_JWT_REFRESH_TTL', 60 * 60 * 24 * 7),
  53. /*
  54. * 可选配置
  55. * 默认使用的加密类
  56. */
  57. 'default' => Encrypter\SHA1Encrypter::class,
  58. /*
  59. * 可选配置
  60. * 加密类必须实现 Qbhy\SimpleJwt\Interfaces\Encrypter 接口
  61. */
  62. 'drivers' => [
  63. Encrypter\PasswordHashEncrypter::alg() => Encrypter\PasswordHashEncrypter::class,
  64. Encrypter\CryptEncrypter::alg() => Encrypter\CryptEncrypter::class,
  65. Encrypter\SHA1Encrypter::alg() => Encrypter\SHA1Encrypter::class,
  66. Encrypter\Md5Encrypter::alg() => Encrypter\Md5Encrypter::class,
  67. ],
  68. /*
  69. * 可选配置
  70. * 编码类
  71. */
  72. 'encoder' => new Encoders\Base64UrlSafeEncoder(),
  73. // 'encoder' => new Encoders\Base64Encoder(),
  74. /*
  75. * 可选配置
  76. * 缓存类
  77. */
  78. 'cache' => new \Doctrine\Common\Cache\FilesystemCache(sys_get_temp_dir()),
  79. // 如果需要分布式部署,请选择 redis 或者其他支持分布式的缓存驱动
  80. // 'cache' => function () {
  81. // return make(\Qbhy\HyperfAuth\HyperfRedisCache::class);
  82. // },
  83. /*
  84. * 可选配置
  85. * 缓存前缀
  86. */
  87. 'prefix' => env('SIMPLE_JWT_PREFIX', 'default'),
  88. ],
  89. 'jwt' => [
  90. 'driver' => Qbhy\HyperfAuth\Guard\JwtGuard::class,
  91. 'provider' => 'users',
  92. /*
  93. * 以下是 simple-jwt 配置
  94. * 必填
  95. * jwt 服务端身份标识
  96. */
  97. 'secret' => env('SIMPLE_JWT_SECRET'),
  98. /*
  99. * 可选配置
  100. * jwt 默认头部token使用的字段
  101. */
  102. 'header_name' => env('JWT_HEADER_NAME', 'Authorization'),
  103. /*
  104. * 可选配置
  105. * jwt 生命周期,单位秒,默认一天
  106. */
  107. 'ttl' => (int) env('SIMPLE_JWT_TTL', 60 * 60 * 24),
  108. /*
  109. * 可选配置
  110. * 允许过期多久以内的 token 进行刷新,单位秒,默认一周
  111. */
  112. 'refresh_ttl' => (int) env('SIMPLE_JWT_REFRESH_TTL', 60 * 60 * 24 * 7),
  113. /*
  114. * 可选配置
  115. * 默认使用的加密类
  116. */
  117. 'default' => Encrypter\SHA1Encrypter::class,
  118. /*
  119. * 可选配置
  120. * 加密类必须实现 Qbhy\SimpleJwt\Interfaces\Encrypter 接口
  121. */
  122. 'drivers' => [
  123. Encrypter\PasswordHashEncrypter::alg() => Encrypter\PasswordHashEncrypter::class,
  124. Encrypter\CryptEncrypter::alg() => Encrypter\CryptEncrypter::class,
  125. Encrypter\SHA1Encrypter::alg() => Encrypter\SHA1Encrypter::class,
  126. Encrypter\Md5Encrypter::alg() => Encrypter\Md5Encrypter::class,
  127. ],
  128. /*
  129. * 可选配置
  130. * 编码类
  131. */
  132. 'encoder' => new Encoders\Base64UrlSafeEncoder(),
  133. // 'encoder' => new Encoders\Base64Encoder(),
  134. /*
  135. * 可选配置
  136. * 缓存类
  137. */
  138. 'cache' => new \Doctrine\Common\Cache\FilesystemCache(sys_get_temp_dir()),
  139. // 如果需要分布式部署,请选择 redis 或者其他支持分布式的缓存驱动
  140. // 'cache' => function () {
  141. // return make(\Qbhy\HyperfAuth\HyperfRedisCache::class);
  142. // },
  143. /*
  144. * 可选配置
  145. * 缓存前缀
  146. */
  147. 'prefix' => env('SIMPLE_JWT_PREFIX', 'default'),
  148. ],
  149. 'session' => [
  150. 'driver' => Qbhy\HyperfAuth\Guard\SessionGuard::class,
  151. 'provider' => 'users',
  152. ],
  153. ],
  154. 'providers' => [
  155. 'users' => [
  156. 'driver' => \Qbhy\HyperfAuth\Provider\EloquentProvider::class,
  157. 'model' => App\Model\User::class, // 需要实现 Qbhy\HyperfAuth\Authenticatable 接口
  158. ],
  159. ],
  160. ];