简体   繁体   English

SimplePreAuthenticatorInterface在BadCredentialsException上引发500错误

[英]SimplePreAuthenticatorInterface throws an 500 error on BadCredentialsException

I'm really new to the topic of security customization symfony, following the guides I've created a simple way to authenticate my API. 我真的对安全性定制symfony主题感到陌生,遵循我创建的简单指南对API进行身份验证的指南。

in case the user dont sent a special header with the token the app throws a 如果用户未发送带有令牌的特殊标头,则应用会抛出一个

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);
}

i dont know exactly what should return symfony, but i know must be a 401 error and not a 500. how i can handle this, I need write my own SimplePreAuthenticationListener ? 我不知道到底应该返回symfony的什么,但我知道必须是401错误而不是500。我如何处理此错误,我需要编写自己的SimplePreAuthenticationListener吗? As i read the idea behind Simple*AuthenticatorInterface is avoid that 当我阅读Simple*AuthenticatorInterface背后的想法时,应避免这种情况

Try implementing Symfony\\Component\\Security\\Http\\Authentication\\AuthenticationFailureHandlerInterface in your Authenticator class 尝试在Authenticator类中实现Symfony \\ Component \\ Security \\ Http \\ Authentication \\ AuthenticationFailureHandlerInterface

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM