简体   繁体   English

带有 SelfValidatingPassport 的 Symfony 6 ApiKeyAuthenticator 取代了警卫?

[英]Symfony 6 ApiKeyAuthenticator with SelfValidatingPassport replaces guard?

I migrating a symfony 5.1 api to symfony 6 with api-platform.我使用 api-platform 将 symfony 5.1 api 迁移到 symfony 6。
My app has it's own user and password logic different to common user database so I had to create my UserRepository and UserProvider.我的应用程序有自己的用户和密码逻辑,与普通用户数据库不同,所以我必须创建我的 UserRepository 和 UserProvider。
I have created a controller with Login functionality that checks credentials and returns a token.我创建了一个具有登录功能的控制器,用于检查凭据并返回一个令牌。
On symfony 5 , I had implemented an AbstractGuardAuthenticator to verify the token and load the user.symfony 5上,我实现了一个 AbstractGuardAuthenticator 来验证令牌并加载用户。
On symfony 6 I use the new system implementing an AbstractAuthenticator (Personal opinion: less clear than guard).symfony 6上,我使用实现 AbstractAuthenticator 的新系统(个人意见:不如警卫清楚)。

security:
    enable_authenticator_manager: true
# [...]
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        api_user_provider:
            id: App\Security\UserProvider

    firewalls:
# [...]
        api:
            pattern: ^/api/
            stateless: true
            provider: api_user_provider
            custom_authenticators:
                - App\Security\TokenAuthenticator
<?php

namespace App\Security;

// usings

class TokenAuthenticator extends AbstractAuthenticator
{
// [...]
    public function supports(Request $request): ?bool
    {
        if ( $request->headers->has('Authorization') ) {
            return true;
        } else {
            throw new AuthenticationException('Authorization header is missing');
        }
    }

    public function authenticate(Request $request): Passport
    {
        $token = $this->getToken($request);
       
        return new SelfValidatingPassport(
            new UserBadge($token, function ($token): UserInterface {
                return $this->getUserFromToken($token);
            }), []
        );


    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
    {
        return New JsonResponse(["result"=> "ok"]);
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
    {
        return new JsonResponse(["result" => "error"], Response::HTTP_UNAUTHORIZED);
    }


}

When I make a simple call to an Endpoint that requires the user to be logged, eg:当我对需要登录用户的端点进行简单调用时,例如:

GET http://localhost:8000/api/categories
Accept: application/json
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQi[...]

I expect a list of categories, but I receive a json from onAuthenticationSuccess :我期望一个类别列表,但我从onAuthenticationSuccess收到一个 json:

{"result":"ok"}

So I think I'm missunderstanding the security system.所以我认为我误解了安全系统。 Please help me.请帮我。
What I'm doing wrong?我做错了什么?

It's simple, you almost had it.这很简单,你几乎拥有它。 onAuthenticationSuccess must return null to let your request continue. onAuthenticationSuccess必须返回 null 才能让您的请求继续。
Your are interrupting your original request when returning a json: {"result": "ok"} .返回 json: {"result": "ok"}时,您正在中断您的原始请求。

public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
     return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM