我相信您想要做的事情在文档中有它自己的页面。看看How to create a custom user provider,它很好地介绍了如何使用 Web 服务对用户进行身份验证。
更新:
是的,我分享的链接告诉您如何创建用户提供程序。这正是你必须做的。在本页的第一段中,它说:
当用户提交用户名和密码时,身份验证层要求配置的用户提供程序返回给定用户名的用户对象。
因此您需要创建一个扩展UserInterface 的类,并且该类应该具有从您的Web 服务返回的所有属性(同样默认情况下,UserInterface 需要一些属性,例如用户名、密码、 salt 和 roles。但是如果你不使用 salt 和 roles 可以是空白的)。
然后您创建一个实现UserProviderInterface 的用户提供程序。这将使函数loadUserByUsername 在您执行登录时被调用。在此函数中,您执行 Web 服务请求,发送凭据。然后可以将您的请求响应映射到您创建的用户类。然后返回您使用从您的网络服务返回的数据创建的用户对象。
为了让 Symfony 知道你想使用你的自定义用户提供者来登录你的表单,你需要为你的用户提供者创建一个服务。因此,在src/Acme/WebserviceUserBundle/Resources/config/services.yml(您可能需要创建文件)中,您可以添加文档中概述的服务:
parameters:
# path to the user provider you just created
webservice_user_provider.class: Acme\WebserviceUserBundle\Security\User\WebserviceUserProvider
services:
webservice_user_provider:
class: "%webservice_user_provider.class%"
创建此服务后,您只需告诉表单登录以使用此自定义提供程序。在 security.yml 中:
security:
providers:
webservice:
# this id is the name of the service you created
id: webservice_user_provider
另外,如果您的密码在您的网络服务中被加密,您应该确保 Symfony 以相同的方式对其进行加密以进行比较。所以在security.yml中你可以添加
security:
encoders:
Acme\WebserviceUserBundle\Security\User\WebserviceUser: sha512 # or md5, or others