So, there's no 'official' pre-login event. But thankfully it's not hard to set one up since Symfony2 is so extendable. The trick is to use your own service to handle authentication.
Symfony uses this class when using a login form:
SymfonyComponentSecurityHttpFirewallUsernamePasswordFormAuthenticationListener
If you override the security.authentication.listener.form.class
parameter (originally defined in SymfonyBundleSecurityBundleResourcesconfigsecurity_listeners.xml
) you can use a custom listener that extends UsernamePasswordFormAuthenticationListener
.
All that's left to do is override the attemptAuthentication()
method to dispatch the custom event.
(Actually you also need to store the event dispatcher as a class property in __construct()
)
This method should work with other authentication methods - all you'd need to do is modify the appropriate listener (ie BasicAuthenticationListener
, X509AuthenticationListener
, etc.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…