teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

cookieにcsrftokenあるか確認。追加コード

2020/11/27 13:43

投稿

YuhiUsui
YuhiUsui

スコア11

title CHANGED
File without changes
body CHANGED
@@ -22,7 +22,22 @@
22
22
  ### 該当のソースコード
23
23
  src/application.php
24
24
  ```ここに言語を入力
25
+ <?php
26
+ declare(strict_types=1);
25
27
 
28
+ /**
29
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
30
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
31
+ *
32
+ * Licensed under The MIT License
33
+ * For full copyright and license information, please see the LICENSE.txt
34
+ * Redistributions of files must retain the above copyright notice.
35
+ *
36
+ * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
37
+ * @link https://cakephp.org CakePHP(tm) Project
38
+ * @since 3.3.0
39
+ * @license https://opensource.org/licenses/mit-license.php MIT License
40
+ */
26
41
  namespace App;
27
42
 
28
43
  use Cake\Core\Configure;
@@ -34,7 +49,150 @@
34
49
  use Cake\Http\MiddlewareQueue;
35
50
  use Cake\Routing\Middleware\AssetMiddleware;
36
51
  use Cake\Routing\Middleware\RoutingMiddleware;
52
+ // use Cake\Network\Exception\InvalidCsrfTokenException;
37
53
 
54
+ // use Authentication\AuthenticationService;
55
+ // use Authentication\AuthenticationServiceInterface;
56
+ // use Authentication\AuthenticationServiceProviderInterface;
57
+ // use Authentication\Middleware\AuthenticationMiddleware;
58
+ // use Psr\Http\Message\ServerRequestInterface;
59
+
60
+ /**
61
+ * Application setup class.
62
+ *
63
+ * This defines the bootstrapping logic and middleware layers you
64
+ * want to use in your application.
65
+ */
66
+ class Application extends BaseApplication
67
+ // implements AuthenticationServiceProviderInterface
68
+ {
69
+ /**
70
+ * Load all the application configuration and bootstrap logic.
71
+ *
72
+ * @return void
73
+ */
74
+ public function bootstrap(): void
75
+ {
76
+ $this->addPlugin('Migrations');
77
+
78
+ // Call parent to load bootstrap from files.
79
+ parent::bootstrap();
80
+
81
+ if (PHP_SAPI === 'cli') {
82
+ $this->bootstrapCli();
83
+ }
84
+
85
+ /*
86
+ * Only try to load DebugKit in development mode
87
+ * Debug Kit should not be installed on a production system
88
+ */
89
+ if (Configure::read('debug')) {
90
+ $this->addPlugin('DebugKit');
91
+ }
92
+
93
+ // Load more plugins here
94
+ }
95
+
96
+ /**
97
+ * Setup the middleware queue your application will use.
98
+ *
99
+ * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
100
+ * @return \Cake\Http\MiddlewareQueue The updated middleware queue.
101
+ */
102
+ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
103
+ {
104
+ $middlewareQueue
105
+ // Catch any exceptions in the lower layers,
106
+ // and make an error page/response
107
+ ->add(new ErrorHandlerMiddleware(Configure::read('Error')))
108
+
109
+ // Handle plugin/theme assets like CakePHP normally does.
110
+ ->add(new AssetMiddleware([
111
+ 'cacheTime' => Configure::read('Asset.cacheTime'),
112
+ ]))
113
+
114
+ // Add routing middleware.
115
+ // If you have a large number of routes connected, turning on routes
116
+ // caching in production could improve performance. For that when
117
+ // creating the middleware instance specify the cache config name by
118
+ // using it's second constructor argument:
119
+ // `new RoutingMiddleware($this, '_cake_routes_')`
120
+ ->add(new RoutingMiddleware($this))
121
+
122
+ // Parse various types of encoded request bodies so that they are
123
+ // available as array through $request->getData()
124
+ // https://book.cakephp.org/4/en/controllers/middleware.html#body-parser-middleware
125
+ ->add(new BodyParserMiddleware())
126
+
127
+ // Cross Site Request Forgery (CSRF) Protection Middleware
128
+ // https://book.cakephp.org/4/en/controllers/middleware.html#cross-site-request-forgery-csrf-middleware
129
+ ->add(new CsrfProtectionMiddleware([
130
+ 'httponly' => true,
131
+ ]));
132
+
133
+ return $middlewareQueue;
134
+ }
135
+
136
+ /**
137
+ * Bootstrapping for CLI application.
138
+ *
139
+ * That is when running commands.
140
+ *
141
+ * @return void
142
+ */
143
+ protected function bootstrapCli(): void
144
+ {
145
+ try {
146
+ $this->addPlugin('Bake');
147
+ } catch (MissingPluginException $e) {
148
+ // Do not halt if the plugin is missing
149
+ }
150
+
151
+ $this->addPlugin('Migrations');
152
+
153
+ // Load more plugins here
154
+ }
155
+
156
+ // public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
157
+ // {
158
+ // $middlewareQueue
159
+ // // ... 前に追加された他のミドルウェア
160
+ // ->add(new RoutingMiddleware($this))
161
+ // // RoutingMiddleware の後に認証を追加
162
+ // ->add(new AuthenticationMiddleware($this));
163
+
164
+ // return $middlewareQueue;
165
+ // }
166
+
167
+ // public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
168
+ // {
169
+ // $authenticationService = new AuthenticationService([
170
+ // 'unauthenticatedRedirect' => '/users/login',
171
+ // 'queryParam' => 'redirect',
172
+ // ]);
173
+
174
+ // // identifiers を読み込み、email と password のフィールドを確認します
175
+ // $authenticationService->loadIdentifier('Authentication.Password', [
176
+ // 'fields' => [
177
+ // 'username' => 'email',
178
+ // 'password' => 'password',
179
+ // ]
180
+ // ]);
181
+
182
+ // // authenticatorsをロードしたら, 最初にセッションが必要です
183
+ // $authenticationService->loadAuthenticator('Authentication.Session');
184
+ // // 入力した email と password をチェックする為のフォームデータを設定します
185
+ // $authenticationService->loadAuthenticator('Authentication.Form', [
186
+ // 'fields' => [
187
+ // 'username' => 'email',
188
+ // 'password' => 'password',
189
+ // ],
190
+ // 'loginUrl' => '/users/login',
191
+ // ]);
192
+
193
+ // return $authenticationService;
194
+ // }
195
+ }
38
196
  ```
39
197
  記事作成
40
198
  ``` php
@@ -141,6 +299,7 @@
141
299
  https://teratail.com/questions/165237
142
300
 
143
301
  ![イメージ説明](4b785e8d09270aefc7efaf7bb9d72a79.png)
302
+ ![![CSRF cookie](ac8d817446730583082c50fdb99971e6.png)](b36e4c632f1d76be4dc36127af49f19b.png)
144
303
 
145
304
  ### 補足情報(FW/ツールのバージョンなど)
146
305
  cake4, php7.4, xamppVM