欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

laravel--新建模型--实现前台用户登录验证

程序员文章站 2022-05-16 21:37:05
...

学习总结

1.新建模型后,如果需要使用Auth类进行登录验证,需要在config文件夹中的auth.php文件进行门卫注册
2.在使用Auth类进行登录验证时,需要使用自定义的门卫进行验证$isLogin = Auth::guard('member')->attempt(['phone'=>$phone,'password'=>$password])

1.新建一个模型Member.php

在app目录下新建一个member.php的模型文件
laravel--新建模型--实现前台用户登录验证

  1. <?php
  2. namespace App;
  3. use Illuminate\Contracts\Auth\MustVerifyEmail;
  4. use Illuminate\Foundation\Auth\User as Authenticatable;
  5. use Illuminate\Notifications\Notifiable;
  6. class Member extends Authenticatable
  7. {
  8. use Notifiable;
  9. //指定手动用户验证的表名
  10. protected $table = 'member';
  11. /**
  12. * The attributes that are mass assignable.
  13. *
  14. * @var array
  15. */
  16. protected $fillable = [
  17. 'name', 'email', 'password',
  18. ];
  19. /**
  20. * The attributes that should be hidden for arrays.
  21. *
  22. * @var array
  23. */
  24. protected $hidden = [
  25. 'password', 'remember_token',
  26. ];
  27. /**
  28. * The attributes that should be cast to native types.
  29. *
  30. * @var array
  31. */
  32. protected $casts = [
  33. 'email_verified_at' => 'datetime',
  34. ];
  35. }

2.在config文件夹中的auth.php中注册新建的模型文件

laravel--新建模型--实现前台用户登录验证

  1. <?php
  2. return [
  3. /*
  4. |--------------------------------------------------------------------------
  5. | Authentication Defaults
  6. |--------------------------------------------------------------------------
  7. |
  8. | This option controls the default authentication "guard" and password
  9. | reset options for your application. You may change these defaults
  10. | as required, but they're a perfect start for most applications.
  11. |
  12. */
  13. 'defaults' => [
  14. 'guard' => 'web',
  15. 'passwords' => 'users',
  16. ],
  17. /*
  18. |--------------------------------------------------------------------------
  19. | Authentication Guards
  20. |--------------------------------------------------------------------------
  21. |
  22. | Next, you may define every authentication guard for your application.
  23. | Of course, a great default configuration has been defined for you
  24. | here which uses session storage and the Eloquent user provider.
  25. |
  26. | All authentication drivers have a user provider. This defines how the
  27. | users are actually retrieved out of your database or other storage
  28. | mechanisms used by this application to persist your user's data.
  29. |
  30. | Supported: "session", "token"
  31. |
  32. */
  33. 'guards' => [
  34. 'web' => [
  35. 'driver' => 'session',
  36. 'provider' => 'users',
  37. ],
  38. 'api' => [
  39. 'driver' => 'token',
  40. 'provider' => 'users',
  41. 'hash' => false,
  42. ],
  43. //自定义guards为member
  44. 'member' => [
  45. 'driver' => 'session',
  46. 'provider' => 'members',
  47. ],
  48. ],
  49. /*
  50. |--------------------------------------------------------------------------
  51. | User Providers
  52. |--------------------------------------------------------------------------
  53. |
  54. | All authentication drivers have a user provider. This defines how the
  55. | users are actually retrieved out of your database or other storage
  56. | mechanisms used by this application to persist your user's data.
  57. |
  58. | If you have multiple user tables or models you may configure multiple
  59. | sources which represent each model / table. These sources may then
  60. | be assigned to any extra authentication guards you have defined.
  61. |
  62. | Supported: "database", "eloquent"
  63. |
  64. */
  65. 'providers' => [
  66. 'users' => [
  67. 'driver' => 'eloquent',
  68. 'model' => App\User::class,
  69. ],
  70. //自定义guard为member ,使用模型为Member
  71. 'members' => [
  72. 'driver' => 'eloquent',
  73. 'model' => App\Member::class,
  74. ],
  75. // 'users' => [
  76. // 'driver' => 'database',
  77. // 'table' => 'users',
  78. // ],
  79. ],
  80. /*
  81. |--------------------------------------------------------------------------
  82. | Resetting Passwords
  83. |--------------------------------------------------------------------------
  84. |
  85. | You may specify multiple password reset configurations if you have more
  86. | than one user table or model in the application and you want to have
  87. | separate password reset settings based on the specific user types.
  88. |
  89. | The expire time is the number of minutes that the reset token should be
  90. | considered valid. This security feature keeps tokens short-lived so
  91. | they have less time to be guessed. You may change this as needed.
  92. |
  93. */
  94. 'passwords' => [
  95. 'users' => [
  96. 'provider' => 'users',
  97. 'table' => 'password_resets',
  98. 'expire' => 60,
  99. 'throttle' => 60,
  100. ],
  101. ],
  102. /*
  103. |--------------------------------------------------------------------------
  104. | Password Confirmation Timeout
  105. |--------------------------------------------------------------------------
  106. |
  107. | Here you may define the amount of seconds before a password confirmation
  108. | times out and the user is prompted to re-enter their password via the
  109. | confirmation screen. By default, the timeout lasts for three hours.
  110. |
  111. */
  112. 'password_timeout' => 10800,
  113. ];

