Symfony 5.4安全多防火墙



我正试图将用户身份验证与管理员身份验证分开。

所以我创建了两个防火墙和两个不同的访问控制。

我的安全yaml看起来是这样的:

enable_authenticator_manager: true
password_hashers:
SymfonyComponentSecurityCoreUserPasswordAuthenticatedUserInterface: 'auto'
providers:
owner_authentication:
entity:
class: AppEntityMerchantOwner
property: emailAddress
user_authentication:
entity:
class: AppEntityUserUser
property: emailAddress
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
user:
lazy: true
pattern: ^/admin/login/
provider: user_authentication
user_checker: AppSecurityAuthentificableModelChecker
form_login:
provider: user_authentication
default_target_path: app.dashboard.index
use_referer: true
use_forward: false
login_path: app.authorization.admin_login
check_path: app.authorization.admin_login
username_parameter: login[emailAddress]
password_parameter: login[password]
logout:
path: app.authorization.logout
target: app.authorization.admin_login
main:
lazy: true
pattern: ^/
provider: owner_authentication
user_checker: AppSecurityAuthentificableModelChecker
form_login:
provider: owner_authentication
default_target_path: app.dashboard.index
use_referer: true
use_forward: false
login_path: app.authorization.login
check_path: app.authorization.login
username_parameter: login[emailAddress]
password_parameter: login[password]
logout:
path: app.authorization.logout
target: app.authorization.login
access_control:
- { path: ^/admin/login, roles: PUBLIC_ACCESS}
- { path: ^/login, roles: PUBLIC_ACCESS }
- { path: ^/, roles: ROLE_USER }

在主防火墙上一切都很好,但当我使用用户(管理员(防火墙提交按钮时,登录页面会自行刷新,什么也不会发生。我没有任何错误。

**如果我在主防火墙上添加用户(admin(登录,那么/admin/login将正常工作,而另一个将不再工作。

当我调用$authenticationUtils->getLastAuthenticationError()时,我没有收到任何错误。但验证也不起作用。

这就是我的控制器的样子:

public function adminLogin(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('app.dashboard.index');
}

$loginForm = $this->createForm(LoginType::class, ['emailAddress' => $authenticationUtils->getLastUsername()]);

return $this->renderForm('app/pages/authorization/admin_login.html.twig', [
'title'      => 'Log in',
'login_form' => $loginForm,
'error'      => $authenticationUtils->getLastAuthenticationError()?->getMessageKey(),
]);
}

这个人也有同样的问题:https://grafikart.fr/forum/35234但我找不到任何解决办法。

最后,我找到了答案,所以我会把它发布在这里:https://stackoverflow.com/a/42352112/8003007我要做的是在两个防火墙中都添加一个context: my_context

这是一个很难识别的选择,因为它没有出现在官方和当前的Symfony文档中,而只出现在以前的文档中,如Symfony 3.4。

最新更新