這是我添加管理員用戶代碼:Symfony的2.1壞憑據時
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()
));
}
這裏是我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' ] }
一切都OK時,我想登錄根,或者當我設置實體編碼器爲明文。當我爲sha512設置實體編碼器時,爲什麼我總是收到錯誤憑據?
你先生的長度,是一個紳士,一位學者!我正在關注這本書,並假設我對YML文件做了一些錯誤......感嘆。 –
OMG !!!非常感謝!我只花了兩個小時改變一切,你救了我! – ValentinH