Symfony 手动登录用户
Symfony Manually Log User In
存在创建用户实体的页面(这在正常注册流程之外)。
创建用户后,他们应该登录,guardHandler 与身份验证器一起使用,如下所示。
use App\Security\FakeAuthenticator;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
$response = $guardHandler->authenticateUserAndHandleSuccess(
$user, // the User object you just created
$request,
$authenticator, // authenticator whose onAuthenticationSuccess you want to use
'main' // the name of your firewall in security.yaml
);
然而验证器一团糟,它只为一种方法创建onAuthenticationSuccess
。
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
class FakeAuthenticator extends AbstractGuardAuthenticator
{
public function supports(Request $request)
{
return false;
}
public function getCredentials(Request $request)
{
throw new \RuntimeException('Unreachable code');
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
throw new \RuntimeException('Unreachable code');
}
public function checkCredentials($credentials, UserInterface $user)
{
throw new \RuntimeException('Unreachable code');
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
return null;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
throw new \RuntimeException('Unreachable code');
}
public function start(Request $request, AuthenticationException $authException = null)
{
throw new \RuntimeException('Unreachable code');
}
public function supportsRememberMe()
{
return true;
}
}
必须实现许多方法,因为方法 handleAuthenticationSuccess
需要 class 来实现 AuthenticatorInterface
.
代码有效,用户已登录,但感觉不是最干净的解决方案,是否有另一种登录用户的方法?
FosUserBundle 正在项目中使用,以下确实有效,但我不确定是否支持调用 loginManager 上的方法,我在文档中找不到任何内容,我不希望我的代码依赖关于可能会改变的功能。
\FOS\UserBundle\Security\LoginManagerInterface::logInUser('main', $user, $response);
我决定使用 loginManager
及其 public 方法 logInUser
,这是最干净的解决方案,无需为单个方法创建额外的 class。
use FOS\UserBundle\Security\LoginManager;
...
public function createUserInControllerAction(LoginManagerInterface $loginManager): Response
{
$user = new User(); // create user however you like
$loginManager->logInUser('main', $user, $response);
return $this->json(['success']);
}
存在创建用户实体的页面(这在正常注册流程之外)。
创建用户后,他们应该登录,guardHandler 与身份验证器一起使用,如下所示。
use App\Security\FakeAuthenticator;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
$response = $guardHandler->authenticateUserAndHandleSuccess(
$user, // the User object you just created
$request,
$authenticator, // authenticator whose onAuthenticationSuccess you want to use
'main' // the name of your firewall in security.yaml
);
然而验证器一团糟,它只为一种方法创建onAuthenticationSuccess
。
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
class FakeAuthenticator extends AbstractGuardAuthenticator
{
public function supports(Request $request)
{
return false;
}
public function getCredentials(Request $request)
{
throw new \RuntimeException('Unreachable code');
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
throw new \RuntimeException('Unreachable code');
}
public function checkCredentials($credentials, UserInterface $user)
{
throw new \RuntimeException('Unreachable code');
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
return null;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
throw new \RuntimeException('Unreachable code');
}
public function start(Request $request, AuthenticationException $authException = null)
{
throw new \RuntimeException('Unreachable code');
}
public function supportsRememberMe()
{
return true;
}
}
必须实现许多方法,因为方法 handleAuthenticationSuccess
需要 class 来实现 AuthenticatorInterface
.
代码有效,用户已登录,但感觉不是最干净的解决方案,是否有另一种登录用户的方法?
FosUserBundle 正在项目中使用,以下确实有效,但我不确定是否支持调用 loginManager 上的方法,我在文档中找不到任何内容,我不希望我的代码依赖关于可能会改变的功能。
\FOS\UserBundle\Security\LoginManagerInterface::logInUser('main', $user, $response);
我决定使用 loginManager
及其 public 方法 logInUser
,这是最干净的解决方案,无需为单个方法创建额外的 class。
use FOS\UserBundle\Security\LoginManager;
...
public function createUserInControllerAction(LoginManagerInterface $loginManager): Response
{
$user = new User(); // create user however you like
$loginManager->logInUser('main', $user, $response);
return $this->json(['success']);
}