为Symfony2.8配置JWT时出错

时间:2017-01-13 07:00:03

标签: symfony

您好我需要使用JWT(Json Web令牌)与我的symfony2.8进行Restfull服务身份验证。所以我遵循JWT参考站点的以下指南:

https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md

以下是我的步骤:

1: configure it in AppKarnal.php
new Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle(),

2:jenerated keys on rsp[ective place app/var/jwt

3: Configure the SSH keys path in your config.yml :

lexik_jwt_authentication:
jwt_private_key_path: '%kernel.root_dir%/var/jwt/private.pem'   # ssh    private key path
jwt_public_key_path:  '%kernel.root_dir%/var/jwt/public.pem'    # ssh    public key path
jwt_key_pass_phrase:  'mypass'                                      # ssh key pass phrase if present
jwt_token_ttl:        86400

4: 配置parameters.yml.dist:

jwt_private_key_path: '%kernel.root_dir%/../var/jwt/private.pem' # ssh    private key path
jwt_public_key_path:  '%kernel.root_dir%/../var/jwt/public.pem'  # ssh public key path
jwt_key_pass_phrase:  ''                                         # ssh key pass phrase

jwt_token_ttl:3600

5:配置security.yml:

     login:
                    pattern:  ^/api/login
                    stateless: true
                    anonymous: true
                    form_login:
                        check_path:               /api/login_check
                        success_handler:          lexik_jwt_authentication.handler.authentication_success
                        failure_handler:          lexik_jwt_authentication.handler.authentication_failure
                        require_previous_session: false

                api:
                    pattern:   ^/api
                    stateless: true
                    guard:
                        authenticators:
                            - lexik_jwt_authentication.jwt_token_authenticator

            access_control:
                - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
                - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }

6:配置您的routing.yml:

api_login_check:
path: /api/login_check

但即使我已经正确地遵循了每一步,我也会遇到以下错误:

There is no extension able to load the configuration for "lexik_jwt_authentication" (in /var/www/html/EduboldApp/eduboldportal/app/config/config.yml). Looked for namespace "lexik_jwt_authentication", found "framework", "security", "twig", "monolog", "swiftmailer", "doctrine", "sensio_framework_extra", "doctrine_mongodb", "fos_rest", "debug", "web_profiler", "sensio_distribution" in /var/www/html/EduboldApp/eduboldportal/app/config/config.yml (which is being imported from "/var/www/html/EduboldApp/eduboldportal/app/config/config_dev.yml").

请帮助我缺少的东西。

我正在使用symfony2.8和ODM

提前谢谢

3 个答案:

答案 0 :(得分:1)

尝试在config.yml中配置SSH密钥路径,如下所示:

lexik_jwt_authentication:
    jwt_private_key_path: '%kernel.root_dir%/var/jwt/private.pem'   # ssh    private key path
    jwt_public_key_path:  '%kernel.root_dir%/var/jwt/public.pem'    # ssh    public key path
    jwt_key_pass_phrase:  'mypass'                                      # ssh key pass phrase if present
    jwt_token_ttl:        86400

确保在lexik_jwt_authentication之后有选项卡:

答案 1 :(得分:1)

您的AppKernel.php看起来如何?

JWT捆绑在那里注册了吗?

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            ...
            new Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle(),
            ...

        ];

        if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
            ...
        }

        return $bundles;
    }

我猜你已经安装了捆绑包? :)

composer require lexik/jwt-authentication-bundle

此外,我认为您需要从config.yml中的jwt_jwt_private_key_pathjwt_key_pass_phrase中删除jwt_token_ttl

看看https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#configuration

所以,你的config.yml应该是这样的:

lexik_jwt_authentication:
    private_key_path: '%jwt_private_key_path%'
    public_key_path:  '%jwt_public_key_path%'
    pass_phrase:      '%jwt_key_pass_phrase%'
    token_ttl:        '%jwt_token_ttl%'

答案 2 :(得分:0)

只需在parameters.yml上的* jwt键上创建4个空格即可解析它。请记住,yml文件是面向身份的。