使用PreAuthentication时,会覆盖自定义异常

时间:2015-03-26 10:25:29

标签: php api rest symfony authentication

我正在关注How to Authenticate Users with API Keys教程。

当我提供正确的API密钥时,一切正常。另一方面,当API密钥不正确或丢失时,代码应该抛出异常(例如第一种情况下的BadCredentialsException)。但是,此异常似乎被覆盖,最终请求响应是Symfony AccessListener.php抛出的AuthenticationCredentialsNotFoundException。 我收到错误:

A Token was not found in the SecurityContext.

这让我认为PreAuthenticator抛出的异常以某种方式被丢弃,安全机制只会进一步说它没有找到令牌(鉴于提供的API密钥不正确,这确实是正常的。)

如果这是预期的行为(我感到很难过),建议在文档中使用异常的PreAuthenticator是没有意义的......

1 个答案:

答案 0 :(得分:0)

要解决此问题,我创建一个自定义身份验证提供程序,它工作正常!

教程:http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html