[英]Symfony 5.4 security multiple firewalls
我正在嘗試將用戶的身份驗證與管理員的身份驗證分開。
所以我創建了 2 個防火牆和 2 個不同的訪問控制。
我的 security.yaml 看起來像這樣:
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 }
在主防火牆上一切正常,但是當我使用用戶(管理員)防火牆提交按鈕時,登錄頁面會自行刷新並且沒有任何反應。 我沒有任何錯誤。
** 如果我在主防火牆上添加用戶(管理員)登錄,那么 /admin/login 將正常工作,而另一個將不再工作。
當我調用$authenticationUtils->getLastAuthenticationError()
時,我沒有收到任何錯誤。 但是驗證也不起作用。
這就是我的 Controller 的樣子:
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.