LexikJWTAuthenticationBundle始终通过令牌存储获取空用户

时间:2018-05-14 21:26:26

标签: symfony jwt token lexikjwtauthbundle

我在后端(api)使用symfony。验证过程由FosUserBundle,LexikJWTAuthenticationBundle和LdapTools处理......一切正常。

问题在于我即将在控制器或服务中获得Authenticated用户。

  

用户通过Authorization标头进行身份验证,不存在401例外

$this->container->get('security.token_storage')->getToken()->getUser()//null

$preAuthToken = $this->container->get('security.token_storage')->getToken();
$tmp = $this->container->get('lexik_jwt_authentication.jwt_manager')->decode($preAuthToken);//i can get the username and roles

但真正的问题在于安全系统

if ($this->isGranted('ROLE_USER')) {
     echo 'never gets here!!';
     } else {
  echo 'always';
}

安全系统总是失败,因为getUser()返回的用户总是为空。

我的问题是:LexikJWTAuthenticationBundle在成功验证后不应注入或替换当前用户令牌?

还是应该以编程方式进行?我不想陷入不良行为......

提前感谢!

  

security.yml info

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt
        LdapTools\Bundle\LdapToolsBundle\Security\User\LdapUser: plaintext

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        ldap:
            id: ldap_tools.security.user.ldap_user_provider

        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        refresh:
            pattern: ^/api/token/refresh
            stateless: true
            anonymous: true

        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        api_login:
            pattern:  ^/login
            stateless: true
            provider: fos_userbundle
            anonymous: true
            form_login:
                check_path:               /login
                require_previous_session: false
                username_parameter:       username
                password_parameter:       password
                success_handler:          lexik_jwt_authentication.handler.authentication_success
                failure_handler:          AppBundle\Handler\AuthenticationFailureHandler
                require_previous_session: false
            guard:
                authenticators:
                    - ldap_tools.security.ldap_guard_authenticator
            logout: true

        api:
            pattern:   ^/
            stateless: true
            lexik_jwt: ~

    access_control:
        - { path: ^/login$,           role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/,                 role: IS_AUTHENTICATED_FULLY }
  

验证。失败处理程序(以防万一)

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        $token = $exception->getToken();

        if (is_string($exception->getToken()->getUser())) {
            $usuario = $this->container->get('fos_user.user_manager')->findUserByUsername($token->getUsername());
            if ($usuario) {
                $token = new UsernamePasswordToken($usuario, 'yes', 'public', $usuario->getRoles());
            } else {
                return $this->container->get('lexik_jwt_authentication.handler.authentication_failure')->onAuthenticationFailure($request, $exception);
            }

        }
        return $this->handleAuthenticationFail($request, $token, $exception);
    }

    public function handleAuthenticationFail(Request $request, TokenInterface $token, AuthenticationException $exception)
    {

        $username = $token->getUsername();

        $password = $request->get('password');
        if ($this->ldapManager->authenticate($username, $password)) {
            return $this->container->get('lexik_jwt_authentication.handler.authentication_success')->handleAuthenticationSuccess($token->getUser());
        }

        return $this->container->get('lexik_jwt_authentication.handler.authentication_failure')->onAuthenticationFailure($request, $exception);
    }

0 个答案:

没有答案