symfony - Symfony 5:使用 make:auth Authenticator 的非持久用户
问题描述
都在标题中。当我登录时,它是成功的,我可以通过控制器中的 $this->getUser() 获取用户以响应登录路由,但是当我刷新页面/站点时,$this->getUser() 回到“null” “ 价值。
基本安全.yaml:
security:
enable_authenticator_manager: true
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
App\Entity\User:
algorithm: auto
providers:
app_user_provider:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: app_user_provider
custom_authenticator:
- App\Security\Authenticator
logout:
path: app_logout
access_control:
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
API 上下文中的身份验证器(这就是 authenticationSuccess 返回 null 的原因):
<?php
namespace App\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
class Authenticator extends AbstractLoginFormAuthenticator
{
use TargetPathTrait;
public const LOGIN_ROUTE = 'app_login';
private UrlGeneratorInterface $urlGenerator;
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
public function authenticate(Request $request): PassportInterface
{
$parameters = json_decode($request->getContent(), true);
return new Passport(
new UserBadge($parameters['username']),
new PasswordCredentials($parameters['password'])
);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
return null;
}
protected function getLoginUrl(Request $request): string
{
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
}
}
Controller中的登录路径:
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
$error = $authenticationUtils->getLastAuthenticationError();
if($error) {
return $this->json(['error' => $error->getMessage()]);
} else {
return $this->json($this->getUser());
}
}
来自 FRONTEND 的带有凭据的角度请求:
this.http.post("http://localhost:8000/login", this.myForm.value).subscribe(
(res : any) => (
this.snackBar.open(res.error || "You are logged", "Close"),
localStorage.setItem("username", res.username || undefined),
localStorage.setItem("email", res.email || undefined)
)
);
解决方案
推荐阅读
- python-3.x - Q:如何在celery worker_ready后台运行一个耗时的任务
- python - django-celery-beat 是否处理由网络服务器创建的多个 django 进程实例
- java - Selenium 中的数据提供程序与 TestNG 和 Java 不匹配
- c++ - 一直在监听 UDP 服务器
- flutter - 在颤振中使用 agora_rtc_engine 4.0.6 进行视频通话的黑屏
- api-gateway - 大使 API 密钥管理
- javascript - INDEXEDDB 更新/放置未返回预期状态的更新对象
- visual-studio-code - 如何在 Visual Studio 代码中编辑 jupyter 笔记本单元格的标签?
- python - discord.py 命令组中的子命令是否继承父命令的装饰器?
- c++ - 我的代码在标准输入中显示 4(Coreect),在 USACO 分级机中显示 5645,在 scanf/printf 中显示 1862013874。问题是什么?