繁体   English   中英

扩展laravel 5.2身份验证

[英]Extending laravel 5.2 authentication

我正在尝试将Laravel连接到外部数据库进行身份验证,但我不确定如何进行此操作。

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

数据库是auth ,它使用usernameemail登录,密码哈希是sha512。

什么是最好的方式?

不同数据库:

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

因此,在您的database.php配置中,添加另一个连接(让我们将其命名为mysql_auth )。

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

protected $connection = 'mysql_auth';

现在,默认情况下,模型查询将对该确切连接进行查询。

同样 ,要构建迁移,请使用Schema::connection('mysql_auth')->create(...)

不同的散列方法:

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

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

$this->app->singleton('hash', function () {
    return new BcryptHasher;
});

现在,这将然后链接到您的散列类(将实现HasherContract接口),将做SHA512(或任何其他)散列。

总而言之,请查看Illuminate\\Hashing\\HashServiceProvider ,它如何注册散列方法和Illuminate\\Hashing\\BcryptHasher ,以了解实现散列所需的方法。

更新:

在提供程序中注释掉Illuminate\\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'];
    }
}

这实际上归还了我们的哈希,我们将在片刻创造。

为了实现ShaHasher ,创建一个实现类HasherContract (就像BcryptHasher一样):

<?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;
    }
}

Sha512的新实现可能是这样的:

namespace App;

use Illuminate\Contracts\Hashing\Hasher;

class Sha512Hasher implements Hasher
{
    public function make($value, array $options = [])
    {

    }

    public function check($value, $hashedValue, array $options = [])
    {

    }

    public function needsRehash($hashedValue, array $options = [])
    {

    }
}

所以你基本上使用ServiceContract Illuminate\\Contracts\\Hashing\\Hasher

要绑定它,请使用ServiceProvider

class Sha512ServiceProvider extends ServiceProvider
{
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('hash', function () { 
        return new App\Sha512Hasher; 
    });

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM