2014-04-17 3 views
2

Symfony로 만든 REST API에서 WSSE를 사용하여 사용자를 인증하려고 할 때 문제가 있습니다. 사이트 symfony wsse 인증 (http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html)에 대한 가이드를 따르고 튜토리얼 (http://obtao.com/blog/2013/06/configure-wsse-on-symfony-with-fosrestbundle/)을 완료했으며 사용자 관리가 FOSUserBundle로 처리됩니다.FOSUserBundle을 사용한 WSSE 인증

기본적으로 I wsse는 리소스/api에 액세스하는 요청을 인증합니다. 그래서 난 내 security.yml

security: 
    encoders: 
     FOS\UserBundle\Model\UserInterface: sha512 

    role_hierarchy: 
     ROLE_ADMIN:  ROLE_USER 
     ROLE_SUPER_ADMIN: ROLE_ADMIN 

    providers: 
     fos_userbundle: 
      id: fos_user.user_provider.username 

    firewalls: 
     wsse_secured: 
      pattern: /api/.* 
      stateless: true 
      wsse:  true 
      anonymous : false 

     #dev: 
     # pattern: ^/(_(profiler|wdt)|css|images|js)/ 
     # security: false 


     main: 
      pattern: ^/ 
      form_login: 
       provider: fos_userbundle 
       csrf_provider: form.csrf_provider 
      logout:  true 
      anonymous: true 

    access_control: 
     - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/admin/, role: ROLE_ADMIN } 

그리고 액세스 할 수있는 내 자원/API/안녕, 내 요청 헤더에 추가 :

Authorization: Authorization profile=”UsernameToken” 
x-wsse: UsernameToken Username="foo", PasswordDigest="9/dyW92Vp+mWEbyXeblRMqTQSJc=", Nonce="MDc1MGNkZjAwMjNmZjk2YQ==", Created="2014-04-17T16:18:34Z" 

그러나 쿼리를 보낸 후 나는를 얻을 수 나에게 반환되는 오류 :

WSSE Login failed for foo. Why? No Authentication Provider found for token of class "Acme \ UserBundle \ Security \ Authentication \ Token \ WsseUserToken". 

이 오류 메시지는 내 WsseListener 클래스에서 제기 예외입니다 :

try { 
     $authToken = $this->authenticationManager->authenticate($token); 
     $this->securityContext->setToken($authToken); 
    return; 
    } catch (AuthenticationException $failed) { 
     $failedMessage = 'WSSE Login failed for '.$token->getUsername().'. Why ? '.$failed->getMessage(); 
     // Deny authentication with a '403 Forbidden' HTTP response 
     $response = new Response(); 
     $response->setStatusCode(403); 
     $response->setContent($failedMessage); 
     $event->setResponse($response); 
     return; 
    } 

답변

1

좋아, 난 그냥있는 문제 ...

(WsseProvider 클래스), I 아무것도 돌려주지 않았다! 전

(하지 않습니다 작품) :

public function authenticate(TokenInterface $token) 
{ 
    $user = $this->userProvider->loadUserByUsername($token->getUsername()); 
    if(!$user){ 
     throw new AuthenticationException("Bad credentials... Did you forgot your username ?"); 
    } 
    if ($user && 
     $this->validateDigest($token->digest, $token->nonce, $token->created, $user->getPassword())) { 
    } 
} 

AFTER (작품) :

public function authenticate(TokenInterface $token) 
    { 
     $user = $this->userProvider->loadUserByUsername($token->getUsername()); 
     if(!$user){ 
      throw new AuthenticationException("Bad credentials... Did you forgot your username ?"); 
     } 
     if ($user && $this->validateDigest($token->digest, $token->nonce, $token->created, $user->getPassword())) { 
     $authenticatedToken = new WsseUserToken($user->getRoles()); 
     $authenticatedToken->setUser($user); 

     return $authenticatedToken; 
    } 
    throw new AuthenticationException('The WSSE authentication failed.'); 
} 

모든 이제 OK입니다!

0

인증 헤더가 아파치에 의해 제거되지 않도록하십시오 (기본적으로 제거됩니다).

당신은 당신의 .htaccess 파일에 다음 줄을 추가하여 강제 할 수 있습니다 : 내 authentificate 방법에

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]