1.建立数据表 admins

2.找到config下的auth.php 文件,加入如下代码

<?php

return [

    /*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/ 'defaults' => [
'guard' => 'web',
'passwords' => 'users',
], /*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/ 'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
], 'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
//加入admin,注意:admin 和app目录的admin.php名字要一致
'admin' => [
'driver' => 'session',
'provider' => 'admins'
]
], /*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/ 'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
//admins 是刚才建立的admins表,名字同样也要保持一致
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class
] // 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
], /*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/ 'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
], /*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Here you may define the amount of seconds before a password confirmation
| times out and the user is prompted to re-enter their password via the
| confirmation screen. By default, the timeout lasts for three hours.
|
*/ 'password_timeout' => 10800, ];

  

3.在app下建立admin.php

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{ protected $fillable = [
'username', 'password',
]; protected $hidden = [
//remember_token 字段用于记住我的功能
'password', 'remember_token',
];
public static $rules = [
'username'=>'required',
'password'=>'required'
];
}
?>

  

4.建立AdminsController

<?php

namespace App\Http\Controllers;

use App\Http\Requests\UserRequest;
use Illuminate\Support\Facades\Auth; class AdminsController extends Controller
{
//
public function login(){
//echo bcrypt('123456');
//die();
return view('admins.login');
} public function dologin(UserRequest $request){
$credentials = $request->only('username', 'password');
/**
* 使用laravel自带的Auth登录
* 1、密码使用的是hash加密 $crypt = password_hash($password, PASSWORD_DEFAULT);
*/
if(Auth::guard('admin')->attempt($credentials)){
return response()->json(['code' => 200, 'msg' => '登录成功']);
}else{
return response()->json(['code' => 403, 'msg' => '用户名或密码错误']);
}
}
}

 5.建立登录视图页面

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

<div style="margin: 0 auto;width: 1000px">
<h3>用户登录</h3>
<hr>
<form action="{{ url('/admins/dologin') }}" method="post">
<div class="form-group">
<input type="text" name="username" class="form-control" placeholder="请输入账号" style="width: 200px">
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="请输入密码" style="width: 200px">
</div> <div class="form-group">
<input type="text" name="code" class="form-control" placeholder="请输入验证码" style="width: 200px">
<img src="{{captcha_src()}}" onclick="this.src='{{captcha_src()}}'+Math.random()">
</div>
@csrf
<button type="submit" class="btn btn-info">登录</button>
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</form>
</div>

 

6.建立中间件,并配置中间件

Route::group(['prefix' =>'admin','middleware'=>['adminauth']],function(){
Route::get('shop/list','ShopController@index');
}); Route::get('admins/login','AdminsController@login');
Route::post('admins/dologin','AdminsController@dologin');
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth; class AdminAuth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
//如果没有指定的话,默认 guard 是 web,
//dump(Auth::guard($guard)->check()); 判断用户是否认证
//Auth::guard($guard)->user() 获取用户信息
//Auth::guard($guard)->guest() 是否是来宾
if(Auth::guard($guard)->guest()){
if($request->ajax() || $request->wantsJson()){
return response('Unauthorized.', 401);
}else{
return redirect()->guest('admins/login');
}
} return $next($request);
}
}

