【问题标题】:SimplePreAuthenticatorInterface throws an 500 error on BadCredentialsExceptionSimplePreAuthenticatorInterface 在 BadCredentialsException 上引发 500 错误
【发布时间】:2014-07-20 18:01:01
【问题描述】:

我对安全定制 symfony 的主题真的很陌生,按照我创建的指南,我创建了一种简单的方法来验证我的 API。

如果用户没有发送带有令牌的特殊标头,应用程序会抛出一个

use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
use Symfony\Component\HttpFoundation\Request;
//...

public function createToken(Request $request, $providerKey)
{
    if (!$request->headers->has(self::API_TOKEN)) {
        throw new BadCredentialsException('No API key found');
    }

    return new PreAuthenticatedToken('anon.', $request->headers->get(self::API_TOKEN), $providerKey);
}

我不知道什么应该返回 symfony,但我知道一定是 401 错误而不是 500。我该如何处理这个问题,我需要自己写 SimplePreAuthenticationListener?当我读到Simple*AuthenticatorInterfaceavoid that背后的想法时,

【问题讨论】:

    标签: php security symfony


    【解决方案1】:

    尝试在您的 Authenticator 类中实现 Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        return new Response("Authentication Failed.", 403);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 2022-01-19
      • 2018-06-13
      • 1970-01-01
      • 2016-08-12
      • 2020-06-07
      相关资源
      最近更新 更多