質問編集履歴
2
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,6 +30,20 @@
|
|
30
30
|
|
31
31
|
|
32
32
|
|
33
|
+
自身の見解は、controllerphpのlogin関数でlogメソッドで確認したところ
|
34
|
+
|
35
|
+
> Argument 1 passed to Cake\Controller\Controller::log() must be of the type string, object given, called in /var/www/html/sample/src/Controller/ContactsController.php on line 163
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
というエラーメッセージが吐かれていたのでlogin関数の$resultになにもデータが入っていないことが原因だと考えていますがどこが問題でデータがとれていないのでしょうか?
|
40
|
+
|
41
|
+
宜しくお願い致します。
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
33
47
|
```applicationphp
|
34
48
|
|
35
49
|
<?php
|
1
足りない情報の補足
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
cakephp4 ログイン認証機能
|
1
|
+
cakephp4 ログイン認証機能について
|
test
CHANGED
@@ -10,48 +10,558 @@
|
|
10
10
|
|
11
11
|
参考にしたサイト↓↓↓↓↓↓↓↓↓↓
|
12
12
|
|
13
|
+
[シンプルな認証と認可のアプリケーション](https://book.cakephp.org/4/ja/tutorials-and-examples/blog-auth-example/auth.html)
|
14
|
+
|
15
|
+
|
16
|
+
|
13
|
-
|
17
|
+
ログインページから名前とパスワードを入力しログインしようとすると、
|
14
|
-
|
18
|
+
|
19
|
+
|
20
|
+
|
15
|
-
|
21
|
+
> Table class for alias Users could not be found.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
22
|
+
|
20
|
-
|
21
|
-
一つ目は上記のサイトの通り下記のように記述するとリダイレクトループが起こりエラーになることです。
|
22
|
-
|
23
|
-
|
23
|
+
エイリアスUsersのテーブルクラスが見つかりませんでした。
|
24
|
-
|
24
|
+
|
25
|
+
|
26
|
+
|
25
|
-
|
27
|
+
というようなエラーメッセージが吐かれます。
|
28
|
+
|
26
|
-
|
29
|
+
このエラーメッセージで検索を行いましたが見当はずれのサイトばかりなのか自分の理解力がないのかで全く参考になるサイトがありませんでした。このエラーメッセージの原因と打開策をご教授お願い致します。
|
30
|
+
|
31
|
+
|
32
|
+
|
27
|
-
```
|
33
|
+
```applicationphp
|
34
|
+
|
28
|
-
|
35
|
+
<?php
|
36
|
+
|
37
|
+
declare(strict_types=1);
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
/**
|
42
|
+
|
43
|
+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
44
|
+
|
45
|
+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
46
|
+
|
47
|
+
*
|
48
|
+
|
49
|
+
* Licensed under The MIT License
|
50
|
+
|
51
|
+
* For full copyright and license information, please see the LICENSE.txt
|
52
|
+
|
53
|
+
* Redistributions of files must retain the above copyright notice.
|
54
|
+
|
55
|
+
*
|
56
|
+
|
57
|
+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
58
|
+
|
59
|
+
* @link https://cakephp.org CakePHP(tm) Project
|
60
|
+
|
61
|
+
* @since 3.3.0
|
62
|
+
|
63
|
+
* @license https://opensource.org/licenses/mit-license.php MIT License
|
64
|
+
|
65
|
+
*/
|
66
|
+
|
67
|
+
namespace App;
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
use Cake\Core\Configure;
|
72
|
+
|
73
|
+
use Cake\Core\ContainerInterface;
|
74
|
+
|
75
|
+
use Cake\Core\Exception\MissingPluginException;
|
76
|
+
|
77
|
+
use Cake\Datasource\FactoryLocator;
|
78
|
+
|
79
|
+
use Cake\Error\Middleware\ErrorHandlerMiddleware;
|
80
|
+
|
81
|
+
use Cake\Http\BaseApplication;
|
82
|
+
|
83
|
+
use Cake\Http\Middleware\BodyParserMiddleware;
|
84
|
+
|
85
|
+
use Cake\Http\Middleware\CsrfProtectionMiddleware;
|
86
|
+
|
87
|
+
use Cake\Http\MiddlewareQueue;
|
88
|
+
|
89
|
+
use Cake\ORM\Locator\TableLocator;
|
90
|
+
|
91
|
+
use Cake\Routing\Middleware\AssetMiddleware;
|
92
|
+
|
93
|
+
use Cake\Routing\Middleware\RoutingMiddleware;
|
94
|
+
|
29
|
-
|
95
|
+
use Authentication\AuthenticationService;
|
96
|
+
|
97
|
+
use Authentication\AuthenticationServiceInterface;
|
98
|
+
|
99
|
+
use Authentication\AuthenticationServiceProviderInterface;
|
100
|
+
|
101
|
+
use Authentication\Middleware\AuthenticationMiddleware;
|
102
|
+
|
103
|
+
use Cake\Routing\Router;
|
104
|
+
|
105
|
+
use Psr\Http\Message\ServerRequestInterface;
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
/**
|
112
|
+
|
113
|
+
* Application setup class.
|
114
|
+
|
115
|
+
*
|
116
|
+
|
117
|
+
* This defines the bootstrapping logic and middleware layers you
|
118
|
+
|
119
|
+
* want to use in your application.
|
120
|
+
|
121
|
+
*/
|
122
|
+
|
123
|
+
class Application extends BaseApplication implements AuthenticationServiceProviderInterface
|
30
124
|
|
31
125
|
{
|
32
126
|
|
127
|
+
/**
|
128
|
+
|
129
|
+
* Load all the application configuration and bootstrap logic.
|
130
|
+
|
131
|
+
*
|
132
|
+
|
133
|
+
* @return void
|
134
|
+
|
135
|
+
*/
|
136
|
+
|
137
|
+
public function bootstrap(): void
|
138
|
+
|
139
|
+
{
|
140
|
+
|
141
|
+
// Call parent to load bootstrap from files.
|
142
|
+
|
143
|
+
parent::bootstrap();
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
if (PHP_SAPI === 'cli') {
|
148
|
+
|
149
|
+
$this->bootstrapCli();
|
150
|
+
|
151
|
+
} else {
|
152
|
+
|
153
|
+
FactoryLocator::add(
|
154
|
+
|
155
|
+
'Table',
|
156
|
+
|
157
|
+
(new TableLocator())->allowFallbackClass(false)
|
158
|
+
|
33
|
-
|
159
|
+
);
|
160
|
+
|
34
|
-
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
/*
|
166
|
+
|
167
|
+
* Only try to load DebugKit in development mode
|
168
|
+
|
169
|
+
* Debug Kit should not be installed on a production system
|
170
|
+
|
171
|
+
*/
|
172
|
+
|
173
|
+
if (Configure::read('debug')) {
|
174
|
+
|
175
|
+
$this->addPlugin('DebugKit');
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
// Load more plugins here
|
182
|
+
|
35
|
-
$this->
|
183
|
+
$this->addPlugin('Authentication');
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
/**
|
190
|
+
|
191
|
+
* Setup the middleware queue your application will use.
|
192
|
+
|
193
|
+
*
|
194
|
+
|
195
|
+
* @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
|
196
|
+
|
197
|
+
* @return \Cake\Http\MiddlewareQueue The updated middleware queue.
|
198
|
+
|
199
|
+
*/
|
200
|
+
|
201
|
+
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
|
202
|
+
|
203
|
+
{
|
204
|
+
|
205
|
+
$middlewareQueue
|
206
|
+
|
207
|
+
// Catch any exceptions in the lower layers,
|
208
|
+
|
209
|
+
// and make an error page/response
|
210
|
+
|
211
|
+
->add(new ErrorHandlerMiddleware(Configure::read('Error')))
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
// Handle plugin/theme assets like CakePHP normally does.
|
216
|
+
|
217
|
+
->add(new AssetMiddleware([
|
218
|
+
|
219
|
+
'cacheTime' => Configure::read('Asset.cacheTime'),
|
220
|
+
|
221
|
+
]))
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
// Add routing middleware.
|
226
|
+
|
227
|
+
// If you have a large number of routes connected, turning on routes
|
228
|
+
|
229
|
+
// caching in production could improve performance. For that when
|
230
|
+
|
231
|
+
// creating the middleware instance specify the cache config name by
|
232
|
+
|
233
|
+
// using it's second constructor argument:
|
234
|
+
|
235
|
+
// `new RoutingMiddleware($this, '_cake_routes_')`
|
236
|
+
|
237
|
+
->add(new RoutingMiddleware($this))
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
->add(new AuthenticationMiddleware($this))
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
// Parse various types of encoded request bodies so that they are
|
248
|
+
|
249
|
+
// available as array through $request->getData()
|
250
|
+
|
251
|
+
// https://book.cakephp.org/4/en/controllers/middleware.html#body-parser-middleware
|
252
|
+
|
253
|
+
->add(new BodyParserMiddleware())
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
// Cross Site Request Forgery (CSRF) Protection Middleware
|
258
|
+
|
259
|
+
// https://book.cakephp.org/4/en/controllers/middleware.html#cross-site-request-forgery-csrf-middleware
|
260
|
+
|
261
|
+
->add(new CsrfProtectionMiddleware([
|
262
|
+
|
263
|
+
'httponly' => true
|
264
|
+
|
265
|
+
]));
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
return $middlewareQueue;
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
/**
|
280
|
+
|
281
|
+
* Register application container services.
|
282
|
+
|
283
|
+
*
|
284
|
+
|
285
|
+
* @param \Cake\Core\ContainerInterface $container The Container to update.
|
286
|
+
|
287
|
+
* @return void
|
288
|
+
|
289
|
+
* @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
|
290
|
+
|
291
|
+
*/
|
292
|
+
|
293
|
+
public function services(ContainerInterface $container): void
|
294
|
+
|
295
|
+
{
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
/**
|
302
|
+
|
303
|
+
* Bootstrapping for CLI application.
|
304
|
+
|
305
|
+
*
|
306
|
+
|
307
|
+
* That is when running commands.
|
308
|
+
|
309
|
+
*
|
310
|
+
|
311
|
+
* @return void
|
312
|
+
|
313
|
+
*/
|
314
|
+
|
315
|
+
protected function bootstrapCli(): void
|
316
|
+
|
317
|
+
{
|
318
|
+
|
319
|
+
try {
|
320
|
+
|
321
|
+
$this->addPlugin('Bake');
|
322
|
+
|
323
|
+
} catch (MissingPluginException $e) {
|
324
|
+
|
325
|
+
// Do not halt if the plugin is missing
|
326
|
+
|
327
|
+
}
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
$this->addPlugin('Migrations');
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
// Load more plugins here
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
|
342
|
+
|
343
|
+
{
|
344
|
+
|
345
|
+
$authenticationService = new AuthenticationService([
|
346
|
+
|
347
|
+
'unauthenticatedRedirect' => '/sample/contacts/login',
|
348
|
+
|
349
|
+
'queryParam' => 'redirect',
|
350
|
+
|
351
|
+
]);
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
// 識別子をロードして、電子メールとパスワードのフィールドを確認します
|
356
|
+
|
357
|
+
$authenticationService->loadIdentifier('Authentication.Password', [
|
358
|
+
|
359
|
+
'fields' => [
|
360
|
+
|
361
|
+
'username' => 'email',
|
362
|
+
|
363
|
+
'password' => 'password',
|
364
|
+
|
365
|
+
]
|
366
|
+
|
367
|
+
]);
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
// 認証子をロードするには、最初にセッションを実行する必要があります
|
372
|
+
|
373
|
+
$authenticationService->loadAuthenticator('Authentication.Session');
|
374
|
+
|
375
|
+
// メールとパスワードを選択するためのフォームデータチェックの設定
|
376
|
+
|
377
|
+
$authenticationService->loadAuthenticator('Authentication.Form', [
|
378
|
+
|
379
|
+
'fields' => [
|
380
|
+
|
381
|
+
'username' => 'email',
|
382
|
+
|
383
|
+
'password' => 'password',
|
384
|
+
|
385
|
+
],
|
386
|
+
|
387
|
+
'loginUrl' => '/sample/contacts/login',
|
388
|
+
|
389
|
+
]);
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
return $authenticationService;
|
36
394
|
|
37
395
|
}
|
38
396
|
|
397
|
+
}
|
398
|
+
|
399
|
+
|
400
|
+
|
39
401
|
```
|
40
402
|
|
41
403
|
|
42
404
|
|
405
|
+
```controllerphp
|
406
|
+
|
407
|
+
public function beforeFilter(\Cake\Event\EventInterface $event)
|
408
|
+
|
409
|
+
{
|
410
|
+
|
411
|
+
parent::beforeFilter($event);
|
412
|
+
|
413
|
+
$this->Authentication->allowUnauthenticated(['login', 'add']);
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
public function login()
|
420
|
+
|
421
|
+
{
|
422
|
+
|
43
|
-
|
423
|
+
$result = $this->Authentication->getResult();
|
424
|
+
|
44
|
-
|
425
|
+
debug($result, true);
|
426
|
+
|
45
|
-
|
427
|
+
// 認証成功
|
428
|
+
|
46
|
-
|
429
|
+
if ($result->isValid()) {
|
430
|
+
|
431
|
+
$target = $this->Authentication->getLoginRedirect() ?? '/home0';
|
432
|
+
|
433
|
+
return $this->redirect($target);
|
434
|
+
|
435
|
+
}
|
436
|
+
|
437
|
+
// ログインできなかった場合
|
438
|
+
|
439
|
+
if ($this->request->is('post') && !$result->isValid()) {
|
440
|
+
|
441
|
+
$this->Flash->error('Invalid username or password');
|
442
|
+
|
443
|
+
}
|
444
|
+
|
445
|
+
}
|
446
|
+
|
447
|
+
```
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
```appcontroller
|
452
|
+
|
453
|
+
<?php
|
454
|
+
|
455
|
+
declare(strict_types=1);
|
456
|
+
|
457
|
+
|
458
|
+
|
459
|
+
/**
|
460
|
+
|
461
|
+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
462
|
+
|
463
|
+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
464
|
+
|
465
|
+
*
|
466
|
+
|
467
|
+
* Licensed under The MIT License
|
468
|
+
|
47
|
-
|
469
|
+
* For full copyright and license information, please see the LICENSE.txt
|
470
|
+
|
48
|
-
|
471
|
+
* Redistributions of files must retain the above copyright notice.
|
472
|
+
|
473
|
+
*
|
474
|
+
|
475
|
+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
476
|
+
|
477
|
+
* @link https://cakephp.org CakePHP(tm) Project
|
478
|
+
|
479
|
+
* @since 0.2.9
|
480
|
+
|
481
|
+
* @license https://opensource.org/licenses/mit-license.php MIT License
|
482
|
+
|
483
|
+
*/
|
484
|
+
|
485
|
+
namespace App\Controller;
|
486
|
+
|
487
|
+
|
488
|
+
|
49
|
-
|
489
|
+
use Cake\Controller\Controller;
|
490
|
+
|
50
|
-
|
491
|
+
use Cake\ORM\TableRegistry;
|
492
|
+
|
493
|
+
|
494
|
+
|
51
|
-
|
495
|
+
/**
|
496
|
+
|
52
|
-
|
497
|
+
* Application Controller
|
498
|
+
|
499
|
+
*
|
500
|
+
|
501
|
+
* Add your application-wide methods in the class below, your controllers
|
502
|
+
|
503
|
+
* will inherit them.
|
504
|
+
|
505
|
+
*
|
506
|
+
|
507
|
+
* @link https://book.cakephp.org/4/en/controllers.html#the-app-controller
|
508
|
+
|
509
|
+
*/
|
510
|
+
|
511
|
+
class AppController extends Controller
|
512
|
+
|
513
|
+
{
|
514
|
+
|
515
|
+
/**
|
516
|
+
|
517
|
+
* Initialization hook method.
|
518
|
+
|
519
|
+
*
|
520
|
+
|
521
|
+
* Use this method to add common initialization code like loading components.
|
522
|
+
|
523
|
+
*
|
524
|
+
|
525
|
+
* e.g. `$this->loadComponent('FormProtection');`
|
526
|
+
|
527
|
+
*
|
528
|
+
|
529
|
+
* @return void
|
530
|
+
|
531
|
+
*/
|
532
|
+
|
533
|
+
public function initialize(): void
|
534
|
+
|
535
|
+
{
|
536
|
+
|
53
|
-
|
537
|
+
parent::initialize();
|
538
|
+
|
539
|
+
|
540
|
+
|
54
|
-
|
541
|
+
$this->Contacts = TableRegistry::get('contacts');
|
542
|
+
|
543
|
+
|
544
|
+
|
545
|
+
$this->loadComponent('RequestHandler');
|
546
|
+
|
547
|
+
$this->loadComponent('Flash');
|
548
|
+
|
55
|
-
|
549
|
+
$this->loadComponent('Authentication.Authentication');
|
56
|
-
|
550
|
+
|
57
|
-
|
551
|
+
/*
|
552
|
+
|
553
|
+
* Enable the following component for recommended CakePHP form protection settings.
|
554
|
+
|
555
|
+
* see https://book.cakephp.org/4/en/controllers/components/form-protection.html
|
556
|
+
|
557
|
+
*/
|
558
|
+
|
559
|
+
//$this->loadComponent('FormProtection');
|
560
|
+
|
561
|
+
}
|
562
|
+
|
563
|
+
}
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
```
|