좋아, 번들의 코드를 좀 더 파낸 후, 나는 그것을 알아 냈습니다. 문제는 Symfony의 HTTP 기본 인증 장애에 의해 잘못된 자격 증명을 처리하는 방식으로 인해 발생합니다. 그만큼 401 Bad Credentials
응답은 사용자 정의 응답입니다 BasicAuthenticationEntryPoint
, 이것은 BasicAuthenticationListener
'에스 handle
AN 직후 기능 AuthenticationException
같은 기능으로 던져졌습니다. 따라서 청취자와 함께이 예외를 포착하는 방법은 없습니다.
public function handle(GetResponseEvent $event)
{
$request = $event->getRequest();
if (false === $username = $request->headers->get('PHP_AUTH_USER', false)) {
return;
}
if (null !== $token = $this->securityContext->getToken()) {
if ($token instanceof UsernamePasswordToken && $token->isAuthenticated() && $token->getUsername() === $username) {
return;
}
}
if (null !== $this->logger) {
$this->logger->info(sprintf('Basic Authentication Authorization header found for user "%s"', $username));
}
try {
$token = $this->authenticationManager->authenticate(new UsernamePasswordToken($username, $request->headers->get('PHP_AUTH_PW'), $this->providerKey));
$this->securityContext->setToken($token);
} catch (AuthenticationException $failed) {
$this->securityContext->setToken(null);
if (null !== $this->logger) {
$this->logger->info(sprintf('Authentication request failed for user "%s": %s', $username, $failed->getMessage()));
}
if ($this->ignoreFailure) {
return;
}
$event->setResponse($this->authenticationEntryPoint->start($request, $failed));
}
}
진입 점 start
함수는 예외없이 사용자 정의 응답을 만듭니다.
public function start(Request $request, AuthenticationException $authException = null)
{
$response = new Response();
$response->headers->set('WWW-Authenticate', sprintf('Basic realm="%s"', $this->realmName));
$response->setStatusCode(401, $authException ? $authException->getMessage() : null);
return $response;
}
주먹 if
-Clause handle
위의 기능은 또한 "사용자 자격 증명이 전혀 없음"의 경우에 작동하는 이유를 설명합니다.이 경우 청취자는 사용자를 인증하려고하지 않기 때문에 Symfony의 방화벽 리스너가 예외를 던지기 때문입니다 (어디에 있는지 확실하지 않습니다. 정확히), so fosrestbundle 's AccessDeniedListener
잡을 수 있습니다 AuthenticationException
그리고 그 일을하십시오.