Comments (4)
Hello,
I tried on a fresh project, and it seems to be working with default configuration.
What version of this package and Symfony are you using?
Can you share your session config in config/packages/framework.yaml
?
from roadrunner-bundle.
Good afternoon!
Im use:
- symfony/security-bundle 6.2.10
- baldinof/roadrunner-bundle 3.0.0
Config:
session:
name: 'rtsid'
handler_id: app.sessions.handler
cookie_secure: auto
cookie_samesite: lax
Services
app.sessions.cache:
class: \Symfony\Component\Cache\Adapter\RedisTagAwareAdapter
arguments:
$redis: '@app.sessions.redis'
app.sessions.handler:
class: App\FrontendWeb\Security\WebSession\WebSessionRedisHandler
arguments:
$webSessionCache: '@app.sessions.cache'
from roadrunner-bundle.
Is it possible to see the code in WebSessionRedisHandler
?
from roadrunner-bundle.
WebSessionRedisHandler
<?php
namespace App\FrontendWeb\Security\WebSession;
use Psr\Cache\CacheException;
use Psr\Cache\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler;
use Symfony\Component\Security\Http\RememberMe\RememberMeDetails;
/**
* Web Session Redis Handler
*/
final class WebSessionRedisHandler extends AbstractSessionHandler
{
/**
* @var RedisTagAwareAdapter Web Session Cache
*/
private RedisTagAwareAdapter $webSessionCache;
/**
* @var Security Security
*/
private Security $security;
/**
* @var RequestStack Request Stack
*/
private RequestStack $requestStack;
/**
* @var LoggerInterface Logger
*/
private LoggerInterface $logger;
/**
* @var string Префикс redis ключей
*/
private string $prefix;
/**
* @var int|null Время жизни в секундах
*/
private ?int $ttl = null;
/**
* List of available options:
* * prefix: The prefix to use for the keys in order to avoid collision on the Redis server
* * ttl: The time to live in seconds.
*
* @param RedisTagAwareAdapter $webSessionCache Web Session Cache
* @param Security $security Security
* @param RequestStack $requestStack Request Stack
* @param LoggerInterface $logger Logger
* @param array $options Options
*
* @throws \InvalidArgumentException When unsupported client or options are passed
*/
public function __construct(
RedisTagAwareAdapter $webSessionCache,
Security $security,
RequestStack $requestStack,
LoggerInterface $logger,
array $options = [],
)
{
if ($diff = \array_diff(\array_keys($options), ['prefix', 'ttl'])) {
throw new \InvalidArgumentException(\sprintf('Указанные параметры не поддерживаются "%s".', \implode(', ', $diff)));
}
$this->webSessionCache = $webSessionCache;
$this->security = $security;
$this->requestStack = $requestStack;
$this->logger = $logger;
if (isset($options['prefix']) && \is_string($options['prefix'])) {
$this->prefix = $options['prefix'];
} else {
$this->prefix = 'sf_s';
}
if (isset($options['ttl']) && \is_int($options['ttl'])) {
$this->ttl = $options['ttl'];
} else {
$this->ttl = (int) \ini_get('session.gc_maxlifetime');
}
}
/**
* @throws InvalidArgumentException
*/
protected function doRead(string $sessionId): string
{
$cacheItem = $this->webSessionCache->getItem($this->prefix.$sessionId);
if ($cacheItem->isHit()) {
return (string) $cacheItem->get();
}
return '';
}
/**
* @throws InvalidArgumentException|CacheException В случае ошибки
*/
protected function doWrite(string $sessionId, string $data): bool
{
return $this->saveData($sessionId, $data);
}
/**
* @throws InvalidArgumentException|CacheException В случае ошибки
*/
protected function doDestroy(string $sessionId): bool
{
return $this->webSessionCache->delete($this->prefix.$sessionId);
}
/**
* {@inheritdoc}
*/
#[\ReturnTypeWillChange]
public function close(): bool
{
return true;
}
public function gc(int $maxlifetime): int|false
{
return 0;
}
/**
* @throws InvalidArgumentException|CacheException В случае ошибки
*/
public function updateTimestamp(string $sessionId, string $data): bool
{
return $this->saveData($sessionId, $data);
}
/**
* @param string $sessionId Идентификатор сессии
* @param string $data Данные сессии
* @return bool Обновление данных сессии
* @throws InvalidArgumentException|CacheException В случае ошибки
*/
private function saveData(string $sessionId, string $data): bool
{
$cacheItem = $this->webSessionCache->getItem($this->prefix.$sessionId);
$cacheItem->set($data);
$cacheItem->expiresAfter($this->ttl);
// tagged session
$tags = [];
if ($user = $this->security->getUser()) {
$tags[] = 'user-'.$user->getUserIdentifier();
}
$token = $this->security->getToken();
if ($token && $token->hasAttribute('usid')) {
$tags[] = 'usid-'.(string) $token->getAttribute('usid');
}
$request = $this->requestStack->getMainRequest();
if ($request && $usidt = $this->getUserSessionTokenId($request)) {
$tags[] = 'usidt-'.\str_replace(['{','}','(',')','/','\\','@',':'], '_', $usidt);
}
if (!empty($tags)) {
$cacheItem->tag($tags);
}
$this->logger->debug(\sprintf("%s: %s, tags: %s", __METHOD__, $this->prefix.$sessionId, \implode(', ', $tags)));
return $this->webSessionCache->save($cacheItem);
}
/**
* @param Request $request Request
* @return string|null User Session Token Id
*/
protected function getUserSessionTokenId(Request $request): ?string
{
if (!$rawCookie = $request->cookies->get('rmt')) {
return null;
}
list($seriesId, ) = \explode(':', RememberMeDetails::fromRawCookie((string) $rawCookie)->getValue());
return $seriesId;
}
}
from roadrunner-bundle.
Related Issues (20)
- RPC Service in Console App HOT 1
- Symfony 6.2: Deprecation warnings HOT 2
- Entity manager bleeding entities over different requests HOT 5
- roadrunner-cli not in composer requirements
- Workers sharing logged users HOT 8
- Failed after run http.Plugin HOT 12
- Cookies not transferred HOT 2
- The RoadRunner plugin 'rpc' is disabled, try to add a `rpc` section to the configuration file. HOT 1
- Reset stateful services question
- GRPC PHP Fatal error: Uncaught Error: Interface "App\Grpc\Generated\Calculator\CalculatorInterface" not found CalculatorService.php
- Running multiple environments at once? HOT 3
- 3.0.1 patch version dropped support for symfony ^6.0 HOT 2
- `Undefined constant self::MASTER_REQUEST`
- Symfony integrated HttpCache reverse proxy cannot serve cached responses
- Event dispatcher doesn't reset automatically after next request HOT 1
- Is there any way to change http log output?
- SentryMiddleware incompatibility with sentry ^4.0
- phpfpm faster than roadrunner HOT 6
- Invalid alias definition: alias "Baldinof\RoadRunnerBundle\Http\RequestHandlerInterface" HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from roadrunner-bundle.