【问题标题】:JWT authentification with Symfony 2.7使用 Symfony 2.7 进行 JWT 身份验证
【发布时间】:2015-06-29 07:33:56
【问题描述】:

我正在尝试在我的 Symfony 2.7 应用程序中使用 LexikJWTAuthenticationBundleFOSUserBundle

基本上,FOSUserBundle 配置工作正常:我正确加载了我的装置,如果我尝试通过登录表单登录它会成功。

从 login_check 获取令牌也可以。然后我将带有“Bearer”的授权标题放在标题中并尝试访问另一个页面。它总是产生 401 响应。

有没有人知道可能是什么问题或如何进行调试?

这是我的配置:

# app/config/config.yml

# DunglasJsonLdApi
dunglas_json_ld_api:
    title:       %api_name%
    description: %api_description%
    default:
        order: DESC

# FOSUserBundle
fos_user:
    db_driver:     orm
    firewall_name: api
    user_class:    ApiBundle\Bundles\UserBundle\Entity\User

# LewikJWTAuthentificationBundle
lexik_jwt_authentication:
    private_key_path: %kernel.root_dir%/config/jwt/private.pem
    public_key_path:  %kernel.root_dir%/config/jwt/public.pem
    pass_phrase:      %jwt_pass_phrase%
    token_ttl:        86400
# app/config/routing.yml

# DunglasJsonLdBundle
api_doc:
    resource: @DunglasJsonLdApiBundle/Resources/config/routing.xml
    prefix:   /api

api:
    resource: .
    type:     json-ld
    prefix:   /api

# FOSUserBundle
fos_user_security_login:
    path:     /login
    defaults: { _controller: FOSUserBundle:Security:login }

fos_user_security_check:
    path:     /api/login_check
    defaults: { _controller: FOSUserBundle:Security:check }

fos_user_security_logout:
    path:     /logout
    defaults: { _controller: FOSUserBundle:Security:logout }
# app/config/security.yml

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

    role_hierarchy:
        ROLE_CA:          ROLE_USER
        ROLE_SUPER_ADMIN: [ ROLE_CA, ROLE_ALLOWED_TO_SWITCH ]

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

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

        login:
            pattern:   ^/login|^/api/login
            provider:  fos_userbundle
            stateless: true
            anonymous: true
            form_login:
                login_path:               fos_user_security_login
                check_path:               fos_user_security_check
                username_parameter:       username
                password_parameter:       password
                success_handler:          lexik_jwt_authentication.handler.authentication_success
                failure_handler:          lexik_jwt_authentication.handler.authentication_failure
                require_previous_session: false

        api:
            pattern:   ^/api
            provider:  fos_userbundle
            stateless: true
            anonymous: true
            lexik_jwt:

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, role: IS_AUTHENTICATED_FULLY }

完整的应用程序可用here

【问题讨论】:

    标签: symfony jwt symfony-2.7 lexikjwtauthbundle


    【解决方案1】:

    现在看来可以正常使用了。自从我发布我的问题以来,我没有改变任何东西,在发布之前我重新启动了我的机器,重新启动了 MySQL、nginx、PHP5-FPM 并删除了 Symfony 缓存,所以我真的不明白发生了什么......

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 2023-03-27
      • 2021-05-29
      • 2017-07-30
      • 2018-06-30
      • 2018-08-03
      • 2021-02-06
      • 2022-12-03
      • 1970-01-01
      相关资源
      最近更新 更多