我正在将一个现有的PHP项目重写为Symfony 5.3。我想把它的认证系统升级到Symfony的。唯一的问题是:原则在这个项目中不是一个选项。
我如何使用Symfony的身份验证(可能与新的基于身份验证器的安全性一起)而不调用任何地方的原则?
我知道我必须实现一个UserLoaderInterface,但是文档中使用Doctrine太多了,我不知道没有Doctrine怎么做。
我刚才提到的帖子是问类似的问题,但它仍然使用Symfony 2,因此太过时了。
我有一个数据库,有必要的用户表与通常的列(ID,电子邮件,密码,名称等)。
言归正传:我如何使用Symfony的身份验证(可能与新的基于身份验证器的安全性一起)没有学说?
可以在官方网站上以及SymfonyCast的本教程中进行配置,但基本上您可以根据需要对用户进行身份验证:
见下一个例子:
在srcAppSecurity文件夹上创建一个文件,如果你的配置使用默认配置并创建类TokenAuthenticator,现在看到下面的代码,在这种情况下检查类AppServiceExternalAuthenticator,谁将负责从其他服务或api获取信息和返回。
<?php
namespace AppSecurity;
use AppExampleStudent;
use AppServiceExternalAuthenticator;
use AppDTOINFORMATIONFROMOTHERSERVICE;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentRoutingGeneratorUrlGeneratorInterface;
use SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface;
use SymfonyComponentSecurityCoreExceptionAuthenticationCredentialsNotFoundException;
use SymfonyComponentSecurityCoreExceptionAuthenticationException;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentSecurityCoreUserUserProviderInterface;
use SymfonyComponentSecurityGuardAbstractGuardAuthenticator;
use SymfonyComponentSecurityCoreSecurity;
final class TokenAuthenticator extends AbstractGuardAuthenticator
{
/** @var Security */
private $security;
/** @var ExternalAuthenticator */
private $externalAuthenticator;
/** @var UrlGeneratorInterface */
private $urlGenerator;
public function __construct(
Security $security,
ExternalAuthenticator $externalAuthenticator
) {
$this->security = $security;
$this->externalAuthenticator = $externalAuthenticator;
}
/**
* {@inheritDoc}
*/
public function supports(Request $request)
{
//on this example, this guard must be using if on the request contains the word token
$response = false;
$apiKey = $request->query->get('token');
if (!is_null($apiKey)) {
$response = true;
}
return $response;
}
/**
* {@inheritDoc}
*/
public function getCredentials(Request $request)
{
$apiKey = $request->query->get('token');
// Validate with anything you want, other service or api
/** @var INFORMATIONFROMOTHERSERVICE**/
$dtoToken = $this->externalAuthenticator->validateToken($apiKey, $simulator);
return $dtoToken;
}
/**
* @param INFORMATIONFROMOTHERSERVICE $credentials
* @param UserProviderInterface $userProvider
* @return INFORMATIONFROMOTHERSERVICE |UserInterface|null
*/
public function getUser($credentials, UserProviderInterface $userProvider)
{
return $userProvider;
}
public function checkCredentials($credentials, UserInterface $user)
{
return true;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
return new RedirectResponse($this->urlGenerator->generate('home_incorrect'));
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey)
{
return new RedirectResponse($request->getPathInfo());
}
public function start(Request $request, AuthenticationException $authException = null)
{
return new RedirectResponse($this->urlGenerator->generate('home_incorrect'));
}
public function supportsRememberMe()
{
// todo
}
}
现在外部服务必须返回AppDTOINFORMATIONFROMOTHERSERVICE类,但是这个类必须实现UserInterface,现在记住这一点。我们需要配置哪个警卫必须负责哪些路由,参见下一个示例:
security:
encoders:
AppEntityUser:
algorithm: bcrypt
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: AppEntityUser
property: email
//You can use a
custom_provider:
id : AppDTOINFORMATIONFROMOTHERSERVICE
# used to reload user from session & other features (e.g. switch_user)
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
survey:
anonymous: true
pattern: ^/(custom_path)/
// The
provider: custom_provider
guard:
// You can use as many authenticator that you want, but in the node entrypoint, you must choose who must be the default if only is one you could remove the entrypoint node, similar as the main firewall
authenticators:
- AppSecurityTokenAuthenticator
- AppSecurityOtherAuthenticator
entry_point: AppSecurityOtherAuthenticator
main:
anonymous: true
lazy: true
provider: app_user_provider
logout:
path: app_logout
guard:
authenticators:
- AppSecurityAppAuthenticator
请参阅下一个文档,它将指导您创建类AppDTOINFORMATIONFROMOTHERSERVICE。
我希望这个答案,对你有所帮助