简体   繁体   English

具有多个提供程序的 LexikJWTAuthenticationBundle

[英]LexikJWTAuthenticationBundle with multiple providers

I 2 part of applications - first for admins (admin panel) and second API.我的应用程序的 2 部分 - 首先是管理员(管理面板)和第二个 API。 For API I want to use another model to check credentials and that retrieve a token.对于 API,我想使用另一个模型来检查凭据并检索令牌。 I thought that it could be achieved by specified check_path route where I can verify the provided data and then return manually token.我认为它可以通过指定的check_path路由来实现,我可以在其中验证提供的数据,然后手动返回令牌。

But It seems that the application doesn't event go to this endpoint because I haven`t seen any debug message from the response - only 401 error code.但似乎应用程序不会事件转到此端点,因为我没有看到来自响应的任何调试消息 - 只有 401 错误代码。 Here is my security.yml config:这是我的 security.yml 配置:

security:
    encoders:
        App\Entity\Security\AdminUser:
            algorithm: bcrypt
        Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUser:
            algorithm: bcrypt

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
    fos_userbundle:
        id: fos_user.user_provider.username_email
    jwt:
        lexik_jwt: ~

firewalls:
    api:
        provider: jwt
        pattern:  ^/api/
        stateless: true
        anonymous: true
        guard:
            authenticators:
                - 'jwt.token.authenticator'
        json_login:
            check_path: api.v1.0.token.get
            username_path: passwordName
            success_handler: lexik_jwt_authentication.handler.authentication_success
            failure_handler: lexik_jwt_authentication.handler.authentication_failure
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        context: 'main'
        pattern: ^/
        form_login:
            provider: fos_userbundle
            default_target_path: easyadmin
            csrf_token_generator: security.csrf.token_manager

        logout:       true
        anonymous:    true


access_control:
    - { path: ^/api/doc, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api/v1.0/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }

And here is my action where I tried to debug:这是我尝试调试的操作:

class TokenController extends AbstractController
{
/**
 * @Route("/login", name="api.v1.0.token.get", methods={"POST"})
 * @param Request $request
 */
public function obtainToken(Request $request, JWTEncoderInterface $encoder, SiteRepository $siteRepository)
  {
      dd(123); // I don`t see this message - only 401 error

  }
}

First, I'm not sure what you're trying to do with your obtainToken function but if you need to either create a token programatically or manipulate / customize its content before returning it, I highly suggest that you have a look at their documentation first as you'll have all the tools to achieve what you want to do:首先,我不确定你想用你的获取令牌函数做什么,但如果你需要在返回之前以编程方式创建令牌或操作/自定义其内容,我强烈建议你先查看他们的文档因为您将拥有实现您想做的事情的所有工具:

Otherwise, the bundle will handle that for you.否则,捆绑包将为您处理。

Now, assuming that you simply want to protect your api with JWT, you'll need to separate your api firewall into two different ones:现在,假设您只是想用 JWT 保护您的 api,您需要将您的 api 防火墙分成两个不同的防火墙:

First one to login, like so:第一个登录,如下所示:

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

And don't forget to update the access control to make sure your users can access it anonymously:并且不要忘记更新访问控制以确保您的用户可以匿名访问它:

- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

This will allow you to hit /api/login_check with the credentials to authenticate and obtain your token.这将允许您使用凭据点击 /api/login_check 以进行身份​​验证并获取您的令牌。


Then, protect the rest of your public api by defining the JWT guard authenticator, like so:然后,通过定义 JWT 保护身份验证器来保护您的公共 api 的其余部分,如下所示:

api:
  pattern: ^/api
  stateless: true
  # /!\ shouldn't be anonymous: true here
  provider: jwt
  guard:
    authenticators:
    - lexik_jwt_authentication.jwt_token_authenticator

And the access control too:还有访问控制:

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

If you need to create an account, you can define your own route and create the token programmatically upon success.如果您需要创建一个帐户,您可以定义自己的路由并在成功后以编程方式创建令牌。


Some other things to mention:还有一些要提的:

  • check_path: api.v1.0.token.get : I actually never tried but I don't think you can define a path by its route name like so, you better specify the path directly. check_path: api.v1.0.token.get :我实际上从未尝试过,但我认为您不能像这样通过路径名称定义路径,最好直接指定路径。
  • username_path: passwordName : here you're telling the bundle to use 'passwordName' as the username, which sounds weird. username_path: passwordName :在这里你告诉 bundle 使用 'passwordName' 作为用户名,这听起来很奇怪。

If you want to specify custom identifiers for both username and password, you better use something like this:如果要为用户名和密码指定自定义标识符,最好使用以下内容:

username_path: email # (or whatever field you use for the authentication)
password_path: password

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

相关问题 在流明中创建多个Auth Provider - Creating Multiple Auth Providers in Lumen Symfony 5: Guard with multiple User Providers - Symfony 5: Guard with multiple User Providers Symfony LexikJWTAuthenticationBundle无法验证 - Symfony LexikJWTAuthenticationBundle Unable to authenticate Symfony 4 - 使用 LexikJWTAuthenticationBundle 未找到 JWT - Symfony 4 - JWT not found with LexikJWTAuthenticationBundle Symfony 2多个提供程序不在secuirty.yml中工作 - Symfony 2 Multiple providers not working in secuirty.yml Laravel:使用`configureMonologUsing()`的多个日志提供程序? - Laravel: multiple log providers using `configureMonologUsing()`? 多个Symfony 2防火墙中的多个提供商:如何避免路由冲突? - Multiple providers in multiple Symfony 2 firewall: how to avoid route conflicts? 是否有多个付款提供商(贝宝,ogone等)的php模块可用于网络应用程序? - is there a multiple payment providers (paypal, ogone, …) php module for use in a web app? 多个支付提供商的设计模式,并根据实现通知不同的用户 - Design Pattern for Multiple Payment Providers and notifying different users based on the implementation FileLoaderLoadException-无法使LexikJWTAuthenticationBundle工作-Symfony - FileLoaderLoadException - can not get LexikJWTAuthenticationBundle to work - Symfony
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM