如何解决如何使用 LoginFormAuthenticator 和 Login Link Authenticator 为 Symfony 5.2 中的特定路径设置默认身份验证器
我在 Symfony 5.2 中为我的客户使用新的登录链接方法 [1]。他们应该只使用这种身份验证方法。但我也为我的管理员用户使用登录表单身份验证 [2]。
当未经身份验证的客户访问受限路径(在我的情况下为 /app)时,他/她会被重定向到登录表单而不是登录链接表单。当会话已过期或他们访问(未经身份验证)受限区域内的书签链接时,也会出现这种情况。
如何为 /app 设置默认验证器? /行政?我想要这样的东西:
我的 security.yaml 如下所示:
security:
encoders:
App\Entity\User:
algorithm: auto
enable_authenticator_manager: true
providers:
app_user_provider:
entity:
class: App\Entity\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: app_user_provider
guard:
authenticators:
- App\Security\LoginFormAuthenticator
login_link:
check_route: login_check
signature_properties: ['id']
max_uses: 1
logout:
path: app_logout
access_control:
- { path: ^/admin,roles: ROLE_ADMIN }
- { path: ^/app,roles: ROLE_USER }
- { path: ^/,roles: PUBLIC_ACCESS }
[1] 登录链接方式:https://symfony.com/doc/current/security/login_link.html
[2] LoginFormAuthenticator:https://symfony.com/doc/current/security/form_login_setup.html
解决方法
好的,我自己想出了一个解决方案。如果有更好的解决方案,请告诉我!
我覆盖了 LoginFormAuthenticator 类中的 start() 方法。在那里,我将 $request obj 存储在私有类 prop 中,因为由于 AbstractFormLoginAuthenticator 类中的定义,我无法覆盖 getLoginUrl()。然后我在 getLoginUrl() 方法中添加一些逻辑来检查用户想要访问的区域(/admin 或 /app)并相应地重定向。而已。奇迹般有效。 ;-)
// App\Security\LoginFormAuthenticator
public const LOGIN_ROUTE = 'admin_login';
public const LOGIN_ROUTE_PASSWORDLESS = 'app_login';
...
private $request;
...
/**
* Override to control what happens when the user hits a secure page
* but isn't logged in yet.
*
* @return RedirectResponse
*/
public function start(Request $request,AuthenticationException $authException = null)
{
$this->request = $request;
$url = $this->getLoginUrl();
return new RedirectResponse($url);
}
...
protected function getLoginUrl()
{
// Check if user would like to access /app
$path = $this->request->getPathInfo();
if ('/app' === substr($path,4)) {
return $this->urlGenerator->generate(self::LOGIN_ROUTE_PASSWORDLESS);
}
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。