【GAE】PHP7ランタイムでBasic認証を設定する
環境
- GAE PHP72 runtime
- Laravel 5.8.7
インストール
基本的にREADMEの内容を設定するだけでBasic認証が有効になります。
composer require olssonm/l5-very-basic-auth:5.*
設定
設定ファイルを作成する
$ php artisan vendor:publish --provider="Olssonm\VeryBasicAuth\VeryBasicAuthServiceProvider"
Copied File [/vendor/olssonm/l5-very-basic-auth/src/config.php] To [/config/very_basic_auth.php]
Publishing complete.
ユーザとパスワードを設定する
config/very_basic_auth.php
user, passwordのキーがそれぞれユーザとパスワードに紐づいているので 設定したい値に変更します。 今回は全環境でBasic認証を適用していますが 特定の環境のみ認証必須にしたければenvsキーを変更することで対応が可能です。
<?php
/**
* Configuration for the "HTTP Very Basic Auth"-middleware
*/
return [
// Username
'user' => 'admin',
// Password
'password' => 'aGnXaX9k',
// Environments where the middleware is active. Use "*" to protect all envs
'envs' => [
'*'
],
// Message to display if the user "opts out"/clicks "cancel"
'error_message' => 'You have to supply your credentials to access this resource.',
// Message to display in the auth dialiog in some browsers (mainly Internet Explorer).
// Realm is also used to define a "space" that should share credentials.
'realm' => 'Basic Auth',
// If you prefer to use a view with your error message you can uncomment "error_view".
// This will superseed your default response message
// 'error_view' => 'very_basic_auth::default'
];
環境を指定する場合
'envs' => [
'production',
'development',
'local'
],
routes/web.php
にBasic認証の適用範囲を設定する
// 複数のルーティングを認証かける場合
Route::group(['middleware' => 'auth.very_basic'], function() {
Route::get('/', ['as' => 'start', 'uses' => 'StartController@index']);
Route::get('/page', ['as' => 'page', 'uses' => 'StartController@page']);
});
// 特定のルーティングのみかける場合
Route::get('/', [
'as' => 'start',
'uses' => 'StartController@index',
'middleware' => 'auth.very_basic'
]);
これでDBの設定等もなしでBasic認証が有効になります デプロイして確認してみましょう
gcloud app deploy