laravel框架--单点登录

// 登录验证
$result = \DB::table(‘user_login’)->where([‘username’ => $input[‘username’], ‘password’ => i n p u t [ ′ p a s s ′ ] ] ) − > f i n d ( ) i f ( input['pass']])->find() if ( input[pass]])>find()if(result) {

登录成功

// 制作 token
$time = time();
// md5 加密
s i n g l e T o k e n = m d 5 ( singleToken = md5( singleToken=md5(request->getClientIp() . $result->guid . $time);
// 当前 time 存入 Redis
\Redis::set(STRING_SINGLETOKEN_ . $result->guid, $time);
// 用户信息存入 Session
\Session::put(‘user_login’, $result);
// 跳转到首页, 并附带 Cookie
return response()->view(‘index’)->withCookie(‘SINGLETOKEN’, $singletoken);
} else {

登录失败逻辑处理

}
我来解释一下: 首先登录成功之后, 得到目前时间戳, 通过IP, time, 和 查询得出用户的 Guid 进行 MD5 加密, 得到 TOKEN 然后我们将刚刚得到的时间戳, 存入 Redis Redis Key 为字符串拼接上 Guid, 方便后面中间件的 TOKEN 验证, 然后我们把用户信息存入 Session 最后我们把计算的 TOKEN 以 Cookie 发送给客户端.

现在到中间件中写程序 app/Http/Middleware/SsoMiddleware.php, 在文件中有 handle 方法, 我们在这个方法中写逻辑.
public function handle($request, Closure $next)
{
KaTeX parse error: Undefined control sequence: \Session at position 12: userInfo = \̲S̲e̲s̲s̲i̲o̲n̲::get('user_log…userInfo) {
// 获取 Cookie 中的 token
$singletoken = r e q u e s t − > c o o k i e ( ′ S I N G L E T O K E N ′ ) ; i f ( request->cookie('SINGLETOKEN'); if ( request>cookie(SINGLETOKEN);if(singletoken) {
// 从 Redis 获取 time
$redisTime = \Redis::get(STRING_SINGLETOKEN_ . $userInfo->guid);
// 重新获取加密参数加密
$ip = $request->getClientIp();
s e c r e t = m d 5 ( secret = md5( secret=md5(ip . $userInfo->guid . r e d i s T i m e ) ; i f ( redisTime); if ( redisTime);if(singletoken != $secret) {
// 记录此次异常登录记录
\DB::table(‘data_login_exception’)->insert([‘guid’ => $userInfo->guid, ‘ip’ => $ip, ‘addtime’ => time()]);
// 清除 session 数据
\Session::forget(‘indexlogin’);
return view(’/403’)->with([‘Msg’ => ‘您的帐号在另一个地点登录…’]);
}
return n e x t ( next( next(request);
} else {
return redirect(’/login’);
}
} else {
return redirect(’/login’);
}
}
我们逻辑写完了, 最后一步就是将用户登录后的每一步操作都掌控在自己手里, 这里我们就需要路由组.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值