在 Symfony 3.4 中手动散列(即编码)字符串值
Manually hash (i.e. encode) a string value in Symfony 3.4
Symfony 3.4 中有没有办法在没有实现 UserInterface
的实体的情况下使用密码编码器?
详情
我想在 Symfony 3.4 中散列一个随机字符串值。我总是可以使用 password_hash
,但我正在尝试以 Symfony 的方式执行此操作并利用该框架。这是我卡住的地方:
Symfony Documentation 有一个示例,但它使用的是 UserPasswordEncoder
,它假定您有一个实现 UserInterface
的实体作为第一个参数传递给 encodePassword($user, $plainPassword)
。
在这种情况下,我没有用户实体,因为我正在为电子邮件生成令牌 link。我确实有一个实体,但是当它不是用户时让它实现 UserInterface
感觉不对。
我研究了 UserPasswordEncoder
的代码,认为我可以注入具有通用 encodePassword($plainPassword, $salt)
方法的 PasswordEncoderInterface
,但是当我尝试自动装配它时出现此异常:
Cannot autowire service "SriBundle\Service\PubMatch\PublicationMatchLogService":
argument "$passwordEncoder" of method "__construct()" references interface
"Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface" but no such service exists.
It cannot be auto-registered because it is from a different root namespace.
似乎 Symfony 没有实现 PasswordEncoder
并且可以自动装配的默认服务。我在这里遗漏了什么,或者 Symfony 是否假设您只想对用户实体的密码进行哈希处理,并且不支持在不传递关联用户的情况下为随机字符串值编码哈希?
Symfony 中没有默认定义的PasswordEncoderInterface 实现服务。它们在内部与 UserPasswordEncoderInterface 一起使用。
感谢 debug:container 命令,我找到了这个。
但是您可以轻松地自己定义它们!
这是在 config/services.yaml 文件中使用 SodiumPasswordEncoder 的示例:
services:
# ... Your other definitions
# Define an alias to allow autowiring on the interface
Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface: '@password_encoder.sodium'
# Define the concrete service
# You can add the arguments entry if needed
password_encoder.sodium:
class: Symfony\Component\Security\Core\Encoder\SodiumPasswordEncoder
Symfony 3.4 中有没有办法在没有实现 UserInterface
的实体的情况下使用密码编码器?
详情
我想在 Symfony 3.4 中散列一个随机字符串值。我总是可以使用 password_hash
,但我正在尝试以 Symfony 的方式执行此操作并利用该框架。这是我卡住的地方:
Symfony Documentation 有一个示例,但它使用的是 UserPasswordEncoder
,它假定您有一个实现 UserInterface
的实体作为第一个参数传递给 encodePassword($user, $plainPassword)
。
在这种情况下,我没有用户实体,因为我正在为电子邮件生成令牌 link。我确实有一个实体,但是当它不是用户时让它实现 UserInterface
感觉不对。
我研究了 UserPasswordEncoder
的代码,认为我可以注入具有通用 encodePassword($plainPassword, $salt)
方法的 PasswordEncoderInterface
,但是当我尝试自动装配它时出现此异常:
Cannot autowire service "SriBundle\Service\PubMatch\PublicationMatchLogService":
argument "$passwordEncoder" of method "__construct()" references interface
"Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface" but no such service exists.
It cannot be auto-registered because it is from a different root namespace.
似乎 Symfony 没有实现 PasswordEncoder
并且可以自动装配的默认服务。我在这里遗漏了什么,或者 Symfony 是否假设您只想对用户实体的密码进行哈希处理,并且不支持在不传递关联用户的情况下为随机字符串值编码哈希?
Symfony 中没有默认定义的PasswordEncoderInterface 实现服务。它们在内部与 UserPasswordEncoderInterface 一起使用。
感谢 debug:container 命令,我找到了这个。
但是您可以轻松地自己定义它们! 这是在 config/services.yaml 文件中使用 SodiumPasswordEncoder 的示例:
services:
# ... Your other definitions
# Define an alias to allow autowiring on the interface
Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface: '@password_encoder.sodium'
# Define the concrete service
# You can add the arguments entry if needed
password_encoder.sodium:
class: Symfony\Component\Security\Core\Encoder\SodiumPasswordEncoder