【问题标题】:LexikJWTAuthenticationBundle | JwtToken is working but Authentication says "Full authentication is required to access this resource."LexikJWTAuthenticationBundle | JwtToken 正在工作,但身份验证显示“访问此资源需要完全身份验证”。
【发布时间】:2020-04-28 22:16:02
【问题描述】:

我的令牌登录工作正常,但如果我尝试知道我是否通过我的路线完全登录:

用户控制器.php

/**
  * @Route("/auth/me", name="userIsAuthenticated")
  */
public function authenticated()
{
    return new Response(':-)', Response::HTTP_OK);
}

它只是显示

Symfony\Component\HttpKernel\Exception\HttpException: 访问此资源需要完全身份验证。

我发现在 StackTrace 中只出现“symfony\security-http\Firewall”,而 Lexik 什么也没有?

如果令牌有效并且它完全有效,我也尝试了 jwt.io。

security.yaml

security:
    encoders:
        App\Entity\User:
            algorithm: auto

    providers:
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email

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


        login:
            pattern:  ^/auth
            stateless: true
            anonymous: true
            json_login:
                check_path:               /auth
                success_handler:          lexik_jwt_authentication.handler.authentication_success
                failure_handler:          lexik_jwt_authentication.handler.authentication_failure


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

    access_control:
         - { path: ^/auth/me, roles: IS_AUTHENTICATED_FULLY }

【问题讨论】:

  • "IS_AUTHENTICATED_FULLY 不是一个角色,但它有点像一个角色,每个登录的用户都会拥有这个。" Symfony 说“IS_AUTHENTICATED_FULLY”就像一个角色,如果你想知道用户是否登录,你可以使用它
  • 但这不是问题。我已经试过了。您可以在任何地方使用 IS_AUTHENTICATED_FULLY 角色来检查用户是否已登录。如果您使用像“ROLE_USER”这样的角色,您只需检查用户是否具有当前角色以及是否已登录。但对我来说没关系我只需要知道这家伙是否登录。所以我要求提供 IS_AUTHENTICATED_FULLY。
  • 我还是一样
  • 您的配置定义了 api 路由的提供者以使用实体提供者。我认为这个提供者不像lexik_jwt_authentication 包中定义的那样具有有效负载意识。
  • 如果我删除提供者让(因为在 lexik 上在示例安全配置中也没有提供者)它只是做同样的事情

标签: php symfony lexikjwtauthbundle


【解决方案1】:

问题是我的 /auth/me 路由将使用 /auth 防火墙。 /auth 防火墙在配置中没有 lexik 验证器。

我只需要将^/auth 更改为^/auth$ 就可以了。

$ 符号会阻止防火墙处理类似的事情

/auth/me
/auth/example
/auth/anotherexample
...

它只使用精确匹配 /auth 路由:-)

【讨论】:

    猜你喜欢
    • 2020-03-30
    • 2016-10-03
    • 2017-06-30
    • 2016-11-11
    • 2015-01-08
    • 2018-09-11
    • 2019-01-01
    • 2020-11-21
    • 2016-07-21
    相关资源
    最近更新 更多