Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
IEQEFCR
halo
提交
22a7311f
H
halo
项目概览
IEQEFCR
/
halo
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
halo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
22a7311f
编写于
5月 06, 2019
作者:
J
johnniang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor authentication filter again
上级
30f21c50
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
48 addition
and
35 deletion
+48
-35
src/main/java/run/halo/app/security/filter/AbstractAuthenticationFilter.java
...alo/app/security/filter/AbstractAuthenticationFilter.java
+17
-0
src/main/java/run/halo/app/security/filter/AdminAuthenticationFilter.java
...n/halo/app/security/filter/AdminAuthenticationFilter.java
+30
-32
src/main/java/run/halo/app/security/filter/ApiAuthenticationFilter.java
...run/halo/app/security/filter/ApiAuthenticationFilter.java
+1
-3
未找到文件。
src/main/java/run/halo/app/security/filter/AbstractAuthenticationFilter.java
浏览文件 @
22a7311f
...
...
@@ -8,6 +8,7 @@ import org.springframework.web.filter.OncePerRequestFilter;
import
run.halo.app.config.properties.HaloProperties
;
import
run.halo.app.exception.NotInstallException
;
import
run.halo.app.model.properties.PrimaryProperties
;
import
run.halo.app.security.context.SecurityContextHolder
;
import
run.halo.app.security.handler.AuthenticationFailureHandler
;
import
run.halo.app.security.handler.DefaultAuthenticationFailureHandler
;
import
run.halo.app.service.OptionService
;
...
...
@@ -75,6 +76,7 @@ public abstract class AbstractAuthenticationFilter extends OncePerRequestFilter
* @param request http servlet request must not be null.
* @return true if the request should skip authentication failure; false otherwise
*/
@Deprecated
protected
boolean
shouldSkipAuthenticateFailure
(
@NonNull
HttpServletRequest
request
)
{
Assert
.
notNull
(
request
,
"Http servlet request must not be null"
);
...
...
@@ -126,6 +128,7 @@ public abstract class AbstractAuthenticationFilter extends OncePerRequestFilter
* @param url url must not be blank
* @param method method must not be blank
*/
@Deprecated
public
void
addTryAuthUrlMethodPattern
(
@NonNull
String
url
,
@NonNull
String
method
)
{
Assert
.
hasText
(
url
,
"Try authenticating url must not be blank"
);
Assert
.
hasText
(
method
,
"Try authenticating method must not be blank"
);
...
...
@@ -176,5 +179,19 @@ public abstract class AbstractAuthenticationFilter extends OncePerRequestFilter
getFailureHandler
().
onFailure
(
request
,
response
,
new
NotInstallException
(
"The blog has not been initialized yet!"
));
return
;
}
if
(
shouldNotFilter
(
request
))
{
filterChain
.
doFilter
(
request
,
response
);
return
;
}
try
{
// Do authenticate
doAuthenticate
(
request
,
response
,
filterChain
);
}
finally
{
SecurityContextHolder
.
clearContext
();
}
}
protected
abstract
void
doAuthenticate
(
HttpServletRequest
request
,
HttpServletResponse
response
,
FilterChain
filterChain
)
throws
ServletException
,
IOException
;
}
src/main/java/run/halo/app/security/filter/AdminAuthenticationFilter.java
浏览文件 @
22a7311f
...
...
@@ -77,47 +77,45 @@ public class AdminAuthenticationFilter extends AbstractAuthenticationFilter {
}
@Override
protected
void
do
FilterInternal
(
HttpServletRequest
request
,
HttpServletResponse
response
,
FilterChain
filterChain
)
throws
ServletException
,
IOException
{
protected
void
do
Authenticate
(
HttpServletRequest
request
,
HttpServletResponse
response
,
FilterChain
filterChain
)
throws
ServletException
,
IOException
{
super
.
doFilterInternal
(
request
,
response
,
filterChain
);
if
(
haloProperties
.
isAuthEnabled
())
{
// Get token from request
String
token
=
getTokenFromRequest
(
request
);
if
(!
haloProperties
.
isAuthEnabled
())
{
// Set security
userService
.
getCurrentUser
().
ifPresent
(
user
->
SecurityContextHolder
.
setContext
(
new
SecurityContextImpl
(
new
AuthenticationImpl
(
new
UserDetail
(
user
)))));
if
(
StringUtils
.
isBlank
(
token
))
{
if
(!
shouldSkipAuthenticateFailure
(
request
))
{
getFailureHandler
().
onFailure
(
request
,
response
,
new
AuthenticationException
(
"You have to login before accessing admin api"
));
return
;
}
}
else
{
// Get user id from cache
Optional
<
Integer
>
optionalUserId
=
cacheStore
.
getAny
(
SecurityUtils
.
buildTokenAccessKey
(
token
),
Integer
.
class
);
// Do filter
filterChain
.
doFilter
(
request
,
response
);
return
;
}
if
(!
optionalUserId
.
isPresent
())
{
getFailureHandler
().
onFailure
(
request
,
response
,
new
AuthenticationException
(
"The token has been expired or not exist"
).
setErrorData
(
token
));
return
;
}
// Get token from request
String
token
=
getTokenFromRequest
(
request
);
// Get the user
User
user
=
userService
.
getById
(
optionalUserId
.
get
());
if
(
StringUtils
.
isBlank
(
token
))
{
getFailureHandler
().
onFailure
(
request
,
response
,
new
AuthenticationException
(
"You have to login before accessing admin api"
));
return
;
}
// Build user detail
UserDetail
userDetail
=
new
UserDetail
(
user
);
// Get user id from cache
Optional
<
Integer
>
optionalUserId
=
cacheStore
.
getAny
(
SecurityUtils
.
buildTokenAccessKey
(
token
),
Integer
.
class
);
// Set security
SecurityContextHolder
.
setContext
(
new
SecurityContextImpl
(
new
AuthenticationImpl
(
userDetail
)));
}
}
else
{
// Set security
userService
.
getCurrentUser
().
ifPresent
(
user
->
SecurityContextHolder
.
setContext
(
new
SecurityContextImpl
(
new
AuthenticationImpl
(
new
UserDetail
(
user
)))));
if
(!
optionalUserId
.
isPresent
())
{
getFailureHandler
().
onFailure
(
request
,
response
,
new
AuthenticationException
(
"The token has been expired or not exist"
).
setErrorData
(
token
));
return
;
}
filterChain
.
doFilter
(
request
,
response
);
// Get the user
User
user
=
userService
.
getById
(
optionalUserId
.
get
());
// Clear context
SecurityContextHolder
.
clearContext
();
// Build user detail
UserDetail
userDetail
=
new
UserDetail
(
user
);
// Set security
SecurityContextHolder
.
setContext
(
new
SecurityContextImpl
(
new
AuthenticationImpl
(
userDetail
)));
// Do filter
filterChain
.
doFilter
(
request
,
response
);
}
@Override
...
...
src/main/java/run/halo/app/security/filter/ApiAuthenticationFilter.java
浏览文件 @
22a7311f
...
...
@@ -39,9 +39,7 @@ public class ApiAuthenticationFilter extends AbstractAuthenticationFilter {
}
@Override
protected
void
doFilterInternal
(
HttpServletRequest
request
,
HttpServletResponse
response
,
FilterChain
filterChain
)
throws
ServletException
,
IOException
{
super
.
doFilterInternal
(
request
,
response
,
filterChain
);
protected
void
doAuthenticate
(
HttpServletRequest
request
,
HttpServletResponse
response
,
FilterChain
filterChain
)
throws
ServletException
,
IOException
{
// Get token
String
token
=
getTokenFromRequest
(
request
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录