From 4bb13bb920feb27266fb99634599f4de8fe4647f Mon Sep 17 00:00:00 2001 From: zzq <565364226@qq.com> Date: Wed, 27 Nov 2019 13:55:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Seesion=20Id=20=E4=BB=8EHeader=E4=B8=AD?= =?UTF-8?q?=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Session/HttpSession.php | 7 ++++++- src/Session/SessionConfig.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Session/HttpSession.php b/src/Session/HttpSession.php index d65258b..f5e1247 100644 --- a/src/Session/HttpSession.php +++ b/src/Session/HttpSession.php @@ -60,9 +60,14 @@ public function __construct() setContextValue("HttpSession", $this); $this->request = getDeepContextValueByClassName(Request::class); $this->response = getDeepContextValueByClassName(Response::class); + if($this->config->getSessionUsage() == SessionConfig::USAGE_COOKIE) { $this->id = $this->request->getCookieParams()[$this->config->getSessionName()] ?? null; - }else{ + } elseif ($this->config->getSessionUsage() == SessionConfig::USEAGE_HEADER) { + /** @var array $_sesionIdentify */ + $_sesionIdentify = $this->request->getHeader(SessionConfig::HEADER_IDENTIFY); + $this->id = !empty($_sesionIdentify[0]) ? $_sesionIdentify[0] : null; + } else{ $authorization = explode(' ',$this->request->getHeaderLine('authorization')); if(isset($authorization[1])){ $this->id = $authorization[1]; diff --git a/src/Session/SessionConfig.php b/src/Session/SessionConfig.php index 354388d..d12161e 100644 --- a/src/Session/SessionConfig.php +++ b/src/Session/SessionConfig.php @@ -13,8 +13,29 @@ class SessionConfig extends BaseConfig { + /** + * Session key + */ const key = "session"; + + /** + * Usage cookie + */ const USAGE_COOKIE = 'cookie'; + + /** + * Usage head + */ + const USEAGE_HEADER = 'header'; + + /** + * Header identify to identify session + */ + const HEADER_IDENTIFY = 'sessionId'; + + /** + * Usage token + */ const USAGE_TOKEN = 'token'; /** @@ -36,6 +57,13 @@ class SessionConfig extends BaseConfig protected $sessionUsage = SessionConfig::USAGE_COOKIE; + /** + * Header identity. When $sessionUsage is set to header, should set $headerIdentity + * + * @var string + */ + protected $headerIdentity = SessionConfig::HEADER_IDENTIFY; + /** * @var string */ From 37e23068fb349759e2ac533b7afb3a0c9388fdc9 Mon Sep 17 00:00:00 2001 From: zzq <565364226@qq.com> Date: Wed, 27 Nov 2019 14:18:20 +0800 Subject: [PATCH 2/2] bug fix --- src/Session/HttpSession.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Session/HttpSession.php b/src/Session/HttpSession.php index f5e1247..3313f1a 100644 --- a/src/Session/HttpSession.php +++ b/src/Session/HttpSession.php @@ -60,7 +60,6 @@ public function __construct() setContextValue("HttpSession", $this); $this->request = getDeepContextValueByClassName(Request::class); $this->response = getDeepContextValueByClassName(Response::class); - if($this->config->getSessionUsage() == SessionConfig::USAGE_COOKIE) { $this->id = $this->request->getCookieParams()[$this->config->getSessionName()] ?? null; } elseif ($this->config->getSessionUsage() == SessionConfig::USEAGE_HEADER) { @@ -151,6 +150,7 @@ public function removeAttribute(string $key): void { unset($this->attribute[$key]); } + public function refresh(): void { $id = $this->getId(); @@ -162,7 +162,14 @@ public function refresh(): void $this->response->withCookie(new Cookie($this->config->getSessionName(), $this->id, time() + $this->config->getTimeout(), $this->config->getPath(), $this->config->getDomain(), $this->config->getSecure(), $this->config->getHttpOnly())); - }else{ + } elseif ($this->config->getSessionUsage() == SessionConfig::USEAGE_HEADER) { + /** @var array $_sesionIdentify */ + $_sesionIdentify = $this->request->getHeader(SessionConfig::HEADER_IDENTIFY); + if (!empty($_sesionIdentify[0])) { + $sesionIdentify = $_sesionIdentify[0]; + $this->response->withHeader(SessionConfig::HEADER_IDENTIFY, $sesionIdentify); + } + } else{ $this->response->withHeader('Authorization', 'Bearer ' .$this->id); } $this->setAttribute("createTime", time());