我正在将 Symfony 从 2.8 升级到 3.4,并且我有一个身份验证侦听器。
监听器的构造器
public function __construct(EntityManager $entityManager, SessionInterface $session, Security $security, LoggerInterface $logger, Redis $redis, $secret)
{
$this->entityManager = $entityManager;
$this->session = $session;
$this->security = $security;
$this->logger = $logger;
$this->redis = $redis;
$this->secret = $secret;
}
在侦听器中调用的请求函数
public function onRequest(GetResponseEvent $event)
{
//Validate token
//Get Authorization Header
$headers = $event->getRequest()->headers;
$authHeader = $headers->get('Authorization');
//Check if Header value starts with 'Bearer'
if($this->startsWith($authHeader, self::$BEARER_HEADER)) {
// Allow request to be processed by controllers
//token handler
} else {
$securityContext = $this->security;
if ($securityContext->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {
return;
} else {
throw new SessionTimeoutException();
}
}
}
服务.yml
app.token_listener:
class: Insead\MIMBundle\Listener\AuthTokenListener
arguments: ["@doctrine.orm.entity_manager", "@session", "@security.helper", "@logger", "@redis.authtoken", "%secret%"]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onRequest, priority: 0 }
ACL 列表条目 - security.php
'access_control' => array(
array('path' => '^/api/(.*?)/login', 'role'=>'IS_AUTHENTICATED_ANONYMOUSLY'),
)
宝慕林4294392
慕尼黑8549860