您可以通过发出一个额外的 cookie(比如说 browser_cookie)来记住已经过身份验证的浏览器来实现这一点。
实施:
创建下表(browser_management):
token (pk)| user_id (fk) | series_identifier
地点:
每当用户登录时检查browser_cookie。
案例1:用户第一次登录。
考虑到用户是第一次登录,browser_cookie 将不存在。因此,您将发送一封带有验证码的电子邮件。
通过身份验证后,为token 和series_identifier 分别生成两个随机数。对于user_id 标识的用户,将散列的token 和series_identifier 存储在browser_management 表中。
另外,使用token 和series_identifier 向用户发出browser_cookie。
案例2:用户下次重新登录。
现在,当同一用户下次登录时,获取token 并在browser_management 表中找到带有散列token 的条目。
如果找到,请检查 user_id 和 series_identifier 是否匹配。
案例 2.1: 匹配的条目:
允许用户进入系统而无需重新验证电子邮件代码。
生成另一个令牌并将cookie 和table 中的token 替换为新令牌。 (这将降低会话劫持的风险)。
案例 2.2: 条目不匹配:
按照电子邮件身份验证的步骤并通知用户可能被盗。(就像 gmail 通知新的浏览器登录一样)。
参考资料:
更新:
示例代码:
迁移:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class browser_management extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('browser_management', function (Blueprint $table) {
$table->string('token');
$table->string('user_id');
$table->string('series_identifier');
$table->timestamps();
$table->primary('token');
$table->foreign('user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
中间件:创建一个新的中间件
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use Cookies;
class Email_verification
{
public function handle($request, Closure $next, $guard = null)
{
//retrieve $token from the user's cookie
$token = $request->cookie('browser_cookie');
//check id token is present
if($token == null){
//if token is not present allow the request to the email_verification
return $next($request);
}
else{
//Retrieve the series_identifier issued to the user
$series_identifier = Auth::user()
->series_identifier(Hash::make($token))
->first()
->series_identifier;
//Check if series_identifier matches
if($series_identifier != $request->cookie('series_identifier')){
//if series_identifier does not match allow the request to the email_verification
return $next($request);
}
}
return redirect('/dashboard'); //replace this with your route for home page
}
}
在kernel.php中添加中间件的入口
protected $routeMiddleware = [
'email_verification' => \App\Http\Middleware\Email_verification::class,
//your middlewares
];
用户模型:将以下方法添加到您的用户模型中
// method to retrieve series_identifier related to token
public function series_identifier($token){
return $this->hasMany(Browser_management::class)->where('token',$token);
}
//method to retriev the tokens related to user
public function tokens (){
return $this->hasMany(Browser_management::class);
}
Browser_management 模型:创建一个模型来表示 browser_managements 表
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Browser_management extends Model
{
protected $primaryKey = 'token';
protected $fillable = array('token','series_identifier');
public function User(){
return $this->hasOne('App\Models\User');
}
}
电子邮件验证方法:将以下方法添加到您的 AuthController 以处理电子邮件验证
public function getVerification(Request $request){
//Create a random string to represent the token to be sent to user via email.
//You can use any string as we are going to hash it in our DB
$token = str_random(16);
//Generate random string to represent series_identifier
$series_identifier = str_random(64);
//Issue cookie to user with the generated series_identifier
Cookie::queue('series_identifier', $series_identifier,43200,null,null,true,true);
//Store the hashed token and series_identifier ini DB
Auth::user()->tokens()->create(['token'=>Hash::make($token)]);
//Your code to send an email for authentication
//return the view with form asking for token
return view('auth.email_verification');
}
public function postVerification(Request $request){
//Retrieve the series_identifier issued to the user in above method
$series_identifier = $request->cookie('series_identifier');
//Retrieve the token associated with the series_identifier
$token = Auth::user()
->tokens()
->where('series_identifier',$series_identifier)
->first()
->value('token');
//Check if the user's token's hash matches our token entry
if(Hash::check($request->token,$token)){
// If token matched, issue the cookie with token id in it. Which we can use in future to authenticate the user
Cookie::queue('token', $token,43200,null,null,true,true);
return redirect('dashboard');
}
//If token did not match, redirect user bak to the form with error
return redirect()->back()
->with('msg','Tokens did not match');
}
路由:添加这些路由以处理电子邮件验证请求。我们还将在其中添加 email_verification 中间件。
Route::get('/auth/email_verification',`AuthController@getVerification')->middleware('email_verification');
Route::post('/auth/email_verification',`AuthController@postVerification')->middleware('email_verification');<br/>
更新 2:
关于gmail..的流程..
我按照以下步骤操作:
1)登录gmail,然后进行两步验证。
2)退出
3)清除缓存link
4)再次登录
当我再次登录时,在清除缓存后,它并没有要求我进行两步验证。
不过,如果您清除 cookie,它会要求进行两步验证。
原因:
所有识别用户的用户数据(此处为 token)都存储在 cookie 中。如果您清除 cookie,服务器将无法识别用户。
更新 3:
Gmail 要求进行两步验证:
首先,Gmail 或任何其他网站不会收到有关清除缓存的通知
如给定here:
缓存只不过是硬盘上的一个地方
浏览器保留下载过的内容以备不时之需
再次。
现在,cookies 是服务器发出的用于存储用户相关信息的小文本文件。如给定here
cookie 的主要目的是识别用户并可能做好准备
自定义网页或为您保存网站登录信息。
因此,基本上当您清除浏览器中的 cookie 时,网络服务器将不会获取任何用户数据。因此,该用户将被视为访客并受到相应的对待。