Laravel 基本身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17447617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:04:54  来源:igfitidea点击:

Laravel basic-auth

laravellaravel-4

提问by Shaddow

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

我想basic.auth用于我的网页,但身份验证不起作用

routes.php

路由文件

admin- authentication

admin- 验证

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

create- create test user

create- 创建测试用户

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

config

配置

  • app/config/app- has defined key(that created Laravel installation)
  • app/config/auth- has defined model(User) and table(users)
  • app/config/app- 已定义key(创建 Laravel 安装)
  • app/config/auth- 已定义model( User) 和table( users)

filters.php

过滤器.php

auth.basic

auth.basic

Route::filter('auth.basic', function()
{
    return Auth::basic();
});

test

测试

I call /createto create User [email protected]:password

我打电话/create创建用户[email protected]password

Here is userstable after: enter image description here

这是users之后的表格: 在此处输入图片说明

Then I call /adminto login

然后我打电话/admin登录

enter image description here

在此处输入图片说明

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

但它并没有让我进去。之后Login- 它只是清除输入。After Cancel- 它返回Invalid credentials.



User model

用户模型

I tried implement UserInterface

我试过实现 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;
    }
}


Problem solved

问题解决了

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

我在User模型中return $this->passsword;有错别字There is 3 s

Now I use default Laravel User model.

现在我使用默认的 Laravel User 模型

回答by Half Crazed

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

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

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

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

/**
 * 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;
}