7.如果你在登录页面使用了验证码的话,可以借助独立验证器进行验证

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
} /**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
'username' => 'required|max:30|min:6',
'password' => 'required|min:6',
'code' => 'required|captcha'
];
}
public function messages()
{
return [
'username.required' => '账号不能为空',
'username.max' => '账号过长',
'username.min' => '账号格式不正确',
'password.required' =>'密码不能为空',
'password.min' => '密码过于简单',
'code.required' =>'验证码不能为空',
'code.captcha' => '验证码错误'
];
}
}

 8.登录页面展示

Laravel-Auth认证的更多相关文章

  1. laravel中的Auth认证:

    简介 Laravel 5.3 的 Auth 认证在 5.2 的基础上又有一些改变,本文说明如何在 Laravel 5.3 下做不同用户表的登录认证. Auth 认证原理简述 Laravel 的认证是使 ...

  2. Laravel Passport认证-多表、多字段解决方案

    Laravel Passport认证-多表.多字段解决方案 2018年08月19日 09:31:01 醉卧码场君莫笑 阅读数:1632   1. 概述 API 通常使用令牌(token)进行认证并且在 ...

  3. Laravel用户认证

    前期准备 Laravel的权限配置文件位于 config/auth.php,Laravel的认证组件由"guards"和"providers"组成, Guard ...

  4. Laravel用户认证系统的实现细节

    原文地址:http://www.php.cn/php-weizijiaocheng-406076.html 这篇文章主要介绍了关于Laravel用户认证系统的实现细节,有着一定的参考价值,现在分享给大 ...

  5. java 发送带Basic Auth认证的http post请求

    构造http header private static final String URL = "url"; private static final String APP_KEY ...

  6. Bootstrap+Thinkphp3.2+Auth认证+jquery-validator后台

    Auth权限认证 本例采用auth权限认证,用户和用户组采用多对多关系处理,自动添加rule规则,带有jquery-validator插件,自动控制菜单显示或隐藏.   config.php中的配置 ...

  7. Django学习之九: auth 认证组件

    目录 Django auth 认证组件 配置使用auth组件及其中间件 request.user 可以直接在template模版中使用 auth组件常用api 获取认证model类 认证检测装饰器@l ...

  8. Django认证系统auth认证

    使用Django认证系统auth认证 auth认证系统可以处理范围非常广泛的任务,且具有一套细致的密码和权限实现.对于需要与默认配置不同需求的项目,Django支持扩展和自定义认证;会将用户信息写入到 ...

  9. auth 认证模块

    . auth认证模块: http://www.cnblogs.com/liwenzhou/p/9030211.html auth模块的知识点: . 创建超级用户 python manage.py cr ...

  10. iOS AFNetWorking下得Basic Auth认证请求方式

    我新入职了一家公司,做了一个项目,服务器的大哥说他采用的是Basic Auth认证请求方式,一般我们用的都是OAuth的认证方式,下面我们就对比一下这两种认证方式 百度百科得到如下 Basic Aut ...

随机推荐

  1. imagenamed和imageWithContentOfFile的区别

    @implementation ViewController /** 图片的两种加载方式: 1> imageNamed: a. 就算指向它的指针被销毁,该资源也不会被从内存中干掉 b. 放到As ...

  2. Pandas中Series与Dataframe的区别

    1. Series Series通俗来讲就是一维数组,索引(index)为每个元素的下标,值(value)为下标对应的值 例如: arr = ['Tom', 'Nancy', 'Jack', 'Ton ...

  3. iOS组件化之-给自己的组件添加资源文件

    在 podspec 中,利用 source_files 可以指定要编译的源代码文件.可是,当我们需要把图片.音频.NIB等资源打包进 Pod 时该怎么办呢? 1.如何把资源文件打包为.bundle文件 ...

  4. Linux 源码安装Ansible 参考篇

    Ansible 源码搭建配置 近期在学习自动化运维相关技术,文章主要模拟内网情况下对Ansible的安装演示,源码安装较为繁琐.枯燥,尤其是在实际安装过程中可能出现各式各样的问题,所有在安装过程中尽量 ...

  5. Vue2.0源码学习(1) - 数据和模板的渲染(上)

    准备 一.首先去GitHub上把vue源码download下来,传送门:https://github.com/vuejs/vue 二.搭建一个vue-cli跑起来,用于代码调试,不看着代码动起来只看源 ...

  6. 日行一算(Table-文字输出)

    题目 +---+---+---+ | | | | +---+---+---+ | | | | +---+---+---+ | | | | +---+---+---+ 题目描述 上图是一个Mysql查询 ...

  7. 本塔科技借力VR Engine,提供更优质的3D VR内容

    本文分享于华为开发者论坛<本塔科技借力HMS Core VR Engine,提供更优质的3D VR视频内容> 本塔科技致力于为影视行业提供优质的3D VR视频内容,追求稳定.观看感舒适.良 ...

  8. Vue 源码解读(6)—— 实例方法

    前言 上一篇文章 Vue 源码解读(5)-- 全局 API 详细介绍了 Vue 的各个全局 API 的实现原理,本篇文章将会详细介绍各个实例方法的实现原理. 目标 深入理解以下实例方法的实现原理. v ...

  9. 4.2 K8S超级完整安装配置

    前言: 采坑 k8s有3种安装方式,如下所示: minikube:这是一个k8s集群模拟器,只有一个节点的集群,只为了测试使用,master和node都在一台机器上 直接使用带有容器功能的云平台安装: ...

  10. RENIX发送固定个数报文——网络测试仪实操

    在使用RENIX软件时,有时候我们需要发送固定个数报文,那么该如何操作呢?以下为您讲解具体操作步骤. 第一步:预约测试资源 打开Renix软件,连接机箱, 预约端口 第二步:发送固定个数的报文 选中流 ...