从Symfony 5+中的控制器启动Authenticator进程



我正在尝试为Symfony 5.4+编写一个SSO身份验证捆绑包

当我尝试访问security.yaml中security属性设置为true的页面时,没问题,我的authenticator类会自动启动并运行良好。我可以对自己进行身份验证,然后在Symfony中获得一个已填充的用户实例。

但现在,我想做的是一个简单的登录页面,它将播放身份验证过程,然后将用户重定向到应用程序管理员在配置文件中定义的页面(用于最终在具有公共访问权限的应用程序中添加身份验证按钮(。

我的问题是:由于我的登录路由具有公共访问权限(然后身份验证系统从未启动(,我如何从控制器启动此身份验证过程?

类似的东西:

class AuthenticationController extends AbstractController
{
public function login(Request $request, Security $security)
{
if (!$this->getUser())
$security->pleaseLaunchTheAuthenticationProcess()

return new RedirectResponse('/my_path');
}
}

您可以使用UserAuthenticatorInterface实现这一点:


/**
* @Route("/some-route", name="some_route")
*
* @param Request $request
* @param UserAuthenticatorInterface $userAuthenticator
* @param AuthenticatorInterface $authenticator
* @return Response
*/
public function book(
Request                     $request,
UserAuthenticatorInterface  $userAuthenticator,
AuthenticatorInterface      $authenticator
): Response
{
if ($this->getUser() instanceof User) {
$userAuthenticator->authenticateUser($this->getUser(), $authenticator, $request);
}
return $this->redirectToRoute('/my_path');
}

您将遇到无法启动的AuthenticatorInterface错误。你必须在你的服务中手动声明

services:
# default configuration for services in *this* file
_defaults:
autowire: true      # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$authenticator: '@security.authenticator.form_login.main'

最新更新