Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
c7cc082d
Commit
c7cc082d
authored
Jun 14, 2019
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
http文件鉴权支持自定义错误提示
parent
cfbdda06
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
101 行增加
和
81 行删除
+101
-81
server/WebApi.cpp
+8
-19
server/WebHook.cpp
+7
-9
src/Http/HttpCookieManager.cpp
+7
-2
src/Http/HttpCookieManager.h
+7
-0
src/Http/HttpSession.cpp
+65
-49
src/Http/HttpSession.h
+7
-2
没有找到文件。
server/WebApi.cpp
查看文件 @
c7cc082d
...
...
@@ -673,34 +673,23 @@ void installWebApi() {
};
API_REGIST
(
hook
,
on_http_access
,{
#if 0
//能访问根目录以及根目录下所有文件10分钟
val["path"] = "/";
val["second"] = 10 * 60;
#else
//在这里根据allArgs["params"](url参数)来判断该http客户端是否有权限访问该文件
if
(
!
checkAccess
(
allArgs
[
"params"
])){
//无访问权限
val
[
"err"
]
=
"无访问权限"
;
//仅限制访问当前目录
val
[
"path"
]
=
""
;
//标记该客户端无权限1分钟,1分钟之内它凭此cookie访问将都无权限
//如果客户端不支持cookie,那么可以根据url参数来追踪用户,请参考kBroadcastTrackHttpClient事件
//如果服务器未处理kBroadcastTrackHttpClient事件,那么ZLMediaKit会根据ip和端口追踪用户
//标记该客户端无权限1分钟
val
[
"second"
]
=
60
;
return
;
}
//只能访问本文件,且只授权10分钟,访问其他文件都要另外授权
if
(
allArgs
[
"is_dir"
].
as
<
bool
>
()){
//访问的是目录,该授权cookie只对该目录有效
val
[
"path"
]
=
(
string
)
allArgs
[
"path"
];
}
else
{
//访问的是文件,那么我们授予客户端访问所在目录的权限
string
dir
=
allArgs
[
"path"
].
substr
(
0
,
allArgs
[
"path"
].
rfind
(
"/"
)
+
1
);
val
[
"path"
]
=
dir
;
}
//该http客户端用户被授予10分钟的访问权限,该权限仅限访问特定目录
//可以访问
val
[
"err"
]
=
""
;
//只能访问当前目录
val
[
"path"
]
=
""
;
//该http客户端用户被授予10分钟的访问权限,该权限仅限访问当前目录
val
[
"second"
]
=
10
*
60
;
#endif
});
...
...
server/WebHook.cpp
查看文件 @
c7cc082d
...
...
@@ -399,13 +399,13 @@ void installWebHook(){
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastHttpAccess
,[](
BroadcastHttpAccessArgs
){
if
(
sender
.
get_peer_ip
()
==
"127.0.0.1"
&&
args
.
_param_strs
==
hook_adminparams
){
//如果是本机或超级管理员访问,那么不做访问鉴权;权限有效期1个小时
invoker
(
"
/
"
,
60
*
60
);
invoker
(
"
"
,
"
"
,
60
*
60
);
return
;
}
if
(
!
hook_enable
||
hook_http_access
.
empty
()){
//未开启http文件访问鉴权,那么允许访问,但是每次访问都要鉴权;
//因为后续随时都可能开启鉴权(重载配置文件后可能重新开启鉴权)
invoker
(
"
/
"
,
0
);
invoker
(
"
"
,
"
"
,
0
);
return
;
}
...
...
@@ -423,15 +423,13 @@ void installWebHook(){
do_http_hook
(
hook_http_access
,
body
,
[
invoker
](
const
Value
&
obj
,
const
string
&
err
){
if
(
!
err
.
empty
()){
//如果接口访问失败,那么仅限本次没有访问http服务器的权限
invoker
(
""
,
0
);
invoker
(
err
,
""
,
0
);
return
;
}
//path参数是该客户端能访问的顶端目录,该目录下的所有文件它都能访问
//second参数规定该cookie超时时间,超过这个时间后,用户需要重新鉴权
//如果path为空字符串,则为禁止访问任何目录
//如果second为0,本次鉴权结果不缓存
//如果被禁止访问文件,在cookie有效期内,假定再次访问的url参数变了,那么也能立即触发重新鉴权操作
invoker
(
obj
[
"path"
].
asString
(),
obj
[
"second"
].
asInt
());
//err参数代表不能访问的原因,空则代表可以访问
//path参数是该客户端能访问或被禁止的顶端目录,如果path为空字符串,则表述为当前目录
//second参数规定该cookie超时时间,如果second为0,本次鉴权结果不缓存
invoker
(
obj
[
"err"
].
asString
(),
obj
[
"path"
].
asString
(),
obj
[
"second"
].
asInt
());
});
});
}
...
...
src/Http/HttpCookieManager.cpp
查看文件 @
c7cc082d
...
...
@@ -76,6 +76,10 @@ bool HttpServerCookie::isExpired() {
return
_ticker
.
elapsedTime
()
>
_max_elapsed
*
1000
;
}
std
::
shared_ptr
<
lock_guard
<
mutex
>
>
HttpServerCookie
::
getLock
(){
return
std
::
make_shared
<
lock_guard
<
mutex
>
>
(
_mtx
);
}
string
HttpServerCookie
::
cookieExpireTime
()
const
{
char
buf
[
64
];
time_t
tt
=
time
(
NULL
)
+
_max_elapsed
;
...
...
@@ -105,7 +109,7 @@ void HttpCookieManager::onManager() {
for
(
auto
it_cookie
=
it_name
->
second
.
begin
()
;
it_cookie
!=
it_name
->
second
.
end
()
;
){
if
(
it_cookie
->
second
->
isExpired
()){
//cookie过期,移除记录
WarnL
<<
it_cookie
->
second
->
getUid
()
<<
" cookie过期"
;
DebugL
<<
it_cookie
->
second
->
getUid
()
<<
" cookie过期:"
<<
it_cookie
->
second
->
getCookie
()
;
it_cookie
=
it_name
->
second
.
erase
(
it_cookie
);
continue
;
}
...
...
@@ -114,7 +118,7 @@ void HttpCookieManager::onManager() {
if
(
it_name
->
second
.
empty
()){
//该类型下没有任何cooki记录,移除之
Warn
L
<<
"该path下没有任何cooki记录:"
<<
it_name
->
first
;
Debug
L
<<
"该path下没有任何cooki记录:"
<<
it_name
->
first
;
it_name
=
_map_cookie
.
erase
(
it_name
);
continue
;
}
...
...
@@ -152,6 +156,7 @@ HttpServerCookie::Ptr HttpCookieManager::getCookie(const string &cookie_name,con
}
if
(
it_cookie
->
second
->
isExpired
()){
//cookie过期
DebugL
<<
"cookie过期:"
<<
it_cookie
->
second
->
getCookie
();
it_name
->
second
.
erase
(
it_cookie
);
return
nullptr
;
}
...
...
src/Http/HttpCookieManager.h
查看文件 @
c7cc082d
...
...
@@ -102,6 +102,12 @@ public:
* @return
*/
bool
isExpired
();
/**
* 获取区域锁
* @return
*/
std
::
shared_ptr
<
lock_guard
<
mutex
>
>
getLock
();
private
:
string
cookieExpireTime
()
const
;
private
:
...
...
@@ -110,6 +116,7 @@ private:
string
_cookie_uuid
;
uint64_t
_max_elapsed
;
Ticker
_ticker
;
mutex
_mtx
;
std
::
weak_ptr
<
HttpCookieManager
>
_manager
;
};
...
...
src/Http/HttpSession.cpp
查看文件 @
c7cc082d
差异被折叠。
点击展开。
src/Http/HttpSession.h
查看文件 @
c7cc082d
...
...
@@ -52,7 +52,12 @@ public:
const
KeyValue
&
headerOut
,
const
string
&
contentOut
)
>
HttpResponseInvoker
;
typedef
std
::
function
<
void
(
const
string
&
accessPath
,
int
cookieLifeSecond
)
>
HttpAccessPathInvoker
;
/**
* @param errMsg 如果为空,则代表鉴权通过,否则为错误提示
* @param accessPath 运行或禁止访问的根目录
* @param cookieLifeSecond 鉴权cookie有效期
**/
typedef
std
::
function
<
void
(
const
string
&
errMsg
,
const
string
&
accessPath
,
int
cookieLifeSecond
)
>
HttpAccessPathInvoker
;
HttpSession
(
const
Socket
::
Ptr
&
pSock
);
virtual
~
HttpSession
();
...
...
@@ -125,7 +130,7 @@ private:
* @param is_dir path是否为目录
* @param callback 有权限或无权限的回调
*/
inline
void
canAccessPath
(
const
string
&
path
,
bool
is_dir
,
const
function
<
void
(
bool
canAccess
,
const
HttpServerCookie
::
Ptr
&
cookie
)
>
&
callback
);
inline
void
canAccessPath
(
const
string
&
path
,
bool
is_dir
,
const
function
<
void
(
const
string
&
errMsg
,
const
HttpServerCookie
::
Ptr
&
cookie
)
>
&
callback
);
/**
* 获取用户唯一识别id
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论