Symfony 6 - LDAP登录与实体更新

q0qdq0h2  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(91)

我尝试通过表单针对LDAP构建登录,通过Doctrine从我的实体获取用户,并希望能够在成功登录时更新用户,或者在数据库中不存在用户时创建一个。此外,如果不存在用户但LDAP登录成功,我希望创建一个用户,或者如果LDAP不可访问,则根据数据库进行身份验证。
我一遍又一遍地阅读文档,但我不知道如何将这些需求合并组合起来。
我设法运行LDAP身份验证,但在我的控制器中,我只得到一个Symfony\Component\Ldap\Security\LdapUser的示例,没有UserEntity,而且我无法挂钩到登录过程中,因此我可以自己处理登录。我尝试了自定义用户提供程序,但我找不到像“afterfulLogin”这样的方法来更新我的实体。此外,在第二种情况下这对我登录数据库没有帮助
什么是正确的“Symfonyway”这样做?
先谢了。
编辑:
我想这是我必须使用的customuserprovider,但我不知道如何用它来实现我的目标。我目前没有在主防火墙中使用它,因为我不知道如何编辑customuserprovider。这是正确的方法吗?我更改了dn凭据,我没有尝试使用示例ldap。

security:
  # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
  password_hashers:
    Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
  # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
  providers:
    main_user_provider:
      entity:
        class: App\Entity\User
        property: email
    authority_user_provider:
      entity:
        class: App\Entity\Authority
        property: email
    company_ldap:
      ldap:
        service: Symfony\Component\Ldap\Ldap
        base_dn: dc=example,dc=com
        search_dn: "CN=admin,CN=Users,DC=example,DC=com"
        search_password:########
        default_roles: ROLE_USER
    custom:
      id: App\Security\CustomUserProvider
  firewalls:
    dev:
      pattern: ^/(_(profiler|wdt)|css|images|js)/
      security: false
    authorityPortal:
      pattern: ^/authority/
      lazy: true
      provider: authority_user_provider
      form_login:
        login_path: app_authority_login
        check_path: app_authority_login
        default_target_path: app_authority_portal
        enable_csrf: true
      logout:
        path: app_logout
      custom_authenticator: App\Security\AuthorityAuthenticator
    main:
      lazy: true
      provider: company_ldap
      form_login_ldap:
        service: Symfony\Component\Ldap\Ldap
        dn_string: "CN=Users,DC=example,DC=com"
        query_string: '(sAMAccountName={user_identifier})'
        search_dn: "CN=admin,CN=Users,DC=example,DC=com"
        search_password: "#######"
        login_path: app_login
        check_path: app_login
        default_target_path: app_workbench_index
        #enable_csrf: true
      logout:
        path: app_logout
mwg9r5ms

mwg9r5ms1#

我得到了它运行感谢这篇文章与一个类似的问题,由勒罗伊在评论中提到:
Symfony LDAP with custom User Entity and auto creation of DB user

相关问题