如何使用 LoginFormAuthenticator 和 Login Link Authenticator 在 Symfony 5.2 中为特定路径设置默认身份验证器
Posted
技术标签:
【中文标题】如何使用 LoginFormAuthenticator 和 Login Link Authenticator 在 Symfony 5.2 中为特定路径设置默认身份验证器【英文标题】:How to set default authenticators for specific paths in Symfony 5.2 using LoginFormAuthenticator and Login Link Authenticator 【发布时间】:2021-07-16 03:06:33 【问题描述】:我正在为我的客户使用 Symfony 5.2 中新的登录链接方法 [1]。他们应该只使用这种身份验证方法。但我也为我的管理员用户使用登录表单身份验证 [2]。
当未经身份验证的客户访问受限路径(在我的情况下为 /app)时,他/她会被重定向到表单登录而不是登录链接表单。当会话过期或他们访问(未经身份验证)受限区域内的书签链接时,也会出现这种情况。
如何设置 /app 的默认身份验证器。 /行政?我想要这样的东西:
客户区 /app -> /login(登录链接验证器) 管理区 /admin -> /login-password(登录表单验证器)我的 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
【问题讨论】:
【参考方案1】:好的,我自己想出了一个解决方案。如果有更好的解决方案,请告诉我!
我覆盖了 LoginFormAuthenticator 类中的 start() 方法。在那里,我将 $request obj 存储在私有类道具中,因为由于 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, 0, 4))
return $this->urlGenerator->generate(self::LOGIN_ROUTE_PASSWORDLESS);
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
【讨论】:
以上是关于如何使用 LoginFormAuthenticator 和 Login Link Authenticator 在 Symfony 5.2 中为特定路径设置默认身份验证器的主要内容,如果未能解决你的问题,请参考以下文章
如何在自动布局中使用约束标识符以及如何使用标识符更改约束? [迅速]
如何使用 AngularJS 的 ng-model 创建一个数组以及如何使用 jquery 提交?