Php 如何在Symfony中配置基本身份验证以允许空密码?
我正在用PHP/Symfony 4.0编写一个REST API端点,我想使用基本身份验证,使用一个密码为空的内存用户。当我尝试使用这些凭据调用端点时,我得到一个Php 如何在Symfony中配置基本身份验证以允许空密码?,php,symfony,symfony4,basic-authentication,symfony-security,Php,Symfony,Symfony4,Basic Authentication,Symfony Security,我正在用PHP/Symfony 4.0编写一个REST API端点,我想使用基本身份验证,使用一个密码为空的内存用户。当我尝试使用这些凭据调用端点时,我得到一个BadCredentialsException说“提供的密码不能为空” 异常似乎是由DaoAuthenticationProvider引发的 如何在Symfony中配置基本身份验证以允许空密码 上下文:我正在为一个简单端点设置基本身份验证,该端点将用作集成字段的Prismic自定义API(请参阅)。Prismic为此支持基本身份验证,您可
BadCredentialsException
说“提供的密码不能为空”
异常似乎是由DaoAuthenticationProvider
引发的
如何在Symfony中配置基本身份验证以允许空密码
上下文:我正在为一个简单端点设置基本身份验证,该端点将用作集成字段的Prismic自定义API(请参阅)。Prismic为此支持基本身份验证,您可以输入一个密钥作为用户名,密码为空
我正在用PHP/Symfony 4.0编写这篇文章,我一直在关注网站上的文档
我的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(代码:0):错误的凭据。在我的项目中\vendor\Symfony\Security\Core\Authentication\Provider\UserAuthenticationProvider.php:84,Symfony\Component\Security\Core\exception\BadCredentialsException(代码:0):提供的密码不能为空。在my project\vendor\symfony\security\Core\Authentication\Provider\DaoAuthenticationProvider.php:54)“}[]
如果您确实不想进行适当的身份验证,然后经过身份验证的用户将使用api密钥调用远程端点,并以更“脏”的方式解决该任务,那么您可以做的是:
- 创建您自己的AuthenticationProvider,它将扩展原始
- 覆盖
方法,在该方法中,您将拥有与原始密码相同的代码,但不检查空密码(可能还有该else语句中与之相关的所有其他内容)checkAuthentication
- 覆盖以指向您自己创建的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>
只是想澄清一下:您使用哪个Symfony版本?您是否尝试过将其更新到4.3的最新版本?Symfony 4.0。我试着升级到Symfony 4.3,我也遇到了同样的异常。谢谢,这看起来很有希望。我已经创建了自己的AuthenticationProvider。如何覆盖服务定义?我有一个
文件,用于处理此项目中类的依赖项注入。我可以用它吗?@dans在Symfony 4服务中大部分是自动连线的,因此下一个链接将明确显示我们在自动连线之前的生活方式。在Symfony 4中,您仍然可以使用链接中描述的方法来显式定义(重新定义)服务。重要的是,使用服务别名并将其设置为可以在上面的xml定义中看到的服务Id,因为在Symfony4中,我们可以定义带别名和不带别名的服务。当您使用别名='security.authentication.provider.dao'定义服务时,它将覆盖原来的onethanks非常感谢您在这方面的帮助。我正试图按照您的建议使用别名重新定义服务,但我不确定是否有正确的语法。在我的services.yaml
文件中,我现在有:services.yaml
但是我似乎还没有说服Symfony使用我的overridedDAAuthenticationProvider,因为我仍然收到原始DaoAuthenticationProvider抛出的相同错误。你能看到我的语法中是否有错误吗?我觉得这一定越来越近了。我尝试了您建议的语法,现在我得到了一个SymfonyMyProject\Service\Security\overridedDAAuthenticationProvider:alias:Security.authentication.provider.dao public:true
,上面写着“Service”security.authentication.provider.dao.securited\u area”:如果尚未配置参数,则无法替换。“我是否需要更改OutOfBoundsException
文件中的其他内容?”?security.yaml
public function __construct(UserProviderInterface $userProvider, UserCheckerInterface $userChecker, string $providerKey, EncoderFactoryInterface $encoderFactory, bool $hideUserNotFoundExceptions = true) { parent::__construct($userProvider, $userChecker, $providerKey, $encoderFactory, $hideUserNotFoundExceptions); }