如何解决Symfony 使用电子邮件而不是用户名记住我
我正在尝试使用电子邮件在 Symfony 4.4 上配置“记住我”功能。登录/身份验证工作正常,但“记住我”功能不起作用。
我的security.yaml
定义如下,非常标准(刚刚添加了email属性)
security:
encoders:
App\Entity\User:
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\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: lazy
provider: app_user_provider
guard:
authenticators:
- App\Security\LoginFormAuthenticator
logout:
path: app_logout
# where to redirect after logout
# target: app_any_route
remember_me:
secret: '%kernel.secret%'
根据发现 here 和 here 的信息,我创建了一个新服务 TokenBasedRememberMeServices
以在进行检查时使用电子邮件而不是用户名,但似乎从未调用内部代码。 .. 知道有什么问题或如何实现吗?
我认为这一定是配置问题,因为我之前从未装饰过服务...
#services.yaml
services:
App\Security\TokenBasedRememberMeServices:
decorates: security.authentication.rememberme.services.simplehash
class TokenBasedRememberMeServices extends \Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices {
/**
* This is called after a user has been logged in successfully,and has
* requested remember-me capabilities. The implementation usually sets a
* cookie and possibly stores a persistent record of it.
* @param Request $request
* @param Response $response
* @param TokenInterface $token
*/
protected function onLoginSuccess (Request $request,Response $response,TokenInterface $token) {
$user = $token->getUser();
$expires = time() + $this->options['lifetime'];
$value = $this->generateCookieValue(\get_class($user),$user->getEmail(),$expires,$user->getpassword());
$response->headers->setCookie(
new Cookie(
$this->options['name'],$value,$this->options['path'],$this->options['domain'],$this->options['secure'] ?? $request->isSecure(),$this->options['httponly'],false,$this->options['samesite']
)
);
}
/**
* {@inheritdoc}
*/
protected function processAutoLoginCookie(array $cookieParts,Request $request)
{
if (4 !== \count($cookieParts)) {
throw new AuthenticationException('The cookie is invalid.');
}
[$class,$email,$hash] = $cookieParts;
if (false === $email = base64_decode($email,true)) {
throw new AuthenticationException('$email contains a character from outside the base64 alphabet.');
}
try {
$user = $this->getUserProvider($class)->loadUserByEmail($email);
} catch (\Exception $e) {
if (!$e instanceof AuthenticationException) {
$e = new AuthenticationException($e->getMessage(),$e->getCode(),$e);
}
throw $e;
}
if (!$user instanceof UserInterface) {
throw new \RuntimeException(sprintf('The UserProviderInterface implementation must return an instance of UserInterface,but returned "%s".',\get_class($user)));
}
if (true !== hash_equals($this->generateCookieHash($class,$user->getpassword()),$hash)) {
throw new AuthenticationException('The cookie\'s hash is invalid.');
}
if ($expires < time()) {
throw new AuthenticationException('The cookie has expired.');
}
return $user;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。