3.在middle文件夹中新建一个AuthMember.php的中间件

laravel--新建模型--实现前台用户登录验证

  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Hamcrest\Arrays\IsArray;
  5. //引入数据库查询构造器,链式调用
  6. use Illuminate\Support\Facades\DB;
  7. //引入Auth类,获取当前登录的用户
  8. use Illuminate\Support\Facades\Auth;
  9. use function GuzzleHttp\json_decode;
  10. class AuthMember
  11. {
  12. /**
  13. * Handle an incoming request.
  14. *
  15. * @param \Illuminate\Http\Request $request
  16. * @param \Closure $next
  17. * @return mixed
  18. */
  19. public function handle($request, Closure $next)
  20. {
  21. //如果用户没有登录Auth::guard('member')->guest()返回真
  22. if(Auth::guard('member')->guest())
  23. {
  24. if($request->ajax())
  25. {
  26. $res = ['code'=>'401','msg'=>'no login'];
  27. return response(json_encode($res),200);
  28. }
  29. return redirect()->guest('/homes/account/login');
  30. }
  31. return $next($request);
  32. }
  33. }

4.在kernel.php中注册前台用户登录验证中间件

laravel--新建模型--实现前台用户登录验证

  1. <?php
  2. namespace App\Http;
  3. use Illuminate\Foundation\Http\Kernel as HttpKernel;
  4. class Kernel extends HttpKernel
  5. {
  6. /**
  7. * The application's global HTTP middleware stack.
  8. *
  9. * These middleware are run during every request to your application.
  10. *
  11. * @var array
  12. */
  13. protected $middleware = [
  14. // \App\Http\Middleware\TrustHosts::class,
  15. \App\Http\Middleware\TrustProxies::class,
  16. \Fruitcake\Cors\HandleCors::class,
  17. \App\Http\Middleware\CheckForMaintenanceMode::class,
  18. \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
  19. \App\Http\Middleware\TrimStrings::class,
  20. \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
  21. ];
  22. /**
  23. * The application's route middleware groups.
  24. *
  25. * @var array
  26. */
  27. protected $middlewareGroups = [
  28. 'web' => [
  29. \App\Http\Middleware\EncryptCookies::class,
  30. \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
  31. \Illuminate\Session\Middleware\StartSession::class,
  32. // \Illuminate\Session\Middleware\AuthenticateSession::class,
  33. \Illuminate\View\Middleware\ShareErrorsFromSession::class,
  34. \App\Http\Middleware\VerifyCsrfToken::class,
  35. \Illuminate\Routing\Middleware\SubstituteBindings::class,
  36. ],
  37. 'api' => [
  38. 'throttle:60,1',
  39. \Illuminate\Routing\Middleware\SubstituteBindings::class,
  40. ],
  41. ];
  42. /**
  43. * The application's route middleware.
  44. *
  45. * These middleware may be assigned to groups or used individually.
  46. *
  47. * @var array
  48. */
  49. protected $routeMiddleware = [
  50. 'auth' => \App\Http\Middleware\Authenticate::class,
  51. 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
  52. 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
  53. 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
  54. 'can' => \Illuminate\Auth\Middleware\Authorize::class,
  55. 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
  56. 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
  57. 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
  58. 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
  59. 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
  60. //注册权限控制中间件
  61. 'rights' => \App\Http\Middleware\RightsVerify::class,
  62. //用户登录验证中间件
  63. 'member' => \App\Http\Middleware\AuthMember::class,
  64. ];
  65. }

5.使用member模型进行前台用户登录验证

laravel--新建模型--实现前台用户登录验证