簡體   English   中英

如何在 Symfony 中配置基本身份驗證以允許空密碼?

[英]How can I configure basic authentication in Symfony to allow an empty password?

我正在用 PHP / Symfony 4.0 編寫一個 REST API 端點,我想使用基本身份驗證,以及一個密碼為空的內存用戶。 當我嘗試使用這些憑據調用端點時,我收到一個BadCredentialsException “提供的密碼不能為空”。

看來異常是由DaoAuthenticationProvider拋出的。

如何在 Symfony 中配置基本身份驗證以允許空密碼?

上下文:我正在為將用作集成字段的 Prismic 自定義 API 的簡單端點設置基本身份驗證(請參閱https://user-guides.prismic.io/en/articles/1401183-connect-to-自定義 API )。 Prismic 支持對此進行基本身份驗證,您可以在其中輸入將用作用戶名的密鑰,而密碼將留空。

我正在用 PHP / Symfony 4.0 寫這篇文章,我一直在關注https://symfony.com/doc/current/security.html 上的文檔。

我的 config/packages/security.yaml 文件是:

security:
    firewalls:
        secured_area:
            pattern: ^/api/my-endpoint
            http_basic: ~
            security: true
        main:
            security: false

    providers:
        in_memory:
            memory:
                users:
                    test:
                        password: ''

    encoders:
        Symfony\Component\Security\Core\User\User: plaintext

我的日志中的完整錯誤消息是:

用戶的基本身份驗證失敗。 {"username":"test","exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\BadCredentialsException(code: 0): Bad credentials. at my-project\\vendor\\symfony\\security\\Core \\Authentication\\Provider\\UserAuthenticationProvider.php:84, Symfony\\Component\\Security\\Core\\Exception\\BadCredentialsException(code: 0): The present password cannot be empty. at my-project\\vendor\\symfony\\security\\Core\\Authentication\\ Provider\\DaoAuthenticationProvider.php:54)"} []

您將需要創建自己的自定義身份驗證提供程序

不同部分的光澤度:

  1. 創建一個實現Symfony\\Component\\Security\\Core\\Authentication\\Provider\\AuthenticationProviderInterface
  2. 創建工廠服務以實例化此服務。 它需要實現Symfony\\Bundle\\SecurityBundle\\DependencyInjection\\Security\\Factory\\SecurityFactoryInterface
  3. 在您的Kernel::build()方法中,將工廠添加到您的安全上下文中。
  4. 添加適當的防火牆規則,以便使用此AuthenticationProvider。

如果您想完全覆蓋DaoAuthenticationProvider ,則可以創建自己的實現並使用以下配置覆蓋原始服務定義:

services:
    security.authentication.provider.dao: '@App\Your\Custom\AuthProvider'

您可以直接擴展DaoAuthenticationProvider ,但是注入的userProvider是私有的,因此您的類將無法訪問它,因此您需要解決此問題。

此外,您將在全球范圍內覆蓋此提供程序。 如果以后發現要依靠自定義行為,則可能會出現問題。

如果您真的不想進行適當的身份驗證,然后經過身份驗證的用戶將使用api密鑰對遠程端點進行調用,並以更“骯臟”的方式解決任務,那么您可以執行以下操作:

  • 創建您自己的AuthenticationProvider,它將擴展原始的https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php
  • 覆蓋checkAuthentication方法,在該方法中,您將擁有與原始代碼相同的代碼,但無需檢查空密碼(以及該else語句中其他所有相關內容
  • 覆蓋服務定義以指向您自己創建的AuthenticationProvider

     <service id="security.authentication.provider.dao" class="XXXX Your class here XXXX" abstract="true"> <argument /> <!-- User Provider --> <argument /> <!-- User Checker --> <argument /> <!-- Provider-shared Key --> <argument type="service" id="security.encoder_factory" /> <argument>%security.authentication.hide_user_not_found%</argument> </service> 
  • 清除緩存並嘗試

更新覆蓋服務: services: ... ... security.authentication.provider.dao: alias: MyProject\\Service\\Security\\OverriddenDaoAuthenticationProvider public: true

更新2

在您的OverriddenDaoAuthenticationProvider類中,記住要調用原始構造函數來傳遞參數:

public function __construct(UserProviderInterface $userProvider, UserCheckerInterface $userChecker, string $providerKey, EncoderFactoryInterface $encoderFactory, bool $hideUserNotFoundExceptions = true)
{
    parent::__construct($userProvider, $userChecker, $providerKey, $encoderFactory, $hideUserNotFoundExceptions);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM