dongping4461 2016-12-22 21:34
浏览 77
已采纳

Symfony LexikJWTAuthenticationBundle无法进行身份验证

I'm encountering an issue with the JWT Auth bundle on Symfony 3. I've followed instructions from their github README but just can't seem to figure out where I've gone wrong or what is going wrong.

I am using Symfony 3.1.1, With LexikJWTAuthenticationBundle 2.0 And FriendsofSymfony userbundle.

The problem: Whenever I try to log in as instructed in their example via (with user and password substituted):

curl -X POST http://192.168.33.30/api/login_check -d _username=johndoe -d _password=test

I get:

{"code":401,"message":"Bad credentials"}

If I generate the token manually via:

        $jwtManager = $this->container->get('lexik_jwt_authentication.jwt_manager');
    $userManager = $this->container->get('fos_user.user_manager');
    $user = $userManager->findUserByEmail('emailhere');
    dump($jwtManager->create($user));

I get given quite a lengthy token. And then using that in postman as a Header with key "Authorization" value: "Bearer "

I then try to call an endpoint under the firewalled url and the failure_handler is triggered. It manages to extract from the token the data, ie the email I encoded in the token and so on. But I always get the failure.

My other data is:

security.yml security: encoders: FOS\UserBundle\Model\UserInterface: bcrypt

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN


# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
    in_memory:
        memory: ~
    fos_userbundle:
        id: fos_user.user_provider.username
        firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    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
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            # csrf_token_generator: security.csrf.token_manager

        logout:       true
        anonymous:    true

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%
# key under which the user identity will be stored in the token payload
user_identity_field: email

# token encoding/decoding settings
encoder:
    # token encoder/decoder service - default implementation based on the namshi/jose library
    service:            lexik_jwt_authentication.encoder.default
    # crypto engine used by the encoder service
    crypto_engine:  openssl
    # encryption algorithm used by the encoder service
    signature_algorithm: RS256

# token extraction settings
token_extractors:
    authorization_header:      # look for a token as Authorization Header
        enabled: true
        prefix:  Bearer
        name:    Authorization
    cookie:                    # check token in a cookie
        enabled: false
        name:    BEARER
    query_parameter:           # check token in query string parameter
        enabled: false
        name:    bearer

routing.yml

api_login_check:
path: /api/login_check

If anyone has any suggestions please let me know. I'm stumped over this.

  • 写回答

1条回答 默认 最新

  • doute3621 2016-12-23 16:30
    关注

    You need to remove the in memory user provider:

    providers:
        fos_userbundle:
             id: fos_user.user_provider.username
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R