Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
IEQEFCR
halo
提交
1e15f21e
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,发现更多精彩内容 >>
提交
1e15f21e
编写于
5月 06, 2019
作者:
J
johnniang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor authentication filters
上级
cfc82079
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
57 addition
and
59 deletion
+57
-59
src/main/java/run/halo/app/security/filter/AbstractAuthenticationFilter.java
...alo/app/security/filter/AbstractAuthenticationFilter.java
+24
-1
src/main/java/run/halo/app/security/filter/AdminAuthenticationFilter.java
...n/halo/app/security/filter/AdminAuthenticationFilter.java
+30
-47
src/main/java/run/halo/app/security/filter/ApiAuthenticationFilter.java
...run/halo/app/security/filter/ApiAuthenticationFilter.java
+3
-11
未找到文件。
src/main/java/run/halo/app/security/filter/AbstractAuthenticationFilter.java
浏览文件 @
1e15f21e
...
...
@@ -6,10 +6,17 @@ import org.springframework.util.AntPathMatcher;
import
org.springframework.util.Assert
;
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.handler.AuthenticationFailureHandler
;
import
run.halo.app.security.handler.DefaultAuthenticationFailureHandler
;
import
run.halo.app.service.OptionService
;
import
javax.servlet.FilterChain
;
import
javax.servlet.ServletException
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.util.*
;
/**
...
...
@@ -36,8 +43,12 @@ public abstract class AbstractAuthenticationFilter extends OncePerRequestFilter
private
final
HaloProperties
haloProperties
;
protected
AbstractAuthenticationFilter
(
HaloProperties
haloProperties
)
{
private
final
OptionService
optionService
;
protected
AbstractAuthenticationFilter
(
HaloProperties
haloProperties
,
OptionService
optionService
)
{
this
.
haloProperties
=
haloProperties
;
this
.
optionService
=
optionService
;
antPathMatcher
=
new
AntPathMatcher
();
}
...
...
@@ -154,4 +165,16 @@ public abstract class AbstractAuthenticationFilter extends OncePerRequestFilter
this
.
failureHandler
=
failureHandler
;
}
@Override
protected
void
doFilterInternal
(
HttpServletRequest
request
,
HttpServletResponse
response
,
FilterChain
filterChain
)
throws
ServletException
,
IOException
{
// Check whether the blog is installed or not
Boolean
isInstalled
=
optionService
.
getByPropertyOrDefault
(
PrimaryProperties
.
IS_INSTALLED
,
Boolean
.
class
,
false
);
if
(!
isInstalled
)
{
// If not installed
getFailureHandler
().
onFailure
(
request
,
response
,
new
NotInstallException
(
"The blog has not been initialized yet!"
));
return
;
}
}
}
src/main/java/run/halo/app/security/filter/AdminAuthenticationFilter.java
浏览文件 @
1e15f21e
...
...
@@ -4,14 +4,11 @@ import lombok.extern.slf4j.Slf4j;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.lang.NonNull
;
import
org.springframework.lang.Nullable
;
import
org.springframework.util.Assert
;
import
run.halo.app.cache.StringCacheStore
;
import
run.halo.app.config.properties.HaloProperties
;
import
run.halo.app.exception.AuthenticationException
;
import
run.halo.app.exception.NotInstallException
;
import
run.halo.app.model.entity.User
;
import
run.halo.app.model.properties.PrimaryProperties
;
import
run.halo.app.security.authentication.AuthenticationImpl
;
import
run.halo.app.security.context.SecurityContextHolder
;
import
run.halo.app.security.context.SecurityContextImpl
;
...
...
@@ -58,7 +55,7 @@ public class AdminAuthenticationFilter extends AbstractAuthenticationFilter {
/**
* Admin token param name.
*/
public
final
static
String
ADMIN_TOKEN_QUERY_NAME
=
"admin
T
oken"
;
public
final
static
String
ADMIN_TOKEN_QUERY_NAME
=
"admin
_t
oken"
;
private
final
HaloProperties
haloProperties
;
...
...
@@ -72,7 +69,7 @@ public class AdminAuthenticationFilter extends AbstractAuthenticationFilter {
UserService
userService
,
HaloProperties
haloProperties
,
OptionService
optionService
)
{
super
(
haloProperties
);
super
(
haloProperties
,
optionService
);
this
.
cacheStore
=
cacheStore
;
this
.
userService
=
userService
;
this
.
haloProperties
=
haloProperties
;
...
...
@@ -82,59 +79,45 @@ public class AdminAuthenticationFilter extends AbstractAuthenticationFilter {
@Override
protected
void
doFilterInternal
(
HttpServletRequest
request
,
HttpServletResponse
response
,
FilterChain
filterChain
)
throws
ServletException
,
IOException
{
// Check whether the blog is installed or not
Boolean
isInstalled
=
optionService
.
getByPropertyOrDefault
(
PrimaryProperties
.
IS_INSTALLED
,
Boolean
.
class
,
false
);
super
.
doFilterInternal
(
request
,
response
,
filterChain
);
if
(!
isInstalled
)
{
// If not installed
getFailureHandler
().
onFailure
(
request
,
response
,
new
NotInstallException
(
"The blog has not been initialized yet!"
));
return
;
}
if
(
haloProperties
.
isAuthEnabled
())
{
// Get token from request
String
token
=
getTokenFromRequest
(
request
);
if
(!
haloProperties
.
isAuthEnabled
())
{
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
);
// If authentication disabled
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
.
isNotBlank
(
token
))
{
// Build user detail
UserDetail
userDetail
=
new
UserDetail
(
user
);
// Get user id from cache
Optional
<
Integer
>
optionalUserId
=
cacheStore
.
getAny
(
SecurityUtils
.
buildTokenAccessKey
(
token
),
Integer
.
class
);
if
(!
optionalUserId
.
isPresent
())
{
getFailureHandler
().
onFailure
(
request
,
response
,
new
AuthenticationException
(
"The token has been expired or not exist"
).
setErrorData
(
token
));
return
;
// Set security
SecurityContextHolder
.
setContext
(
new
SecurityContextImpl
(
new
AuthenticationImpl
(
userDetail
)));
}
// Get the user
User
user
=
userService
.
getById
(
optionalUserId
.
get
());
// Build user detail
UserDetail
userDetail
=
new
UserDetail
(
user
);
}
else
{
// Set security
SecurityContextHolder
.
setContext
(
new
SecurityContextImpl
(
new
AuthenticationImpl
(
userDetail
)));
filterChain
.
doFilter
(
request
,
response
);
return
;
userService
.
getCurrentUser
().
ifPresent
(
user
->
SecurityContextHolder
.
setContext
(
new
SecurityContextImpl
(
new
AuthenticationImpl
(
new
UserDetail
(
user
)))));
}
if
(
shouldSkipAuthenticateFailure
(
request
))
{
// If should skip this authentication failure
log
.
debug
(
"Skipping authentication failure, url: [{}], method: [{}]"
,
request
.
getServletPath
(),
request
.
getMethod
());
filterChain
.
doFilter
(
request
,
response
);
return
;
}
filterChain
.
doFilter
(
request
,
response
);
getFailureHandler
().
onFailure
(
request
,
response
,
new
AuthenticationException
(
"You have to login before accessing admin api"
));
// Clear context
SecurityContextHolder
.
clearContext
();
}
@Override
...
...
src/main/java/run/halo/app/security/filter/ApiAuthenticationFilter.java
浏览文件 @
1e15f21e
...
...
@@ -8,9 +8,7 @@ import org.springframework.util.Assert;
import
run.halo.app.config.properties.HaloProperties
;
import
run.halo.app.exception.AuthenticationException
;
import
run.halo.app.exception.ForbiddenException
;
import
run.halo.app.exception.NotInstallException
;
import
run.halo.app.model.properties.OtherProperties
;
import
run.halo.app.model.properties.PrimaryProperties
;
import
run.halo.app.service.OptionService
;
import
javax.servlet.FilterChain
;
...
...
@@ -30,26 +28,20 @@ public class ApiAuthenticationFilter extends AbstractAuthenticationFilter {
public
final
static
String
API_TOKEN_HEADER_NAME
=
"API-"
+
HttpHeaders
.
AUTHORIZATION
;
public
final
static
String
API_TOKEN_QUERY_NAME
=
"api
T
oken"
;
public
final
static
String
API_TOKEN_QUERY_NAME
=
"api
_t
oken"
;
private
final
OptionService
optionService
;
public
ApiAuthenticationFilter
(
HaloProperties
haloProperties
,
OptionService
optionService
)
{
super
(
haloProperties
);
super
(
haloProperties
,
optionService
);
this
.
optionService
=
optionService
;
}
@Override
protected
void
doFilterInternal
(
HttpServletRequest
request
,
HttpServletResponse
response
,
FilterChain
filterChain
)
throws
ServletException
,
IOException
{
// Check whether the blog is installed or not
Boolean
isInstalled
=
optionService
.
getByPropertyOrDefault
(
PrimaryProperties
.
IS_INSTALLED
,
Boolean
.
class
,
false
);
if
(!
isInstalled
)
{
// If not installed
getFailureHandler
().
onFailure
(
request
,
response
,
new
NotInstallException
(
"The blog has not been initialized yet!"
));
return
;
}
super
.
doFilterInternal
(
request
,
response
,
filterChain
);
// Get token
String
token
=
getTokenFromRequest
(
request
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录