Symfony 4 EasyAdmin 如何加密密码?

Posted

技术标签:

【中文标题】Symfony 4 EasyAdmin 如何加密密码?【英文标题】:Symfony 4 EasyAdmin how to encrypt passwords? 【发布时间】:2019-07-11 16:37:51 【问题描述】:

我正在使用 EasyAdmin 来添加/编辑用户,并想问是否有可能加密您的密码? 以前我使用 Symfony 4 make:registration-form 时密码加密有效,但我现在不能使用它,我必须使用 EasyAdmin。

easy_admin.yaml

easy_admin:
  entities:
    User:
     class: App\Entity\User
     password_encoding:  algorithm: 'bcrypt', cost: 12 

(实际)我进入EasyAdmin页面(/admin),点击用户,添加用户,填写邮箱(test@gmail.com)和密码(test),点击保存更改。

现在用户存储在数据库中,但使用明文密码。

(预期)以上所有内容,但密码已加密。

【问题讨论】:

它与 Easy Admin 无关。您必须在packages/security.yaml 中自己配置安全性 我在security.yaml中写了这个安全性:编码器:App\Entity\User:算法:argon2i 你能扩展你的问题吗?您的意思是当您通过 EasyAdmin 创建/编辑用户时,它不会加密密码? 是的,我就是这个意思 也许你在这里找到答案:github.com/EasyCorp/EasyAdminBundle/issues/1261。您应该覆盖 UserController 并手动添加密码编码/解码。 【参考方案1】:

扩展 EasyAdmin 控制器并处理用户实体。像这样的:

namespace AppBundle\Controller\Admin;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController;
use AppBundle\Entity\User;

class AdminController extends BaseAdminController

    protected function prePersistUserEntity(User $user)
    
        $encodedPassword = $this->encodePassword($user, $user->getPassword());
        $user->setPassword($encodedPassword);
    

    protected function preUpdateUserEntity(User $user)
    
        if (!$user->getPlainPassword()) 
            return;
        
        $encodedPassword = $this->encodePassword($user, $user->getPlainPassword());
        $user->setPassword($encodedPassword);
    

    private function encodePassword($user, $password)
    
        $passwordEncoderFactory = $this->get('security.encoder_factory');
        $encoder = $passwordEncoderFactory->getEncoder($user);
        return $encoder->encodePassword($password, $user->getSalt());
    



【讨论】:

【参考方案2】:

与 Symfony 5 兼容的新版本:

<?php

namespace App\Controller;

use App\Entity\User;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;

class AdminController extends EasyAdminController

    protected function persistUserEntity($user)
    
        $encodedPassword = $this->encodePassword($user, $user->getPlainPassword());
        $user->setPassword($encodedPassword);

        parent::persistEntity($user);
    

    protected function updateUserEntity($user)
    
        $encodedPassword = $this->encodePassword($user, $user->getPlainPassword());
        $user->setPassword($encodedPassword);

        parent::updateEntity($user);
    

    private function encodePassword($user, $password)
    
        $passwordEncoderFactory = new EncoderFactory([
            User::class => new MessageDigestPasswordEncoder('sha512', true, 5000)
        ]);

        $encoder = $passwordEncoderFactory->getEncoder($user);

        return $encoder->encodePassword($password, $user->getSalt());
    

【讨论】:

【参考方案3】:

我的工作项目中基于 Symfony 5 和 PHP 7.4 的代码

    扩展EasyAdminController:
<?php

declare(strict_types=1);

namespace App\Controller;

use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

/**
 * Class AdminController.
 *
 * @author Dmitriy Atamaniuc <d.atamaniuc@gmail.com>
 */
final class AdminController extends EasyAdminController

    private UserPasswordEncoderInterface $encoder;

    private function setUserPlainPassword(User $user): void
    
        if ($user->getPlainPassword()) 
            $user->setPassword($this->encoder->encodePassword($user, $user->getPlainPassword()));
        
    

    /**
     * @required
     */
    public function setEncoder(UserPasswordEncoderInterface $encoder): void
    
        $this->encoder = $encoder;
    

    public function persistUserEntity(User $user): void
    
        $this->setUserPlainPassword($user);

        $this->persistEntity($user);
    

    public function updateUserEntity(User $user): void
    
        $this->setUserPlainPassword($user);

        $this->updateEntity($user);
    

    在 EasyAdminBundle 中注册您的控制器,编辑 config/routes/easy_admin.yaml
easy_admin_bundle:
    resource: 'App\Controller\AdminController'
    prefix: /admin
    type: annotation
    plainPassword“虚拟”属性添加到您的User实体:
/**
* @Assert\NotBlank()
* @Assert\Length(min=5, max=128)
*/
private ?string $plainPassword = null;

public function getPlainPassword(): ?string

    return $this->plainPassword;


public function setPlainPassword(string $password): void

    $this->plainPassword = $password;

    更新easy_admin.yaml
easy_admin:
    entities:
        User:
            class: App\Entity\User
            label: label.user
            new:
                title:  field.user.add_new
                fields:
                    # some fields like username here
                    -  property: username, label: field.user.username 
                    -  property: fullname, label: field.user.fullname, type: 'text' 
                    # plain password 
                    -  property: plainPassword, label: field.user.password, type: 'password' 

【讨论】:

以上是关于Symfony 4 EasyAdmin 如何加密密码?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Symfony EasyAdmin 3 中创建密码输入类型

如何在 Symfony EasyAdmin 3 中创建数据验证系统

EasyAdmin 3.1 CrudControllers Symfony

如何在easyadmin 3中更改分页

在 Symfony 5 中使用 composer 安装 EasyAdmin 的问题

Symfony - EasyAdmin - 从 AssociationField 中忽略添加和删除功能