您必須重寫Hash
模塊。感謝Laravel遵循IoC和依賴注入概念的想法,它會相對容易。
首先,創建一個文件夾app/libraries
並將其添加到作曲家的autoload.classmap
:現在
"autoload": {
"classmap": [
// ...
"app/libraries"
]
},
,這是我們創建的上課時間。創建一個SHAHasher
類,實現Illuminate\Hashing\HasherInterface
。我們需要實現3種方法:make
,check
和needsRehash
。
注意:在Laravel 5上,執行Illuminate/Contracts/Hashing/Hasher
而不是Illuminate\Hashing\HasherInterface
。
應用程序/庫/ SHAHasher.php
class SHAHasher implements Illuminate\Hashing\HasherInterface {
/**
* Hash the given value.
*
* @param string $value
* @return array $options
* @return string
*/
public function make($value, array $options = array()) {
return hash('sha1', $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 = array()) {
return $this->make($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 = array()) {
return false;
}
}
現在,我們有我們的類來完成,我們希望它在默認情況下被使用,通過Laravel。要做到這一點,我們將創建SHAHashServiceProvider
,延長Illuminate\Support\ServiceProvider
,並將其註冊爲hash
成分:
應用程序/庫/ SHAHashServiceProvider.php
class SHAHashServiceProvider extends Illuminate\Support\ServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register() {
$this->app['hash'] = $this->app->share(function() {
return new SHAHasher();
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return array('hash');
}
}
酷,現在我們要做的就是確保我們的應用程序加載正確的服務提供商。上app/config/app.php
,providers
下,除去下面的行:
'Illuminate\Hashing\HashServiceProvider',
然後,添加這一個:
'SHAHashServiceProvider',
確保您的密碼字段的長度至少爲100個字符。 –
它有255個字符。我只需要讓SHA1加密方法工作。 – dinomuharemagic
Laravel在'auth'上做了一些機制,也許它使用'bcryp't來檢查密碼,但是不完全確定它。 –