doctrine 2 - 用户提供者必须是WebserviceUserRepository的实例

i wanted to create an Custom EntityRepository to load a User from the Database, so i followed http://symfony.com/doc/current/cookbook/security/entity_provider.html this tutorial.

Now i get a exception and i do not know why:

{"error":{"code":500,"message":"Internal Server Error","exception":[{"message":"The user provider must be an instance of WebserviceUserRepository (Symfony\\Bridge\\Doctrine\\Security\\User\\EntityUserProvider was given)."

If I remove extends EntityRepository from my WebserviceUser repo and also change security.ml and services.yml than the authentication works and the class is an instanceof WebserviceUserRepository.

I am using Symfony 2.7

I hope somebody can help me with that i am starting to pull my hair out ;-)

My Api Key Authenticator looks like that:

class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface,     

AuthenticationFailureHandlerInterface {

public function createToken(Request $request, $providerKey)
{
    // look for an apikey query parameter
    $apiKey = $request->query->get('apikey');

    // or if you want to use an "apikey" header, then do something like     this:
    // $apiKey = $request->headers->get('apikey');

    if (!$apiKey) {
        throw new BadCredentialsException('No API key found');

        // or to just skip api key authentication
        //return null;
    }

    return new PreAuthenticatedToken(
        'anon.',
        $apiKey,
        $providerKey
    );
}

public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{

    **if (!$userProvider instanceof WebserviceUserRepository) {**
        throw new \InvalidArgumentException(
            sprintf(
                'The user provider must be an instance of WebserviceUserRepository (%s was given).',
                get_class($userProvider)
            )
        );
    }

    $apiKey = $token->getCredentials();
    $username = $userProvider->getUsernameForApiKey($apiKey);

    if (!$username) {
        throw new AuthenticationException(
            sprintf('API Key "%s" does not exist.', $apiKey)
        );
    }

    $user = $userProvider->loadUserByUsername($username);

    return new PreAuthenticatedToken(
        $user,
        $apiKey,
        $providerKey,
        $user->getRoles()
    );
}

public function supportsToken(TokenInterface $token, $providerKey)
{
    return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
}

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

this is my user repo:

class WebserviceUserRepository extends EntityRepository implements  UserProviderInterface {

public function getUsernameForApiKey($apiKey)
{
    // Look up the username based on the token in the database, via
    // an API call, or do something entirely different

    $username = 'username';

    return $username;
}

public function loadUserByUsername($username)
{
    // make a call to your webservice here
    $userData = "";
    // pretend it returns an array on success, false if there is no user

    if (false) {
        $password = 'password';
        $salt = '';
        $roles = array('roles' => 'ROLE_ADMIN');

        return new WebserviceUser($username, $password, $salt, $roles);
    }

        throw new UsernameNotFoundException(
            sprintf('Username "%s" does not exist.', $username)
        );

}

public function refreshUser(UserInterface $user)
{
    $class = get_class($user);
    if (!$this->supportsClass($class)) {
        throw new UnsupportedUserException(
            sprintf(
                'Instances of "%s" are not supported.',
                $class
            )
        );
    }

    return $this->find($user->getUsername());
}

public function supportsClass($class)
{
    return $this->getEntityName() === $class
    || is_subclass_of($class, $this->getEntityName());
}
}

i also added the repository class to my entity object:

 * @ORM\Table()
 *     @ORM\Entity(repositoryClass="Moera\RestBundle\Entity\WebserviceUserRepository")
 */
class WebserviceUser implements UserInterface, \Serializable, EquatableInterface

and i also registered bot classes in security.yml and services.yml:

services:
  webservice_user_provider:
    class: Moera\RestBundle\Entity\WebserviceUserRepository

apikey_authenticator:
    class: Moera\RestBundle\Security\ApiKeyAuthenticator
    public: false

security:
  encoders:
    Moera\RestBundle\Entity\WebserviceUser:
        algorithm: bcrypt

  providers:
    webservice_user_provider:
        entity:
            class: MoeraRestBundle:WebserviceUser

  firewalls:
    secured_area:
        pattern: ^/
        stateless: true
        simple_preauth:
            authenticator: apikey_authenticator
        provider: webservice_user_provider

Thank You very much!

Kind Regards, Johannes