簡體   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