CakePHP 3.5 编写和加密 Cookie
CakePHP 3.5 Writing & Encrypting Cookie
背景:我刚刚升级到 CakePHP 3.5.17。
我有一个写cookie的代码。但是,似乎我缺少一些加密步骤。有人可以阐明缺少的步骤在哪里吗?目前,Web 浏览器正在获取 cookie 的值,但它未加密。请注意,我还在 app.php
上设置了 cookieKey
我还在下面提供的 link 中包含了这些步骤
https://book.cakephp.org/3.0/en/development/application.html#adding-http-stack
//In src/Controller/UsersController.php
use Cake\I18n\Time;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;
use Cake\Core\Configure;
use App\Application;
use Cake\Error\Middleware\ErrorHandlerMiddleware;
use Cake\Http\MiddlewareQueue;
use Cake\Routing\Middleware\AssetMiddleware;
use Cake\Routing\Middleware\RoutingMiddleware;
use Cake\Http\Middleware\EncryptedCookieMiddleware;
public function writecookie() {
$cookie = new Cookie(
'goodday', // name
'YES', // value
(Time::now())->modify('+1 year'), // expiration time, if applicable
'/', // path, if applicable
'', // domain, if applicable
false, // secure only?
true // http only ?
);
$middlewareQueue = new MiddlewareQueue();
$cookiesEncrypted = new EncryptedCookieMiddleware(
['goodday'],
Configure::read('Security.cookieKey')
);
$cookiesEncrypted = $middlewareQueue->add($cookiesEncrypted);
$this->response = $this->response->withCookie($cookie); //value is still YES in the web browser cookie storage
}
进一步调试后,我注意到在class EncryptedCookieMiddleware 中。它声明请求数据中的 Cookie 将被解密,而响应 headers 中的 cookie 将被自动加密。如果响应是 Cake\Http\Response,则使用 withCookie()
和 `cookie()`` 设置的 cookie 数据也将被加密。但是对我来说它不会自动加密?
您可能想让自己更熟悉中间件的工作方式,您不应该在控制器中使用它们,它们应该是 "wrapped around" 您的应用程序并与请求交互发送到应用程序,以及应用程序发回的响应。
您在应用程序 Application::middleware()
方法、Server.buildMiddleware
事件或连接路由时注册它们。
// src/Application.php
// ...
use Cake\Http\Middleware\EncryptedCookieMiddleware;
class Application extends BaseApplication
{
public function middleware($middlewareQueue)
{
// ...
$middlewareQueue->add(new EncryptedCookieMiddleware(/* ... */));
return $middlewareQueue;
}
}
另见
背景:我刚刚升级到 CakePHP 3.5.17。
我有一个写cookie的代码。但是,似乎我缺少一些加密步骤。有人可以阐明缺少的步骤在哪里吗?目前,Web 浏览器正在获取 cookie 的值,但它未加密。请注意,我还在 app.php
上设置了 cookieKey我还在下面提供的 link 中包含了这些步骤
https://book.cakephp.org/3.0/en/development/application.html#adding-http-stack
//In src/Controller/UsersController.php
use Cake\I18n\Time;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;
use Cake\Core\Configure;
use App\Application;
use Cake\Error\Middleware\ErrorHandlerMiddleware;
use Cake\Http\MiddlewareQueue;
use Cake\Routing\Middleware\AssetMiddleware;
use Cake\Routing\Middleware\RoutingMiddleware;
use Cake\Http\Middleware\EncryptedCookieMiddleware;
public function writecookie() {
$cookie = new Cookie(
'goodday', // name
'YES', // value
(Time::now())->modify('+1 year'), // expiration time, if applicable
'/', // path, if applicable
'', // domain, if applicable
false, // secure only?
true // http only ?
);
$middlewareQueue = new MiddlewareQueue();
$cookiesEncrypted = new EncryptedCookieMiddleware(
['goodday'],
Configure::read('Security.cookieKey')
);
$cookiesEncrypted = $middlewareQueue->add($cookiesEncrypted);
$this->response = $this->response->withCookie($cookie); //value is still YES in the web browser cookie storage
}
进一步调试后,我注意到在class EncryptedCookieMiddleware 中。它声明请求数据中的 Cookie 将被解密,而响应 headers 中的 cookie 将被自动加密。如果响应是 Cake\Http\Response,则使用 withCookie()
和 `cookie()`` 设置的 cookie 数据也将被加密。但是对我来说它不会自动加密?
您可能想让自己更熟悉中间件的工作方式,您不应该在控制器中使用它们,它们应该是 "wrapped around" 您的应用程序并与请求交互发送到应用程序,以及应用程序发回的响应。
您在应用程序 Application::middleware()
方法、Server.buildMiddleware
事件或连接路由时注册它们。
// src/Application.php
// ...
use Cake\Http\Middleware\EncryptedCookieMiddleware;
class Application extends BaseApplication
{
public function middleware($middlewareQueue)
{
// ...
$middlewareQueue->add(new EncryptedCookieMiddleware(/* ... */));
return $middlewareQueue;
}
}
另见