如何在Symfony3中解决ServiceNotFoundException

时间:2017-09-07 09:08:33

标签: php symfony jwt

错误

 ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php 
 The service "token_authenticator" has a dependency on a non-existent service "lexik_jwt_authentication.jwt_encoder".

我需要什么

  • 我需要使用symfony验证angular2 app。

config.yml

lexik_jwt_authentication:
private_key_path: '%kernel.root_dir%/../var/jwt/private.pem'
public_key_path:  '%kernel.root_dir%/../var/jwt/public.pem'
pass_phrase:      '%jwt_key_pass_phrase%'
token_ttl: 3600

security.yml

 firewalls:
    main:
        pattern: ^/
        logout:       true
        anonymous:    true
        stateless: true

        guard:
            authenticators:
                - 'token_authenticator'

services.yml

 services:

 token_authenticator:
    class: AcmeStoreBundle\Security\TokenAuthenticator
    arguments: ['@lexik_jwt_authentication.jwt_encoder', '@doctrine_mongodb']

的routing.yml

 acme_store_login_user:
   type: rest
   path:     /login_check
   defaults: { _controller: AcmeStoreBundle:Login:login }
name_prefix:  api_

登录控制器代码

  public function loginAction(Request $request) {

   $data = json_decode(file_get_contents('php://input'), true);
    $userName = $data['username'];
    $password = $data['password'];

    $user = $this->get('doctrine_mongodb')
            ->getRepository('AcmeStoreBundle:User')
            ->findOneBy(['username' => $userName]);

    if (!$user) {
        throw $this->createNotFoundException();
    }

    $isValid = $this->get('security.password_encoder')
            ->isPasswordValid($user, $password);

    if (!$isValid) {
        throw new BadCredentialsException();
    }

    $response = new Response(Response::HTTP_OK);
    $token = $this->getToken($user);

    $response = new Response($this->serialize(['token' => $token]), Response::HTTP_OK);
    return $this->setBaseHeaders($response);
}


public function serialize($data) {
    $context = new SerializationContext();
    $context->setSerializeNull(true);

    return $this->get('jms_serializer')
                    ->serialize($data, 'json', $context);
}

public function getToken(User $user) {

    return $this->container->get('lexik_jwt_authentication.encoder')
                    ->encode([
                        'username' => $user->getUsername(),
                        'exp' => time() + 3600 ,
    ]);
}

参考:

https://github.com/chalasr/lexik-jwt-authentication-sandbox    https://knpuniversity.com/screencast/symfony-rest4/create-json-web-token#play

  • 任何人都可以建议我如何解决这个问题。

2 个答案:

答案 0 :(得分:3)

您正在使用2.x版本,其中(从更改日志中可以看到)lexik_jwt_authentication.jwt_encoder服务(来自1.x版本)已不复存在。您应该使用lexik_jwt_authentication.encoder.default

  

已删除服务lexik_jwt_authentication.jwt_encoder   赞成支持的lexik_jwt_authentication.encoder.default   OpenSSL和phpseclib加密引擎。

 token_authenticator:
    class: Acme\StoreBundle\Security\TokenAuthenticator
    arguments: ['@lexik_jwt_authentication.encoder.default', '@doctrine_mongodb']

答案 1 :(得分:0)

我在 Symfony 4 中遇到了同样的问题,因为我的LoginConroller扩展了BaseController,因此请确保您的LoginController扩展了 Controller Class 。像这样:

class LoginController extends Controller {
  .......

}