2014-07-20 44 views
0

我對安全定製symfony的主題非常陌生,遵循指南我創建了一個簡單的方法來驗證我的API。SimplePreAuthenticatorInterface在BadCredentialsException中拋出500錯誤

的情況下,用戶不要發送一個特殊的頭與令牌應用程序拋出一個

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*AuthenticatorInterface想法是avoid that

回答

1

嘗試在你的鑑定者類實現的Symfony \分量\安全\ HTTP \認證\ AuthenticationFailureHandlerInterface

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