LexikJWTAuthenticationBundle在控制器中解码令牌

时间:2018-06-11 17:23:31

标签: symfony

如果有办法将令牌解码为用户ID,我试图在文档中找到它。这个捆绑包是否提供此类选项/服务? 感谢

1 个答案:

答案 0 :(得分:0)

所以有效的解决方案是创建新的防火墙规则

    auto_login:
        pattern: ^/auto_login
        anonymous: true
        stateless: true
        lexik_jwt:
            query_parameter:
                enabled: true
                name:    bearer

现在我可以将令牌作为获取查询传递

/auto_login/redirect_to_some_url?bearer=eyJhbGciOiJSUzI1...

并在路由器的控制器" / auto_login"如果令牌很好,我会为用户创建会话,并且我自动登录,看起来很好。

/**
 * @Route("/auto_login", name="auto_login")
 * @Route("/auto_login/{redirect}")
 */
public function autoLoginAction(Request $request, $redirect = null)
{
    $current_user_id = $this->getUser()->getId();
    if ($redirect && $current_user_id) {

        $user = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->find($current_user_id);

        $sf_token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
        $this->get('security.token_storage')->setToken($sf_token);
        $this->get('session')->set('_security_main', serialize($sf_token));

        $event = new InteractiveLoginEvent($request, $sf_token);
        $this->get('event_dispatcher')->dispatch('security.interactive_login', $event);

        return $this->redirectToRoute($redirect);
    } else {
        return $this->redirectToRoute('login');
    }
}