之前记录了一点关于login_form的,其中说到了authenticator,这里再记录几个注意点:
修改security.yaml
设置:enable_authenticator_manager: true
form_login: #这个会调用内置的 Symfony\Component\Security\Http\Authenticator\FormLoginAuthenticator
success_handler: App\Security\LoginSuccessHandler #由于不能直接修改 内置Authenticator的 onAuthenticationSucces 回调事件,可以通过form_login的success_handler来定义,如写 用户cookie信息
username_parameter: email
password_parameter: password
打印输出:
provider security.user.provider.concrete.main #这个其实是UserEntity
context main
entry_point security.authenticator.form_login.main
user_checker security.user_checker
access_denied_handler (none)
access_denied_url (none)
authenticators
[▼
"form_login"
]
注释掉 form_login,然后添加自定义Authenticator
custom_authenticator: App\Security\FormLoginAuthenticator #可以在这个 onAuthenticationSucces 里添加 授权成功以后的事件操作,如写 用户cookie信息
打印输出:
provider security.user.provider.concrete.main #这个其实是UserEntity
context main
entry_point App\Security\FormLoginAuthenticator
user_checker security.user_checker
access_denied_handler (none)
access_denied_url (none)
authenticators
[▼
"App\Security\FormLoginAuthenticator"
]
同一个firewall下,上面两个Authenticator不能同时存在,否则会报错:
Because you have multiple authenticators in firewall "main", you need to set the "entry_point" key to one of your authenticators ("App\Security\FormLoginAuthenticator", "form_login") or a service ID impleme
nting "Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface". The "entry_point" determines what should happen (e.g. redirect to "/login") when an anonymous user tries to access a pro
tected page
需要手动设置entry_point,来定义使用哪个Authenticator
另一种定义了多个 authenticator 的场景,extends 自不同的Authenticator,而不指定 entry_point 是可以的
main:
# lazy: true
pattern: ^/
provider: main
# form_login:
# success_handler: App\Security\LoginSuccessHandler
# username_parameter: email
# password_parameter: password
# custom_authenticator: App\Security\FormLoginAuthenticator #当只有一个自定义的 authenticator 的时候,可以直接这样key value 定义,多个的话就用下面的数组定义
custom_authenticators:
- App\Security\FormLoginAuthenticator # AbstractLoginFormAuthenticator
- App\Security\RedirectAuthenticator # AbstractAuthenticator
# entry_point: App\Security\FormLoginAuthenticator
logout:
invalidate_session: true
path: /logout
security: true
查看指定 firewall下的配置信息: php bin/console debug:firewall main
关于Authenticator的调用源码:Symfony\Component\Security\Http\Authentication\AuthenticatorManager # 重点在 147行 executeAuthenticators
