Symfony新手在这里。 我只是在按照一个教程尝试使用Symfony 4.6构建一个简单的注册/登录系统。一切正常,除了每次“客户”注册时,其密码都会保存到数据库(mySql)中,而不会被散列。
基本上,我已经创建了“客户”实体和“ CustomAuthenticator”,但我注意到在security.yaml文件中,编码器算法仍设置为“自动”,而我希望将其更改为“ argon2i”或者是其他东西... 这是我的Security.yaml文件和“ RegistrationController”:
encoders:
App\Entity\Customer:
algorithm: auto
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\Customer
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: true
guard:
authenticators:
- App\Security\CustomAuthAuthenticator
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#firewalls-authentication
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
--------------------------"RegistrationController"--------------------------
```<?php
namespace App\Controller;
use App\Entity\Customer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class RegisterController extends AbstractController
{
/**
* @Route("/register", name="register")
*/
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$form = $this->createFormBuilder()
->add('username')
->add('password', RepeatedType::class, [
'type'=>PasswordType::class,//definisco il tipo di field
'required'=>true,//required html tag
'first_options' => ['label' => 'Password'],
'second_options'=> ['label' => 'Confirm password']
])
->add('Sign Up!',SubmitType::class,[
'attr'=>[
'class' => 'btn btn-success float-right'
]
])->getForm();
//getForm crea effettivamente il form che nei comandi sopra è stato solamente definito nelle sue componenti
$form->handleRequest($request);
if($form->isSubmitted()){
$data=$form->getData();
$customer = new Customer();
$customer->setUsername($data['username']);
$customer->setPassword($data['password'],
$passwordEncoder->encodePassword($customer,$data['password'])
);
//dump($data);//vardump
$em = $this->getDoctrine()->getManager();
$em->persist($customer);
$em->flush();
return $this->redirect($this->generateUrl('app_login'));
}
return $this->render('register/index.html.twig',[
'form'=>$form->createView()
]);
}
}