【发布时间】:2011-10-24 16:38:19
【问题描述】:
我是 Symfony2 的新手,我可能有一个关于在我的数据库中编码我的用户密码的简单问题。
我想以这种方式对我的用户密码进行编码并存储在数据库中:
encoded_password = salt . sha1 ( salt . raw_password )
我找到了各种编码器(sha1、sha512、明文),我看到我的数据库 raw_password{salt} 中有明文,但我仍然不习惯 Symfony2 中的 signin/login/getSalt() 方法。
如果您能帮我解决这个问题(请假设我不想将现有的捆绑包用于 UserManagement,我想自己制作),那就太棒了!
谢谢
编辑:
我可以在我的 signinAction() 中做到这一点:
$salt = substr(md5(time()),0,10);
$pwd = $encoder->encodePassword($user->getPassword(), $salt);
$user->setPassword($salt.$pwd);
我可以在我的 getSalt() 中做到这一点:
return substr($this->password,0,10);
但我目前在我的 loginAction() 中只有那个:(见这里:http://symfony.com/doc/current/book/security.html)
// src/Acme/SecurityBundle/Controller/Main;
namespace Acme\SecurityBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
class SecurityController extends Controller
{
public function loginAction()
{
$request = $this->getRequest();
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('AcmeSecurityBundle:Security:login.html.twig', array(
// last username entered by the user
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
}
如何告诉 Symfony2 在登录操作期间按照我需要的方式检查密码? (目前正在做 encode(password,salt) 而不是 salt.encode(password,salt)
【问题讨论】:
-
“我仍然对 Symfony2 中的 signin/login/getSalt() 方法不满意” - 具体是什么?很高兴知道回答您的问题。
-
这意味着我看到了如何在我的登录控制器上实现此行为,但我目前使用 Sf2 默认 loginAction 并且不知道如何在该控制器中实现它。
-
您指的是哪个捆绑包?
-
嘿@hakre,我在我的问题中放了一些代码。希望它能帮助你理解我的问题。非常感谢
-
查看我的捆绑包:github.com/elnur/ElnurBlowfishPasswordEncoderBundle。它提供真正的加密——不仅仅是散列。