Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SimplePreAuthenticatorInterface对BadCredentialsException抛出500错误_Php_Security_Symfony - Fatal编程技术网

Php SimplePreAuthenticatorInterface对BadCredentialsException抛出500错误

Php SimplePreAuthenticatorInterface对BadCredentialsException抛出500错误,php,security,symfony,Php,Security,Symfony,我对安全定制symfony这个主题还很陌生,根据指南,我创建了一个简单的方法来验证我的API 如果用户未发送带有令牌的特殊头,应用程序将抛出 使用Symfony\Component\Security\Core\Exception\BadCredentialsException; 使用Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken; 使用Symfony\Component\HttpFoundati

我对安全定制symfony这个主题还很陌生,根据指南,我创建了一个简单的方法来验证我的API

如果用户未发送带有令牌的特殊头,应用程序将抛出

使用Symfony\Component\Security\Core\Exception\BadCredentialsException;
使用Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
使用Symfony\Component\HttpFoundation\Request;
//...
公共函数createToken(请求$Request,$providerKey)
{
如果(!$request->headers->has(self::API_令牌)){
抛出新的BadCredentialsException(“未找到API键”);
}
返回新的预验证数据TOKEN('anon',$request->headers->get(self::API_-TOKEN),$providerKey);
}
我不知道应该返回什么symfony,但我知道一定是401错误,而不是500错误。如何处理这个问题,我需要编写自己的
SimplePreAuthenticationListener
?当我读到
Simple*AuthenticatorInterface
背后的思想时,请尝试在Authenticator类中实现Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface

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