update:优化auth认证

This commit is contained in:
JaguarJack 2020-10-26 19:09:28 +08:00
parent 91a1d253c5
commit 4506219fb2
2 changed files with 186 additions and 163 deletions

View File

@ -12,33 +12,95 @@ use thans\jwt\facade\JWTAuth;
class Index extends CatchController
{
/**
* 登陆
*
* @time 2019年11月28日
* @param LoginRequest $request
* @param CatchAuth $auth
* @return bool|string
*/
/**
* 登陆
*
* @time 2019年11月28日
* @param LoginRequest $request
* @param CatchAuth $auth
* @return bool|string
*/
public function login(LoginRequest $request, CatchAuth $auth)
{
$condition = $request->param();
try {
$token = $auth->attempt($condition);
$user = $auth->user();
$this->afterLoginSuccess($user);
// 登录事件
$this->loginEvent($user->username);
return CatchResponse::success([
'token' => $auth->attempt($request->param()),
'token' => $token,
], '登录成功');
} catch (\Exception $exception) {
$code = $exception->getCode();
return CatchResponse::fail($code == Code::USER_FORBIDDEN ?
'该账户已被禁用,请联系管理员' : '登录失败,请检查邮箱和密码', Code::LOGIN_FAILED);
$this->detailWithLoginFailed($exception, $condition);
$code = $exception->getCode();
return CatchResponse::fail($code == Code::USER_FORBIDDEN ?
'该账户已被禁用,请联系管理员' : '登录失败,请检查邮箱和密码', Code::LOGIN_FAILED);
}
}
/**
* 登出
*
* @time 2019年11月28日
* @return \think\response\Json
*/
/**
* 处理登录失败
*
* @time 2020年10月26日
* @param $exception
* @param $condition
* @return void
*/
protected function detailWithLoginFailed($exception, $condition)
{
$message = $exception->getMessage();
if (strpos($message, '|') !== false) {
$username = explode('|', $message)[1];
} else {
$username = $condition['email'];
}
$this->loginEvent($username, false);
}
/**
* 用户登录成功后
*
* @time 2020年09月09日
* @param $user
* @return void
*/
protected function afterLoginSuccess($user)
{
$user->last_login_ip = request()->ip();
$user->last_login_time = time();
$user->save();
}
/**
* 登录事件
*
* @time 2020年09月09日
* @param $name
* @param bool $success
* @return void
*/
protected function loginEvent($name, $success = true)
{
$params['login_name'] = $name;
$params['success'] = $success ? 1 : 2;
event('loginLog', $params);
}
/**
* 登出
*
* @time 2019年11月28日
* @return \think\response\Json
*/
public function logout(): \think\response\Json
{
return CatchResponse::success();

View File

@ -29,31 +29,30 @@ class CatchAuth
$this->guard = $this->auth['default']['guard'];
}
/**
* set guard
*
* @time 2020年01月07日
* @param $guard
* @return $this
*/
/**
* set guard
*
* @time 2020年01月07日
* @param $guard
* @return $this
*/
public function guard($guard)
{
$this->guard = $guard;
$this->guard = $guard;
return $this;
return $this;
}
/**
*
* @time 2020年01月07日
* @param $condition
* @return mixed
*/
/**
*
* @time 2020年01月07日
* @param $condition
* @return mixed
*/
public function attempt($condition)
{
try {
$user = $this->authenticate($condition);
if (!$user) {
throw new LoginFailedException();
}
@ -65,51 +64,13 @@ class CatchAuth
throw new LoginFailedException('登录失败|' . $user->username);
}
$token = $this->{$this->getDriver()}($user);
$this->afterLoginSuccess($user);
// 登录事件
$this->loginEvent($user->username);
return $token;
return $this->{$this->getDriver()}($user);
} catch (\Exception $exception) {
$message = $exception->getMessage();
if (strpos($message, '|') !== false) {
$username = explode('|', $message)[1];
} else {
$username = $condition['email'];
}
$this->loginEvent($username, false);
throw new LoginFailedException('登录失败', $exception->getCode());
//
}
}
/**
* 用户登录成功后
*
* @time 2020年09月09日
* @param $user
* @return void
*/
protected function afterLoginSuccess($user)
{
$user->last_login_ip = request()->ip();
$user->last_login_time = time();
$user->save();
}
/**
* 登录事件
*
* @time 2020年09月09日
* @param $name
* @param bool $success
* @return void
*/
protected function loginEvent($name, $success = true)
{
$params['login_name'] = $name;
$params['success'] = $success ? 1 : 2;
event('loginLog', $params);
}
/**
* user
@ -142,30 +103,30 @@ class CatchAuth
return $user;
}
/**
*
* @time 2020年01月07日
* @return mixed
*/
/**
*
* @time 2020年01月07日
* @return mixed
*/
public function logout()
{
switch ($this->getDriver()) {
case 'jwt':
return true;
case 'session':
Session::delete($this->sessionUserKey());
return true;
default:
throw new FailedException('user not found');
}
switch ($this->getDriver()) {
case 'jwt':
return true;
case 'session':
Session::delete($this->sessionUserKey());
return true;
default:
throw new FailedException('user not found');
}
}
/**
*
* @time 2020年01月07日
* @param $user
* @return string
*/
/**
*
* @time 2020年01月07日
* @param $user
* @return string
*/
protected function jwt($user)
{
$token = JWTAuth::builder([$this->jwtKey() => $user->id]);
@ -175,52 +136,52 @@ class CatchAuth
return $token;
}
/**
*
* @time 2020年01月07日
* @param $user
* @return void
*/
/**
*
* @time 2020年01月07日
* @param $user
* @return void
*/
protected function session($user)
{
Session::set($this->sessionUserKey(), $user);
}
/**
*
* @time 2020年01月07日
* @return string
*/
/**
*
* @time 2020年01月07日
* @return string
*/
protected function sessionUserKey()
{
return $this->guard . '_user';
return $this->guard . '_user';
}
/**
*
* @time 2020年01月07日
* @return string
*/
/**
*
* @time 2020年01月07日
* @return string
*/
protected function jwtKey()
{
return $this->guard . '_id';
return $this->guard . '_id';
}
/**
*
* @time 2020年01月07日
* @return mixed
*/
/**
*
* @time 2020年01月07日
* @return mixed
*/
protected function getDriver()
{
return $this->auth['guards'][$this->guard]['driver'];
return $this->auth['guards'][$this->guard]['driver'];
}
/**
*
* @time 2020年01月07日
* @return mixed
*/
/**
*
* @time 2020年01月07日
* @return mixed
*/
protected function getProvider()
{
if (!isset($this->auth['guards'][$this->guard])) {
@ -230,12 +191,12 @@ class CatchAuth
return $this->auth['providers'][$this->auth['guards'][$this->guard]['provider']];
}
/**
*
* @time 2020年01月07日
* @param $condition
* @return mixed
*/
/**
*
* @time 2020年01月07日
* @param $condition
* @return mixed
*/
protected function authenticate($condition)
{
$provider = $this->getProvider();
@ -243,51 +204,51 @@ class CatchAuth
return $this->{$provider['driver']}($condition);
}
/**
*
* @time 2020年01月07日
* @param $condition
* @return void
*/
/**
*
* @time 2020年01月07日
* @param $condition
* @return void
*/
protected function database($condition): void
{}
/**
*
* @time 2020年01月07日
* @param $condition
* @return mixed
*/
/**
*
* @time 2020年01月07日
* @param $condition
* @return mixed
*/
protected function orm($condition)
{
return app($this->getProvider()['model'])->where($this->filter($condition))->find();
}
/**
*
* @time 2020年01月07日
* @param $condition
* @return array
*/
/**
*
* @time 2020年01月07日
* @param $condition
* @return array
*/
protected function filter($condition): array
{
$where = [];
foreach ($condition as $field => $value) {
if ($field != $this->password) {
$where[$field] = $value;
}
if ($field != $this->password) {
$where[$field] = $value;
}
}
return $where;
}
/**
*
* @time 2020年01月07日
* @param $field
* @return $this
*/
/**
*
* @time 2020年01月07日
* @param $field
* @return $this
*/
public function username($field): self
{
$this->username = $field;
@ -295,12 +256,12 @@ class CatchAuth
return $this;
}
/**
*
* @time 2020年01月07日
* @param $field
* @return $this
*/
/**
*
* @time 2020年01月07日
* @param $field
* @return $this
*/
public function password($field): self
{
$this->password = $field;