Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与LDAP提供程序一起使用LexikJWTAuthenticationBundle_Ldap_Lexikjwtauthbundle_Symfony 4.4 - Fatal编程技术网

与LDAP提供程序一起使用LexikJWTAuthenticationBundle

与LDAP提供程序一起使用LexikJWTAuthenticationBundle,ldap,lexikjwtauthbundle,symfony-4.4,Ldap,Lexikjwtauthbundle,Symfony 4.4,我有一个使用symfonyapi平台的项目。我想在我的项目上使用LexikJWTAuthenticationBundle,但我的用户存储在Active Directory中,因此我设置了LDAP用户提供程序。我尝试将以下两个文档的设置结合起来,但没有成功: 这就是我所做的 在security.yml中: security: providers: my_ldap: ldap: service: Symf

我有一个使用symfonyapi平台的项目。我想在我的项目上使用LexikJWTAuthenticationBundle,但我的用户存储在Active Directory中,因此我设置了LDAP用户提供程序。我尝试将以下两个文档的设置结合起来,但没有成功:

这就是我所做的

在security.yml中:

security:
    providers:
        my_ldap:
            ldap:
                service:     Symfony\Component\Ldap\Ldap
                base_dn:     OU=organization_unit,DC=domain,DC=local
                search_dn:   "OU=organization_unit,DC=domain,DC=local"
                search_password: '%env(resolve:LDAP_PASSWORD)%'
                default_roles: ROLE_USER
                uid_key: uid
firewalls:
    login:
        pattern:  ^/api/login
        stateless: true
        anonymous: true
        json_login:
            provider: my_ldap
            check_path:               /api/login_check
            success_handler:          lexik_jwt_authentication.handler.authentication_success
            failure_handler:          lexik_jwt_authentication.handler.authentication_failure

    api: 
        pattern:   ^/api/
        stateless: true
        guard:
            provider: my_ldap
            authenticators:
                - lexik_jwt_authentication.jwt_token_authenticator
在services.yml中:

Symfony\Component\Ldap\Ldap:
    arguments: ['@Symfony\Component\Ldap\Adapter\ExtLdap\Adapter']
Symfony\Component\Ldap\Adapter\ExtLdap\Adapter:
    arguments:
        -   host: '%env(resolve:LDAP_HOST)%'
            port: 636
            # encryption: tls
            options:
                protocol_version: 3
                referrals: false
但当我尝试将请求发送到我的\u域/api/login\u时,请检查以下内容:

{
  "username": "my_user",
  "password": "my_password"
}
我收到此错误作为响应:

{"code":401,"message":"Invalid credentials."}

我已经通过使用自定义操作解决了我的问题,该操作检查用户是否使用LDAP存在并获取其角色。然后,如果新检索到的用户被授权使用该应用程序,我将创建一个新令牌并在响应中返回它。最后,该令牌用于为用户对API的每次调用进行身份验证

这是我的保安

providers:
    # provider to retrieve user from LDAP
    my_ldap:
        id: App\Security\LdapService
    # provider to retrieve user from user
    jwt:
        lexik_jwt:
            class: App\Security\User
firewalls:
    login:
        pattern:  ^/api/login
        stateless: true
        anonymous: true
api: 
        pattern:   ^/api/
        stateless: true
        guard:
            provider: jwt
            authenticators:
                - lexik_jwt_authentication.jwt_token_authenticator
控制器中的登录操作:

/**
 * @Route("/api/login", name="api_login", methods={"POST"})
 */
public function loginCheck(Request $request, LdapService $ldapService, AuthenticationSuccessHandler $authenticationSuccessHandler, AuthenticationFailureHandler $authenticationFailureHandler){
    $content = json_decode($request->getContent());
    $response= new JsonResponse();
    try{
        $user=$ldapService->authenticate($content->username, $content->password);
        return $authenticationSuccessHandler->handleAuthenticationSuccess($user);
    }catch(AuthenticationException $e){
        return $authenticationFailureHandler->onAuthenticationFailure($request, $e);
    }        
    return $response;    
}

你能分享你的LdapService课程吗?