我正在學習Symfony2通過移動一些wordpress博客到Symfony。我被登錄程序卡住了。 Wordpress使用非標準密碼散列,如$P$....
,我想在用戶登錄時檢查舊密碼散列,並在密碼正確時將其重新轉換爲bcrypt。到目前爲止,我創建了custome編碼器類來與symfony安全機制一起使用。Symfony2自定義密碼編碼器和密碼哈希更新
<?php
namespace Pkr\BlogUserBundle\Service\Encoder;
use PHPassLib\Application\Context;
use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\Util\SecureRandom;
class WpTransitionalEncoder implements PasswordEncoderInterface
{
public function __construct($cost = 13)
{
$secure = new SecureRandom();
$this->_bcryptEncoder = new BCryptPasswordEncoder($secure, $cost);
}
public function isPasswordValid($encoded, $raw, $salt)
{
if (preg_match('^\$P\$', $encoded)) {
$context = new Context();
$context->addConfig('portable');
return $context->verify($raw, $encoded);
}
return $this->_bcryptEncoder->isPasswordValid($encoded, $raw, $salt);
}
public function encodePassword($raw, $salt)
{
return $this->_bcryptEncoder->encodePassword($raw, $salt);
}
}
我使用它作爲一個服務:
#/src/Pkr/BlogUserBundle/Resources/config/services.yml
services:
pkr_blog_user.wp_transitional_encoder:
class: Pkr\BlogUserBundle\Service\Encoder\WpTransitionalEncoder
而且在security.yml:
#/app/config/security.yml
security:
encoders:
Pkr\BlogUserBoundle\Entity\User:
id: pkr_blog_user.wp_transitional_encoder
cost: 15
我的問題是:
如何傳遞參數我的編碼器服務形式在security.yml
?
我在問,因爲cost: 15
不起作用。
我應該在哪裏放置密碼哈希更新邏輯?我在想,莫比剛密碼驗證這樣的事情後:
public function isPasswordValid($encoded, $raw, $salt)
{
if (preg_match('^\$P\$', $encoded)) {
$context = new Context();
$context->addConfig('portable');
$isValid = $context->verify($raw, $encoded);
if ($isValid) {
// put logic here...
}
return $isValid;
}
return $this->_bcryptEncoder->isPasswordValid($encoded, $raw, $salt);
}
,但它似乎在某種程度上就像錯了地方吧。那麼正確的方法是什麼?