仅在编码密码时Symfony 2.1错误凭据

mic*_*l_w 3 authentication encoding password-protection symfony

这是我添加管理员用户的代码:

public function addAction(Request $request){
    $admin=new Admin();
    $form=$this->createForm(new AdminType(), $admin);
    if($request->getMethod()=='POST'){
        $form->bindRequest($request);
        if($form->isValid()){
            $factory = $this->get('security.encoder_factory');
            $encoder = $factory->getEncoder($admin);
            $password = $encoder->encodePassword($admin->getPassword(), $admin->getSalt());
            $admin->setPassword($password);

            $em=$this->getDoctrine()->getEntityManager();
            $em->persist($admin);
            $em->flush();
        }
    }
    return $this->render('PuzzleAdminBundle:Admin:add.html.twig', array(
        'form'=>$form->createView()
    ));
}
Run Code Online (Sandbox Code Playgroud)

这是我的security.yml:

security:
encoders:
    Puzzle\AdminBundle\Entity\Admin: sha512
    Symfony\Component\Security\Core\User\User: plaintext

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

providers:
    chain_providers:
        chain:
            providers: [admin_db, in_memory]
    admin_db:
        entity: { class: Puzzle\AdminBundle\Entity\Admin, property: username }
    in_memory:
        memory:
            users:
                root: { password: 123456, roles: [ 'ROLE_SUPER_ADMIN' ] }
Run Code Online (Sandbox Code Playgroud)

当我想在root上登录时,或者当我将实体编码器设置为明文时,一切都很好.为什么在将实体编码器设置为sha512时,我总是会收到错误的凭据?

小智 8

检查数据库中的字段长度.如果您正在关注"The Book",那么您的字段可能会被设置为varchar(50).Base64编码的SHA-512将输出88个字符的字符串.为此调整你应该很高兴.