如何在AuthenticationSuccessHandler中取消身份验证?

时间:2014-01-09 21:07:52

标签: php symfony

我创建了一个从DefaultAuthenticationSuccessHandler扩展的子类,以便在身份验证过程中添加自定义功能。如果不符合条件,我想停止身份验证过程。我该怎么做?

public function onAuthenticationSuccess( Request $request, TokenInterface $token) {
    if(some_condition_applies){
        //if success, resume default flow
        return parent::onAuthenticationSuccess( $request, $token);
    }else{
        //how to fail the authentication here?
    }
}

2 个答案:

答案 0 :(得分:2)

AuthenticationSuccessHandler的主要目的是在用户通过身份验证后执行某些操作。换句话说,对它做任何事都为时已晚。

您可以创建自定义身份验证提供程序,以处理您需要的所有身份验证逻辑。

http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html#the-authentication-provider

答案 1 :(得分:0)

根据您的设置,Symfony 2.4中新的SimpleFormAuthenticator功能可以帮助您:How to Create a Custom Form Password Authenticator

这可能仅适用于您的身份验证机制是基于用户名/密码的(不是OAuth或其他)。

此外,您可能只是实现AdvancedUserInterface,然后从“isEnabled”之类的方法返回“false”以阻止登录(Forbid Inactive Users)。唯一的缺点是您只能访问User类中的数据。如果你需要的东西超过这个,上面的方法应该工作(但是更多的工作)。

干杯!