質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

Q&A

0回答

828閲覧

CakePHP3.8のAuthorizationプラグインの設定方法について

tomona

総合スコア37

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

0グッド

0クリップ

投稿2019/08/19 04:59

CakePHP3.8を用いて名簿管理システムを作成しています。

ユーザー認証機能を付けたいと考えており、Authorizationプラグインの導入を検討しています。
テストプロジェクトにインストールしてドキュメントQiitaの記事を参考に進めていたのですが、リンク先のコードをすべて書き込んだところで以下のエラーが発生しました。

The request to / did not apply any authorization checks.

私のイメージではここまでの記述でログインページに飛ばされるものと思っていましたがそうならないようです。
恐れ入りますが、改善方法をご教示いただけますと幸いです。

src/Application.php

PHP

1<?php 2/** 3 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) 4 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 5 * 6 * Licensed under The MIT License 7 * For full copyright and license information, please see the LICENSE.txt 8 * Redistributions of files must retain the above copyright notice. 9 * 10 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 11 * @link https://cakephp.org CakePHP(tm) Project 12 * @since 3.3.0 13 * @license https://opensource.org/licenses/mit-license.php MIT License 14 */ 15namespace App; 16 17use Cake\Core\Configure; 18use Cake\Core\Exception\MissingPluginException; 19use Cake\Error\Middleware\ErrorHandlerMiddleware; 20use Cake\Http\BaseApplication; 21use Cake\Routing\Middleware\AssetMiddleware; 22use Cake\Routing\Middleware\RoutingMiddleware; 23//以下はAuthプラグインの追加クラス 24use Authorization\AuthorizationService; 25use Authorization\AuthorizationServiceProviderInterface; 26use Authorization\Middleware\AuthorizationMiddleware; 27use Authorization\Policy\OrmResolver; 28use Psr\Http\Message\ResponseInterface; 29use Psr\Http\Message\ServerRequestInterface; 30 31use Cake\Routing\Router; 32use Authentication\Middleware\AuthenticationMiddleware; 33use Authentication\AuthenticationService; 34use Authentication\AuthenticationServiceProviderInterface; 35 36/** 37 * Application setup class. 38 * 39 * This defines the bootstrapping logic and middleware layers you 40 * want to use in your application. 41 */ 42class Application extends BaseApplication implements AuthorizationServiceProviderInterface 43{ 44 public function getAuthorizationService(ServerRequestInterface $request, ResponseInterface $response) 45 { 46 $resolver = new OrmResolver(); 47 48 return new AuthorizationService($resolver); 49 50 51 } 52 /** 53 * {@inheritDoc} 54 */ 55 public function bootstrap() 56 { 57 // Call parent to load bootstrap from files. 58 parent::bootstrap(); 59 $this->addPlugin('Authorization'); 60 61 if (PHP_SAPI === 'cli') { 62 $this->bootstrapCli(); 63 } 64 65 /* 66 * Only try to load DebugKit in development mode 67 * Debug Kit should not be installed on a production system 68 */ 69 if (Configure::read('debug')) { 70 $this->addPlugin(\DebugKit\Plugin::class); 71 } 72 73 // Load more plugins here 74 } 75 76 /** 77 * Setup the middleware queue your application will use. 78 * 79 * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup. 80 * @return \Cake\Http\MiddlewareQueue The updated middleware queue. 81 */ 82 public function middleware($middlewareQueue) 83 { 84 $middlewareQueue 85 // Catch any exceptions in the lower layers, 86 // and make an error page/response 87 ->add(new ErrorHandlerMiddleware(null, Configure::read('Error'))) 88 89 // Handle plugin/theme assets like CakePHP normally does. 90 ->add(new AssetMiddleware([ 91 'cacheTime' => Configure::read('Asset.cacheTime') 92 ])) 93 94 // Add routing middleware. 95 // If you have a large number of routes connected, turning on routes 96 // caching in production could improve performance. For that when 97 // creating the middleware instance specify the cache config name by 98 // using it's second constructor argument: 99 // `new RoutingMiddleware($this, '_cake_routes_')` 100 ->add(new RoutingMiddleware($this)); 101 102 $middlewareQueue->add(new AuthorizationMiddleware($this)); 103 104 return $middlewareQueue; 105 } 106 107 /** 108 * @return void 109 */ 110 protected function bootstrapCli() 111 { 112 try { 113 $this->addPlugin('Bake'); 114 } catch (MissingPluginException $e) { 115 // Do not halt if the plugin is missing 116 } 117 118 $this->addPlugin('Migrations'); 119 120 // Load more plugins here 121 } 122} 123

src/Controller/AppController.php

PHP

1<?php 2/** 3 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) 4 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 5 * 6 * Licensed under The MIT License 7 * For full copyright and license information, please see the LICENSE.txt 8 * Redistributions of files must retain the above copyright notice. 9 * 10 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 11 * @link https://cakephp.org CakePHP(tm) Project 12 * @since 0.2.9 13 * @license https://opensource.org/licenses/mit-license.php MIT License 14 */ 15namespace App\Controller; 16 17use Cake\Controller\Controller; 18use Cake\Event\Event; 19 20/** 21 * Application Controller 22 * 23 * Add your application-wide methods in the class below, your controllers 24 * will inherit them. 25 * 26 * @link https://book.cakephp.org/3.0/en/controllers.html#the-app-controller 27 */ 28class AppController extends Controller 29{ 30 31 /** 32 * Initialization hook method. 33 * 34 * Use this method to add common initialization code like loading components. 35 * 36 * e.g. `$this->loadComponent('Security');` 37 * 38 * @return void 39 */ 40 public function initialize() 41 { 42 parent::initialize(); 43 44 $this->loadComponent('RequestHandler', [ 45 'enableBeforeRedirect' => false, 46 ]); 47 $this->loadComponent('Flash'); 48 49 /* 50 * Enable the following component for recommended CakePHP security settings. 51 * see https://book.cakephp.org/3.0/en/controllers/components/security.html 52 */ 53 //$this->loadComponent('Security'); 54 $this->loadComponent('Authorization.Authorization', [ 55 'logoutRedirect' => '/users/login', // Default is false 56 'requireIdentity' => true]); 57 } 58} 59

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問