多个防火墙提升了Symfony 5.4的安全性
P粉952365143
2023-08-26 16:14:30
<p>我正在尝试将用户的身份验证与管理员的身份验证分开。</p>
<p>所以我创建了2个防火墙和2个不同的访问控制。</p>
<p>我的security.yaml文件如下:</p>
<pre class="brush:php;toolbar:false;">enable_authenticator_manager: true
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
providers:
owner_authentication:
entity:
class: App\Entity\Merchant\Owner
property: emailAddress
user_authentication:
entity:
class: App\Entity\User\User
property: emailAddress
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
user:
lazy: true
pattern: ^/admin/login/
provider: user_authentication
user_checker: App\Security\AuthentificableModelChecker
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: App\Security\AuthentificableModelChecker
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 }</pre>
<p>主防火墙上的一切都正常工作,但当我使用用户(管理员)防火墙提交按钮时,登录页面会刷新自身,然后什么也不发生。我没有任何错误。</p>
<p>** 如果我在主防火墙上添加用户(管理员)登录,那么/admin/login将正常工作,而另一个则不再工作。</p>
<p>当我调用<code>$authenticationUtils->getLastAuthenticationError()</code>时,我没有收到任何错误。但验证也不起作用。</p>
<p>这是我的控制器的样子:</p>
<pre class="brush:php;toolbar:false;">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(),
]);
}</pre>
<p>这是一个人遇到的相同问题:https://grafikart.fr/forum/35234,但我找不到任何解决方案。</p>
最后,我找到了答案,我会在这里发布:https://stackoverflow.com/a/42352112/8003007 我所需要做的是在两个防火墙中添加一个
context: my_context
。这是一个难以识别的选项,因为它在官方和当前的Symfony文档中并没有出现,只出现在之前的版本中,比如Symfony 3.4。