update:优化auth认证
This commit is contained in:
parent
91a1d253c5
commit
4506219fb2
@ -12,33 +12,95 @@ use thans\jwt\facade\JWTAuth;
|
|||||||
|
|
||||||
class Index extends CatchController
|
class Index extends CatchController
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* 登陆
|
* 登陆
|
||||||
*
|
*
|
||||||
* @time 2019年11月28日
|
* @time 2019年11月28日
|
||||||
* @param LoginRequest $request
|
* @param LoginRequest $request
|
||||||
* @param CatchAuth $auth
|
* @param CatchAuth $auth
|
||||||
* @return bool|string
|
* @return bool|string
|
||||||
*/
|
*/
|
||||||
public function login(LoginRequest $request, CatchAuth $auth)
|
public function login(LoginRequest $request, CatchAuth $auth)
|
||||||
{
|
{
|
||||||
|
$condition = $request->param();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
$token = $auth->attempt($condition);
|
||||||
|
|
||||||
|
$user = $auth->user();
|
||||||
|
|
||||||
|
$this->afterLoginSuccess($user);
|
||||||
|
// 登录事件
|
||||||
|
$this->loginEvent($user->username);
|
||||||
|
|
||||||
return CatchResponse::success([
|
return CatchResponse::success([
|
||||||
'token' => $auth->attempt($request->param()),
|
'token' => $token,
|
||||||
], '登录成功');
|
], '登录成功');
|
||||||
} catch (\Exception $exception) {
|
} catch (\Exception $exception) {
|
||||||
$code = $exception->getCode();
|
$this->detailWithLoginFailed($exception, $condition);
|
||||||
return CatchResponse::fail($code == Code::USER_FORBIDDEN ?
|
$code = $exception->getCode();
|
||||||
'该账户已被禁用,请联系管理员' : '登录失败,请检查邮箱和密码', Code::LOGIN_FAILED);
|
return CatchResponse::fail($code == Code::USER_FORBIDDEN ?
|
||||||
|
'该账户已被禁用,请联系管理员' : '登录失败,请检查邮箱和密码', Code::LOGIN_FAILED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登出
|
* 处理登录失败
|
||||||
*
|
*
|
||||||
* @time 2019年11月28日
|
* @time 2020年10月26日
|
||||||
* @return \think\response\Json
|
* @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
|
public function logout(): \think\response\Json
|
||||||
{
|
{
|
||||||
return CatchResponse::success();
|
return CatchResponse::success();
|
||||||
|
@ -29,31 +29,30 @@ class CatchAuth
|
|||||||
$this->guard = $this->auth['default']['guard'];
|
$this->guard = $this->auth['default']['guard'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set guard
|
* set guard
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @param $guard
|
* @param $guard
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function guard($guard)
|
public function guard($guard)
|
||||||
{
|
{
|
||||||
$this->guard = $guard;
|
$this->guard = $guard;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function attempt($condition)
|
public function attempt($condition)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$user = $this->authenticate($condition);
|
$user = $this->authenticate($condition);
|
||||||
|
|
||||||
if (!$user) {
|
if (!$user) {
|
||||||
throw new LoginFailedException();
|
throw new LoginFailedException();
|
||||||
}
|
}
|
||||||
@ -65,51 +64,13 @@ class CatchAuth
|
|||||||
throw new LoginFailedException('登录失败|' . $user->username);
|
throw new LoginFailedException('登录失败|' . $user->username);
|
||||||
}
|
}
|
||||||
|
|
||||||
$token = $this->{$this->getDriver()}($user);
|
return $this->{$this->getDriver()}($user);
|
||||||
$this->afterLoginSuccess($user);
|
|
||||||
// 登录事件
|
|
||||||
$this->loginEvent($user->username);
|
|
||||||
return $token;
|
|
||||||
} catch (\Exception $exception) {
|
} 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
|
* user
|
||||||
@ -142,30 +103,30 @@ class CatchAuth
|
|||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function logout()
|
public function logout()
|
||||||
{
|
{
|
||||||
switch ($this->getDriver()) {
|
switch ($this->getDriver()) {
|
||||||
case 'jwt':
|
case 'jwt':
|
||||||
return true;
|
return true;
|
||||||
case 'session':
|
case 'session':
|
||||||
Session::delete($this->sessionUserKey());
|
Session::delete($this->sessionUserKey());
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
throw new FailedException('user not found');
|
throw new FailedException('user not found');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @param $user
|
* @param $user
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function jwt($user)
|
protected function jwt($user)
|
||||||
{
|
{
|
||||||
$token = JWTAuth::builder([$this->jwtKey() => $user->id]);
|
$token = JWTAuth::builder([$this->jwtKey() => $user->id]);
|
||||||
@ -175,52 +136,52 @@ class CatchAuth
|
|||||||
return $token;
|
return $token;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @param $user
|
* @param $user
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function session($user)
|
protected function session($user)
|
||||||
{
|
{
|
||||||
Session::set($this->sessionUserKey(), $user);
|
Session::set($this->sessionUserKey(), $user);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function sessionUserKey()
|
protected function sessionUserKey()
|
||||||
{
|
{
|
||||||
return $this->guard . '_user';
|
return $this->guard . '_user';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function jwtKey()
|
protected function jwtKey()
|
||||||
{
|
{
|
||||||
return $this->guard . '_id';
|
return $this->guard . '_id';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected function getDriver()
|
protected function getDriver()
|
||||||
{
|
{
|
||||||
return $this->auth['guards'][$this->guard]['driver'];
|
return $this->auth['guards'][$this->guard]['driver'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected function getProvider()
|
protected function getProvider()
|
||||||
{
|
{
|
||||||
if (!isset($this->auth['guards'][$this->guard])) {
|
if (!isset($this->auth['guards'][$this->guard])) {
|
||||||
@ -230,12 +191,12 @@ class CatchAuth
|
|||||||
return $this->auth['providers'][$this->auth['guards'][$this->guard]['provider']];
|
return $this->auth['providers'][$this->auth['guards'][$this->guard]['provider']];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected function authenticate($condition)
|
protected function authenticate($condition)
|
||||||
{
|
{
|
||||||
$provider = $this->getProvider();
|
$provider = $this->getProvider();
|
||||||
@ -243,51 +204,51 @@ class CatchAuth
|
|||||||
return $this->{$provider['driver']}($condition);
|
return $this->{$provider['driver']}($condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function database($condition): void
|
protected function database($condition): void
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected function orm($condition)
|
protected function orm($condition)
|
||||||
{
|
{
|
||||||
return app($this->getProvider()['model'])->where($this->filter($condition))->find();
|
return app($this->getProvider()['model'])->where($this->filter($condition))->find();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function filter($condition): array
|
protected function filter($condition): array
|
||||||
{
|
{
|
||||||
$where = [];
|
$where = [];
|
||||||
|
|
||||||
foreach ($condition as $field => $value) {
|
foreach ($condition as $field => $value) {
|
||||||
if ($field != $this->password) {
|
if ($field != $this->password) {
|
||||||
$where[$field] = $value;
|
$where[$field] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $where;
|
return $where;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @param $field
|
* @param $field
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function username($field): self
|
public function username($field): self
|
||||||
{
|
{
|
||||||
$this->username = $field;
|
$this->username = $field;
|
||||||
@ -295,12 +256,12 @@ class CatchAuth
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @time 2020年01月07日
|
* @time 2020年01月07日
|
||||||
* @param $field
|
* @param $field
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function password($field): self
|
public function password($field): self
|
||||||
{
|
{
|
||||||
$this->password = $field;
|
$this->password = $field;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user