Php 扩展laravel 5.2身份验证

Php 扩展laravel 5.2身份验证,php,laravel,authentication,laravel-5.2,Php,Laravel,Authentication,Laravel 5.2,我正在尝试将Laravel连接到外部数据库以进行身份验证,尽管我不知道如何进行 我如何扩展Laravels身份验证以允许自定义登录方法 数据库为auth,使用username或email登录,密码散列为sha512 最好的办法是什么 不同的数据库: 我认为最好的方法是为特定模型定义一个单独的连接 因此,在您的database.phpconfig中,添加另一个连接(将其命名为mysql\u auth) 要在模型中使用它,需要将其作为变量添加到类中: protected $connection =

我正在尝试将Laravel连接到外部数据库以进行身份验证,尽管我不知道如何进行

我如何扩展Laravels身份验证以允许自定义登录方法

数据库为
auth
,使用
username
email
登录,密码散列为sha512


最好的办法是什么

不同的数据库:

我认为最好的方法是为特定模型定义一个单独的连接

因此,在您的
database.php
config中,添加另一个连接(将其命名为
mysql\u auth

要在模型中使用它,需要将其作为变量添加到类中:

protected $connection = 'mysql_auth';
现在,默认情况下,模型查询将对该确切连接执行查询

请注意,要构建迁移,请使用
Schema::connection('mysql\u auth')->create(…)

不同的哈希方法:

原件: 要使用不同的散列功能,基本上需要使用不同的类作为散列类

默认情况下,在提供程序中定义的哈希在此处完成:
illighte\hashing\HashServiceProvider::class
。要更改它,您必须创建一个单独的、不同的类作为提供程序,并更改此行:

$this->app->singleton('hash', function () {
    return new BcryptHasher;
});
现在,这将链接到您的散列类(将实现
HasherContract
接口),该类将执行sha512(或任何其他)散列

总之,请查看
illumb\Hashing\HashServiceProvider
,了解它如何注册哈希方法,以及
illumb\Hashing\BcryptHasher
了解实现哈希所需的方法

更新:

注释掉提供程序中的
illumb\Hashing\HashServiceProvider::class
,并添加类似
\App\providers\NewHashServiceProvider::class
的内容。现在,我们创建一个新的提供者(
app/Providers
)。这应该起作用:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class NewHashServiceProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('hash', function () {
            return new \App\ShaHasher;
        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return ['hash'];
    }
}

Sha512的新实现可能如下所示:

namespace应用程序;
使用Illumb\Contracts\Hashing\Hasher;
类Sha512Hasher实现Hasher
{
公共函数make($value,数组$options=[]))
{
}
公共函数检查($value、$hashedValue、数组$options=[]))
{
}
公共函数需要散列($hashedValue,数组$options=[]))
{
}
}
因此,您基本上使用了
illumb\Contracts\Hashing\Hasher

要绑定此文件,请使用:

类Sha512ServiceProvider扩展了ServiceProvider
{
/**
*在容器中注册绑定。
*
*@返回无效
*/
公共职能登记册()
{
$this->app->singleton('hash',function(){
返回新应用程序\Sha512Hasher;
});
}
}

这解决了一个问题,但是密码的散列方式与laravel默认值不同,我必须承认,尽管模型中的连接是一个非常漂亮的特性,但我已经在问题的散列部分添加了更多信息。最干净的方法是使用@Andrius提到的
HashServiceProvider
。好主意。似乎无法从用户模型中读取连接或表,它只是默认值,但您在database.php中指定了第二个连接,并添加了“protected$connection=”;”在你的模型里?
<?php

namespace App;

use RuntimeException;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;

class ShaHasher implements HasherContract
{
    /**
     * Hash the given value.
     *
     * @param  string  $value
     * @param  array   $options
     * @return string
     *
     * @throws \RuntimeException
     */
    public function make($value, array $options = [])
    {
        return hash('sha512',$value);
    }

    /**
     * Check the given plain value against a hash.
     *
     * @param  string  $value
     * @param  string  $hashedValue
     * @param  array   $options
     * @return bool
     */
    public function check($value, $hashedValue, array $options = [])
    {
        if (strlen($hashedValue) === 0) {
            return false;
        }

        return hash('sha512',$value) == $hashedValue
    }

    /**
     * Check if the given hash has been hashed using the given options.
     *
     * @param  string  $hashedValue
     * @param  array   $options
     * @return bool
     */
    public function needsRehash($hashedValue, array $options = [])
    {
        return false;
    }
}