Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
aic_duty_strategy
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
yinxiaoling
aic_duty_strategy
Commits
e1b44d3c
Commit
e1b44d3c
authored
May 15, 2020
by
yinxiaoling
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
登录验证机制修改
parent
56c0bf3b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
86 deletions
+33
-86
.env
.env
+1
-1
LoginApiController.php
app/Http/Controllers/LoginApiController.php
+21
-54
CheckApiKey.php
app/Http/Middleware/CheckApiKey.php
+11
-31
No files found.
.env
View file @
e1b44d3c
...
...
@@ -9,7 +9,7 @@ LOG_CHANNEL=stack
DB_CONNECTION=pgsql
DB_HOST=120.77.61.117
DB_PORT=5432
DB_DATABASE=
postgres
DB_DATABASE=
strategy
DB_USERNAME=postgres
DB_PASSWORD=yhhl123
...
...
app/Http/Controllers/LoginApiController.php
View file @
e1b44d3c
...
...
@@ -7,6 +7,7 @@ use App\Http\models\users;
use
Illuminate\Support\Facades\Log
;
use
App\User
;
use
Illuminate\Support\Facades\DB
;
use
Illuminate\Support\Facades\Redis
;
class
LoginApiController
extends
Controller
{
...
...
@@ -31,63 +32,29 @@ class LoginApiController extends Controller
return
$this
->
errorWithInfo
(
'用户名不正确'
,
401
);
}
$user
=
DB
::
table
(
'users'
)
->
select
(
'status'
,
'user_name'
,
'password'
,
'user_sn'
,
'team_name'
,
'password'
,
'expired'
,
'ai_count'
,
'caller_group'
,
'api_key'
,
'parent_sn'
)
->
where
(
'user_name'
,
$param
[
'username'
])
->
where
(
function
(
$query
){
$query
->
where
(
'status'
,
0
)
->
orWhere
(
function
(
$query
){
$query
->
whereNull
(
'status'
);
});
})
->
first
();
if
(
empty
(
$user
)){
return
$this
->
errorWithInfo
(
'您的账号不存在'
,
401
);
}
if
(
$user
->
status
==
1
){
return
$this
->
errorWithInfo
(
'您的账号已被清理,请联系管理员处理'
,
401
);
}
if
(
empty
(
$user
->
parent_sn
)){
$the_expired
=
$user
->
expired
;
//调用小a登录接口
$url
=
"https://test117.ciopaas.com/api/login"
;
$params
=
[
'username'
=>
$param
[
'username'
],
'password'
=>
$param
[
'password'
],
'from'
=>
"2"
];
$params
[
'url'
]
=
substr
(
md5
(
json_encode
(
$params
)),
1
,
8
);
$requestReturn
=
$this
->
requestPost
(
$url
,
$params
);
//dump($requestReturn);
$requestReturnArr
=
json_decode
(
$requestReturn
,
true
);
if
(
isset
(
$requestReturnArr
[
'code'
])){
//登录成功
if
(
$requestReturnArr
[
'code'
]
==
0
){
//dd($requestReturnArr);
$rKey
=
'strategy:login:'
.
$requestReturnArr
[
'data'
][
'user_sn'
];
$expired
=
60
*
60
*
10
;
//从登陆后有效时长
$r
=
Redis
::
setex
(
$rKey
,
$expired
,
json_encode
(
$requestReturnArr
[
'data'
]));
/* $u = Redis::get($rKey);
dd($u); */
return
$this
->
successWithInfo
(
$requestReturnArr
[
'data'
]);
}
else
{
return
$this
->
errorWithInfo
(
$requestReturnArr
[
'msg'
],
401
);
}
}
else
{
$puser
=
DB
::
table
(
'users'
)
->
where
(
'user_name'
,
$user
->
parent_sn
)
->
select
(
'expired'
)
->
first
();
$the_expired
=
$puser
->
expired
;
}
$todayDate
=
strtotime
(
date
(
'y-m-d 00:00:00'
,
time
()));
$expiredDate
=
strtotime
(
$the_expired
.
' 00:00:00'
);
if
(
empty
(
$the_expired
)
||
$expiredDate
-
$todayDate
<=
0
){
return
$this
->
errorWithInfo
(
'您的账号已过期,请联系管理员处理'
,
401
);
}
if
(
empty
(
$user
)
||
!
password_verify
(
$param
[
'password'
],
$user
->
password
)){
return
$this
->
errorWithInfo
(
'用户名或密码错误'
,
401
);
return
$this
->
errorWithInfo
(
'登录接口异常'
,
401
);
}
//生成api_key
$api_key
=
$this
->
create_apikey
(
8
);
$expire
=
time
()
+
3600
*
10
;
//有效时间10小时
$data
[
'api_key'
]
=
$api_key
;
$data
[
'api_key_expire'
]
=
$expire
;
$rs
=
DB
::
table
(
'users'
)
->
where
(
'user_sn'
,
$user
->
user_sn
)
->
update
(
$data
);
$user
->
api_key
=
$api_key
;
$user
->
api_key_expire
=
$expire
;
unset
(
$user
->
password
);
if
(
$rs
){
return
$this
->
successWithInfo
([
'data'
=>
(
array
)
$user
]);
}
else
{
return
$this
->
errorWithInfo
(
'登录失败'
,
401
);
}
}
private
function
create_apikey
(
$length
=
8
)
{
// 密码字符集,可任意添加你需要的字符
$chars
=
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
;
$password
=
''
;
for
(
$i
=
0
;
$i
<
$length
;
$i
++
)
{
$password
.=
$chars
[
mt_rand
(
0
,
strlen
(
$chars
)
-
1
)
];
}
return
$password
;
}
}
app/Http/Middleware/CheckApiKey.php
View file @
e1b44d3c
...
...
@@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Log;
use
App\User
;
use
Illuminate\Support\Facades\DB
;
use
App\Http\Controllers\Tool
;
use
Illuminate\Support\Facades\Redis
;
class
CheckApiKey
{
use
Tool
;
...
...
@@ -21,7 +22,7 @@ class CheckApiKey
public
function
handle
(
$request
,
Closure
$next
)
{
try
{
$url
=
$request
->
url
;
//
$url=$request->url;
//$api_key=$request->api_key;
$user_sn
=
$request
->
user_sn
;
$api_key
=
$request
->
header
(
'api-key'
);
//header头里面不能用下划线的参数
...
...
@@ -30,38 +31,17 @@ class CheckApiKey
return
$this
->
errorWithInfo
(
'参数验证不通过'
,
401
);
}
$param
=
$request
->
except
([
'url'
,
'client_info_json'
,
'source'
,
'aes'
,
'upload'
,
'fail_recall_of_reason'
]);
Log
::
channel
(
'api'
)
->
info
(
'API_PARAM:'
.
json_encode
(
$param
));
Log
::
channel
(
'api'
)
->
info
(
'API_SERVER_PARAM:'
.
substr
(
md5
(
json_encode
(
$param
)),
1
,
8
));
/* Log::channel('api')->info('API_CLIENT_PARAM:'.$url);
$md_url = substr(md5(json_encode($param)),1,8);
if(strcmp($url,$md_url)){
if($_SERVER['REQUEST_SCHEME'] == 'http'){
return $this->errorWithInfo('验证url失败', 401);
}
} */
$user
=
DB
::
table
(
'users'
)
->
where
(
'user_sn'
,
$param
[
'user_sn'
])
->
first
();
if
(
empty
(
$user
->
parent_sn
)){
$the_expired
=
$user
->
expired
;
$rKey
=
'strategy:login:'
.
$user_sn
;
if
(
!
Redis
::
exists
(
$rKey
)){
return
$this
->
errorWithInfo
(
'登陆失效'
,
401
);
}
else
{
$puser
=
DB
::
table
(
'users'
)
->
where
(
'user_name'
,
$user
->
parent_sn
)
->
select
(
'expired'
)
->
first
();
$the_expired
=
$puser
->
expired
;
}
if
(
$api_key
!=
$user
->
api_key
){
return
$this
->
errorWithInfo
(
'key验证失败'
,
401
);
}
$todayDate
=
strtotime
(
date
(
'y-m-d 00:00:00'
,
time
()));
$expiredDate
=
strtotime
(
$the_expired
.
' 00:00:00'
);
if
(
empty
(
$the_expired
)
||
$expiredDate
-
$todayDate
<=
0
){
return
$this
->
errorWithInfo
(
'您的账号已过期,请联系管理员处理'
,
401
);
$user
=
Redis
::
get
(
$rKey
);
$user
=
json_decode
(
$user
,
true
);
if
(
$api_key
!=
$user
[
'api_key'
]){
return
$this
->
errorWithInfo
(
'key验证不通过'
,
401
);
}
}
$request
->
attributes
->
add
([
'user'
=>
$user
]);
//添加参数
}
catch
(
\Exception
$e
){
Log
::
channel
(
'api'
)
->
error
(
$e
->
getFile
()
.
'-'
.
$e
->
getLine
()
.
'-'
.
$e
->
getMessage
());
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment