laravel 用户身份验证
来源:互联网 发布:数据库接口文档模板 编辑:程序博客网 时间:2024/06/10 21:04
一、相关文件:
/config/auth.php // 验证配置,包含:验证方式、用户模型类、用户表名/App/User.php // 用户 Eloquent 模型,对应用户表名 users,当 auth.php 配置认证驱动为 Eloquent 时,配合一起使用。 // 使用官方认证,密码应最少 60 位,并需要包含一个叫做 remember_token 的字段用于存储记住登录状态的 session token。// 记住登录状态:$table->rememberToken();/App/Http/Controllers/Auth/AuthController // 认证控制器一,处理新用户的注册和验证/App/Http/Controllers/Auth/PasswordController // 认证控制器二,处理用户找回密码
二、使用方法:
1、注册路由:laravel默认没有指定登录、注册的路由地址,故需要手动指定:
Route::get('auth/login', 'Auth\AuthController@getLogin'); //登录表单Route::post('auth/login', 'Auth\AuthController@postLogin'); //登录处理Route::get('auth/logout', 'Auth\AuthController@getLogout'); //退出登录Route::get('auth/register', 'Auth\AuthController@getRegister'); //注册表单Route::post('auth/register', 'Auth\AuthController@postRegister //注册处理
2、提供视图:在 resources/views/auth 中,创建 login.blade.php 和 register.blade.php
字段名:邮箱(登录账号):email 密码:password 确认密码:password_confirmation 用户名为:name 记录登录:remember
3、认证:
(1) 自动认证:默认使用 AuthController,提供了新用户验证规则 validator 和 添加用户 create两个方法,可以自行修改已适应需求。
(2) 手动认证:
public function authenticate(){ if (Auth::attempt(['email' => $email, 'password' => $password]), $isRememberLogin) { // 认证通过... return redirect()->intended('dashboard'); }}
4、跳转:
认证成功后默认跳转到 /home 路由,可以在 routers.php 中定义该路由实现定制,也可以在控制器中修改:
protected $redirectPath = '/dashboard'; // 自定义登录成功跳转位置,默认是:/homeprotected $loginPath = '/login'; // 自定义登录失败后跳转位置,默认是:/auth/login
5、获取认证信息:
Auth::check(); //是否认证通过Auth::login($user); //手动登录Auth::loginUsingId(); //使用ID登录Auth::once($userInfoArray); //临时登录,完成即退Auth:logout(); // 退出登录Auth::viaRemember(); // 是否记住登录$userInfo = Auth::user(); // 得到认证用户实例$userInfo = $request->user(); // 登录成功后通过 Request 门面获取默认在登录失败N次后将有一分钟不能登录,它基于用户名|邮箱+IP。
三、路由保护(访问权限):使用 auth 中间件来控制可以访问某些路由的用户
1、使用方法:
Route::get('/path', [ 'middleware' => 'auth', 'uses' => 'SomeController@method ']);或者:直接在控制器中使用 middleware
public function __construct(){ $this->middleware('auth');}
四、重置密码:默认使用邮箱认证,添加以下路由:
// 重置密码邮件...Route::get('password/email', 'Auth\PasswordController@getEmail');Route::post('password/email', 'Auth\PasswordController@postEmail');// 密码重置...Route::get('password/reset/{token}', 'Auth\PasswordController@getReset');Route::post('password/reset', 'Auth\PasswordController@postReset');
0 0
- laravel 用户身份验证
- 用户身份验证
- 用户身份验证
- laravel身份验证-Auth的使用
- laravel身份验证-Auth的使用
- 基本用户身份验证
- Spring 用户身份验证
- 用户管理和身份验证
- HttpModule实现用户身份验证
- 页面用户身份验证
- 安全-用户身份验证
- Apache用户身份验证
- Yii 用户身份验证
- Laravel 4.2 解决多张表身份验证的问题
- laravel 5 用户管理
- Laravel 用户认证
- Laravel 重写用户登录
- Laravel 用户之间关注
- 数据库
- bootstarp table 列横向扩展(动态列)
- 成员变量、局部变量和全局变量
- itext合并两个PDF流
- JDBC编程之数据查询
- laravel 用户身份验证
- 嵌入式Linux磁盘(硬盘、SD卡)读写性能测试
- 前端练手项目小结
- Linux下使用openssl的AES加密-ECB模式
- 实现屏幕右划退出当前Activity(带动画效果)
- 数据库基础
- okhttp get post请求
- STM32 GPIO寄存器ODR BSRR BRR
- css如何清除浮动(四)