Laravel基本认证 [英] Laravel basic-auth

查看:83
本文介绍了Laravel基本认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在网页上使用basic.auth,但是身份验证不起作用

I want to use basic.auth for my web page but authentication donst work

Route::get('admin', array('before' => 'auth.basic', function()
{
    return 'Top secret';
}));

create-创建测试用户

create - create test user

Route::get('create', function()
{
    $user = new User;
    $user->email = 'test@test.com';
    $user->username = 'test';
    $user->password = Hash::make('password');
    $user->save();
});

config

  • app/config/app-定义了key(创建了Laravel安装)
  • app/config/auth-定义了model(User)和table(users)
  • config

    • app/config/app - has defined key (that created Laravel installation)
    • app/config/auth - has defined model (User) and table (users)
    • Route::filter('auth.basic', function()
      {
          return Auth::basic();
      });
      

      测试

      我呼叫/create创建用户test@test.com:password

      以下是users表:

      然后我打电话给/admin进行登录

      Then I call /admin to login

      但是它不让我进去.登录之后-它只是清除输入. 取消之后-返回Invalid credentials..

      But it doesnt let me in. After Login - it just clear inputs. After Cancel - it return Invalid credentials..

      我尝试实施UserInterface

      <?php
      use Illuminate\Auth\UserInterface;
      
      class User extends Eloquent implements UserInterface {
      
          protected $table = 'users';
      
          /**
           * Get the unique identifier for the user.
           *
           * @return mixed
           */
          public function getAuthIdentifier()
          {
              return $this->getKey();
          }
      
          /**
           * Get the password for the user.
           *
           * @return string
           */
          public function getAuthPassword()
          {
              return $this->passsword;
          }
      }
      


      问题解决了

      我在User模型return $this->passsword;中有错字.有3个s.


      Problem solved

      I had typo in User model return $this->passsword; There is 3 s.

      现在,我使用默认的Laravel 用户模型

      Now I use default Laravel User model.

      推荐答案

      确保在app/config/auth.php中-driver设置为eloquent.

      Ensure that in app/config/auth.php - driver is set to eloquent.

      您可能还需要实现UserInterface接口(class User extends Eloquent implements UserInterface)-然后需要在模型中包括方法:

      You may also need to implement the UserInterface interface (class User extends Eloquent implements UserInterface) - then you'll need to include the methods in your model:

      /**
       * Get the unique identifier for the user.
       *
       * @return mixed
       */
      public function getAuthIdentifier()
      {
          return $this->getKey();
      }
      
      /**
       * Get the password for the user.
       *
       * @return string
       */
      public function getAuthPassword()
      {
          return $this->password;
      }
      

      这篇关于Laravel基本认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