Symfony api平台忽略jwt令牌

发布于 2025-01-09 01:55:39 字数 1064 浏览 0 评论 0原文

我正在使用 jwt (LexikJWTAuthenticationBundle) 创建 symfony api (api 平台)

登录效果很好。我将其添加到 security.yaml

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

服务器返回我令牌。但是,当我创建安全源(如“

- { path: ^/api/notes, roles: IS_AUTHENTICATED_FULLY }

并使用带有授权的令牌发送请求”(如

Bearer --token--

“服务器”)时,忽略它并返回 “访问此资源需要完全身份验证。”

我怎样才能识别出了什么问题?我花了5个小时,但我不知道我能做什么。

我使用 symfony server:start 运行服务器

当我创建测试控制器时,

 #[Route('/test', name: 'test')]
public function index(Request $request): Response
{
    $auth = $request->headers->get("Authorization");
    return new Response("Authorization: ". "'".$auth."'");
}

它可以工作,服务器打印我的令牌

Authorization: 'Bearer eyJ0eXAiOiJKV1QiLCJh.....'

Iam creating symfony api (api platform) with jwt (LexikJWTAuthenticationBundle)

Login works great. I added this into security.yaml

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

Server return me token. But when I create secure source like

- { path: ^/api/notes, roles: IS_AUTHENTICATED_FULLY }

And send request with token with authorization like

Bearer --token--

Server ignore it and return
"Full authentication is required to access this resource."

How can i recognize whats wrong? I spent 5 hours with it, but I dont know, what I can do.

I Run server with symfony server:start

When I created test controller like

 #[Route('/test', name: 'test')]
public function index(Request $request): Response
{
    $auth = $request->headers->get("Authorization");
    return new Response("Authorization: ". "'".$auth."'");
}

It works, server print me token

Authorization: 'Bearer eyJ0eXAiOiJKV1QiLCJh.....'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮云壑 2025-01-16 01:55:39

我认为你缺少一些配置。您仅配置了 json_login 选项,以便 Lexik 包现在负责处理用户登录并发出 JWT。默认情况下,API 平台仍将基于基于会话的 cookie 来验证请求,而不是通过 Authorization 标头。您必须告诉 API 平台根据您的 JWT 对请求进行身份验证。

我相信将其添加到您的 security.yml 中应该足够了,但我还没有检查这一点:(

firewalls:
    main:
        stateless: true
        provider: app_user_provider <-- this has to point to your user provider
        json_login:
            check_path: /api/login_check
            success_handler: lexik_jwt_authentication.handler.authentication_success
            failure_handler: lexik_jwt_authentication.handler.authentication_failure
        jwt: ~

删除 pattern,更改 login 到 json_login 并添加 jwt: ~

请查看此文档:https://api-platform.com/docs/core/jwt/ 其中展示了如何使用 JWT 保护 API 平台。

I think you're missing some configuration. You only configured the json_login options, so that the Lexik bundle is now responsible for handling the logging in of your users and issuing JWTs. By default, API platform will still authenticate requests based on session-based cookies, not through the Authorization header. You have to tell API Platform to authenticate requests based on your JWTs.

I believe that it should be enough to add this to your security.yml, but I haven't checked this:

firewalls:
    main:
        stateless: true
        provider: app_user_provider <-- this has to point to your user provider
        json_login:
            check_path: /api/login_check
            success_handler: lexik_jwt_authentication.handler.authentication_success
            failure_handler: lexik_jwt_authentication.handler.authentication_failure
        jwt: ~

(drop the pattern, change login to json_login and add jwt: ~)

Have a look at this documentation: https://api-platform.com/docs/core/jwt/ where it shows how to secure the API platform with JWTs.

梦毁影碎の 2025-01-16 01:55:39

我遇到了同样的问题 Sylius 的 API Platform 和第二个 GraphQL API /api/v2/graphql(除了 Api 平台 REST API /api/v2)。

在 Symfony 分析器中,graphQL 请求的安全部分被禁用。

graphql 端点需要自己的防火墙:

security.yaml

security:
    firewalls:
        graphql_shop_user:
            pattern: "%sylius.security.new_api_route%/graphql"
            provider: sylius_api_shop_user_provider
            stateless: true
            anonymous: true
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator

I had the same issue API Platform by Sylius and a second GraphQL API /api/v2/graphql (in addition to the Api platform REST API /api/v2).

In the Symfony profiler the security section was disabled for graphQL requests.

The graphql endpoint needs its own firewall :

In security.yaml

security:
    firewalls:
        graphql_shop_user:
            pattern: "%sylius.security.new_api_route%/graphql"
            provider: sylius_api_shop_user_provider
            stateless: true
            anonymous: true
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文