安全认证之用户认证
web服务器:Nginx
安装用户认证并分析原理(举一隅不以三隅反则不复也)
php artisan make:auth
和php artisan migrate
除了首页各个路由访问报错:laravel No input file specified
解决:
打开nginx的配置文件,在location中添加上
try_files $uri u r i / / i n d e x . p h p uri/ /index.php uri//index.phpis_args$query_string;
解决报错: Specified key was too long
AppServiceProvider.php
use Illuminate\Support\Facades\Schema;
public function boot() {
Schema::defaultStringLength(191);
}
php artisan make:auth 命令解析:
代码运行位置:vendor/laravel/framework/src/Illuminate/Auth/Console/AuthMakeCommand.php
handel方法:
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->createDirectories();
$this->exportViews();
if (! $this->option('views')) {
file_put_contents(
app_path('Http/Controllers/HomeController.php'),
$this->compileControllerStub()
);
file_put_contents(
base_path('routes/web.php'),
file_get_contents(__DIR__.'/stubs/make/routes.stub'),
FILE_APPEND
);
}
$this->info('Authentication scaffolding generated successfully.');
}
/**
* Compiles the HomeController stub.
*
* @return string
*/
protected function compileControllerStub()
{
return str_replace(
'{{namespace}}',
$this->getAppNamespace(),
file_get_contents(__DIR__.'/stubs/make/controllers/HomeController.stub')
);
}
这里用到了 file_get_contents
,file_put_contents
函数生成目录和文件和stub
文件。
stub文件:
存根, 占位代码,占坑代码,桩代码,粘合代码,残存代码, 指满足形式要求但没有实现具体功能的占坑/代理代码。
stub code 给出的实现是临时性的/待编辑的。它使得程序在结构上能够符合标准,又能够使程序员可以暂时不编辑这段代码。
举个例子,我写了一个类文件,里面只有类、方法、变量的声明部分,没有具体实现的部分,那么这个类文件就可以称之为 stub 文件。
web.php路由文件:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
关键代码:
Auth::routes();
位置:vendor/laravel/framework/src/Illuminate/Routing/Router.php
/**
* Register the typical authentication routes for an application.
*
* @param array $options
* @return void
*/
public function auth(array $options = [])
{
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
if ($options['register'] ?? true) {
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
}
// Password Reset Routes...
if ($options['reset'] ?? true) {
$this->resetPassword();
}
// Email Verification Routes...
if ($options['verify'] ?? false) {
$this->emailVerification();
}
}
路由都在这边进行注册,
顺便讲一个技巧,如果不知道方法在哪个实体类中,可以解析容器中的别名查看,dd( app(‘auth’))打印出来,常用的别名都在
config/app.php
下:
'aliases' => [
...
'Auth' => Illuminate\Support\Facades\Auth::class,
...
],
登录之后用户如何认证,默认是采用的web
看守器,session
文件存储方式,数据驱动是users
表
‘defaults’ => [
‘guard’ => ‘web’,
‘passwords’ => ‘users’,
],
---------------------
‘guards’ => [
‘web’ => [
‘driver’ => ‘session’,
‘provider’ => ‘users’,
],
‘api’ => [
‘driver’ => ‘token’,
‘provider’ => ‘users’,
‘hash’ => false,
],
],
laravel的session机制并未采用php默认的机制,而是自定义了一套读写逻辑, session_set_save_handler
函数了解一下,关于'laravel session生命周期做了什么?'
在另一篇laravel session 生命周期中有记录。
待续。。。。
如有不正确的地方请指正