找不到我做出授權由ApiKey
,我想401 Unauthorized
如果沒有授權的數據呈現,並且403 Forbidden
如果授權數據是無效的。但是在兩種情況下我都得到了500 Internal Server Error
。安全throwns 500例外與令牌在TokenStorage,而不是403或401
security.yml:
security:
providers:
api_key_user_provider:
entity:
class: RestBundle:RestUser
property: apikey
firewalls:
rest_api_area:
pattern: ^/api
stateless: true
rest_auth:
header: x-apikey
provider: api_key_user_provider
access_control:
- { path: ^/api, roles: ROLE_REST_USER }
RestUserListener.php:
class RestUserListener implements ListenerInterface
{
protected $tokenStorage;
protected $authenticationManager;
private $header;
function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, $header)
{
$this->tokenStorage = $tokenStorage;
$this->authenticationManager = $authenticationManager;
$this->header = $header;
}
public function handle(GetResponseEvent $event)
{
$request = $event->getRequest();
$apikey = $request->headers->get($this->header);
if (!$apikey) return;
$token = new RestUserToken();
$token->setUser($apikey);
$authToken = $this->authenticationManager->authenticate($token);
$this->tokenStorage->setToken($authToken);
return;
}
}
RestUserAuthenticationProvider.php:
class RestUserAuthenticationProvider implements AuthenticationProviderInterface
{
private $userProvider;
public function __construct(UserProviderInterface $userProvider)
{
$this->userProvider = $userProvider;
}
public function authenticate(TokenInterface $token)
{
$user = $this->userProvider->loadUserByUsername($token->getUsername());
if ($user)
{
$authenticatedToken = new RestUserToken($user->getRoles());
$authenticatedToken->setUser($user);
return $authenticatedToken;
}
throw new AuthenticationException("Apikey not found.");
}
public function supports(TokenInterface $token)
{
return $token instanceof RestUserToken;
}
}
RestUserToken爲AbstractToken
一樣簡單,並且沒有額外的邏輯。
api_key_user_provider是RestUser
RestUserFactory的apikey
屬性標識的標準實體提供商也沒有額外的魔法裏面,就像官方文檔中
謝謝!真的很好的答案。我已經讓'hadle()'拋出'AuthenticationException',它在'KernelExceptionListener'中轉換爲適當的'HTTP'錯誤。 – Neka
@Neka那麼它工作? :) –
是的,它運作良好。但是,如果我想添加一個更多的身份驗證方法到'^/api'?你的解決方案將阻止所有其他auth方法,因爲它會拋出異常,並沒有給其他安全監聽器一個機會,不是嗎? – Neka