Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
yubinCloud
fairy-wiki
提交
971e7398
F
fairy-wiki
项目概览
yubinCloud
/
fairy-wiki
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
fairy-wiki
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
971e7398
编写于
4月 27, 2021
作者:
yubinCloud
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
9-11 前后端加入请求拦截用于验证 token
上级
c601990a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
93 addition
and
1 deletion
+93
-1
src/main/java/io/github/yubincloud/fairywiki/config/SpringMvcConfig.java
...o/github/yubincloud/fairywiki/config/SpringMvcConfig.java
+16
-1
src/main/java/io/github/yubincloud/fairywiki/interceptor/LoginInterceptor.java
...ub/yubincloud/fairywiki/interceptor/LoginInterceptor.java
+71
-0
web/src/main.ts
web/src/main.ts
+6
-0
未找到文件。
src/main/java/io/github/yubincloud/fairywiki/config/SpringMvcConfig.java
浏览文件 @
971e7398
package
io.github.yubincloud.fairywiki.config
;
import
io.github.yubincloud.fairywiki.interceptor.LoginInterceptor
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.servlet.config.annotation.InterceptorRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
javax.annotation.Resource
;
@Configuration
public
class
SpringMvcConfig
implements
WebMvcConfigurer
{
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
@Resource
LoginInterceptor
loginInterceptor
;
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
registry
.
addInterceptor
(
loginInterceptor
)
.
addPathPatterns
(
"/**"
)
.
excludePathPatterns
(
"/test/**"
,
"/redis/**"
,
"/user/login"
,
"/category/all"
,
"/ebook/query"
,
"/doc/all/**"
,
"/doc/read-content/**"
);
}
}
src/main/java/io/github/yubincloud/fairywiki/interceptor/LoginInterceptor.java
0 → 100644
浏览文件 @
971e7398
package
io.github.yubincloud.fairywiki.interceptor
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.servlet.HandlerInterceptor
;
import
org.springframework.web.servlet.ModelAndView
;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
/**
* 拦截器:Spring框架特有的,常用于登录校验,权限校验,请求日志打印
*/
@Component
public
class
LoginInterceptor
implements
HandlerInterceptor
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
LoginInterceptor
.
class
);
@Resource
private
RedisTemplate
redisTemplate
;
@Override
public
boolean
preHandle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
)
throws
Exception
{
// 打印请求信息
LOG
.
info
(
"------------- LoginInterceptor 开始 -------------"
);
long
startTime
=
System
.
currentTimeMillis
();
request
.
setAttribute
(
"requestStartTime"
,
startTime
);
// OPTIONS请求不做校验,
// 前后端分离的架构, 前端会发一个OPTIONS请求先做预检, 对预检请求不做校验
if
(
request
.
getMethod
().
toUpperCase
().
equals
(
"OPTIONS"
)){
return
true
;
}
String
path
=
request
.
getRequestURL
().
toString
();
LOG
.
info
(
"接口登录拦截:,path:{}"
,
path
);
//获取header的token参数
String
token
=
request
.
getHeader
(
"token"
);
LOG
.
info
(
"登录校验开始,token:{}"
,
token
);
if
(
token
==
null
||
token
.
isEmpty
())
{
LOG
.
info
(
"token为空,请求被拦截"
);
response
.
setStatus
(
HttpStatus
.
UNAUTHORIZED
.
value
());
return
false
;
}
Object
object
=
redisTemplate
.
opsForValue
().
get
(
token
);
if
(
object
==
null
)
{
LOG
.
warn
(
"token无效,请求被拦截"
);
response
.
setStatus
(
HttpStatus
.
UNAUTHORIZED
.
value
());
return
false
;
}
else
{
LOG
.
info
(
"已登录:{}"
,
object
);
return
true
;
}
}
@Override
public
void
postHandle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
,
ModelAndView
modelAndView
)
throws
Exception
{
long
startTime
=
(
Long
)
request
.
getAttribute
(
"requestStartTime"
);
LOG
.
info
(
"------------- LoginInterceptor 结束 耗时:{} ms -------------"
,
System
.
currentTimeMillis
()
-
startTime
);
}
@Override
public
void
afterCompletion
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
,
Exception
ex
)
throws
Exception
{
// LOG.info("LogInterceptor 结束");
}
}
web/src/main.ts
浏览文件 @
971e7398
...
...
@@ -6,6 +6,7 @@ import Antd from 'ant-design-vue'
import
'
ant-design-vue/dist/antd.css
'
import
*
as
Icons
from
'
@ant-design/icons-vue
'
import
axios
from
'
axios
'
;
import
{
Tool
}
from
"
@/util/tool
"
;
axios
.
defaults
.
baseURL
=
process
.
env
.
VUE_APP_SERVER
;
// 在使用 axios 发送请求时全局的base域
...
...
@@ -14,6 +15,11 @@ axios.defaults.baseURL = process.env.VUE_APP_SERVER; // 在使用 axios 发送
*/
axios
.
interceptors
.
request
.
use
(
function
(
reqConf
)
{
console
.
log
(
'
请求参数:
'
,
reqConf
);
const
token
=
store
.
state
.
localUser
.
token
;
if
(
Tool
.
isNotEmpty
(
token
))
{
reqConf
.
headers
.
token
=
token
;
console
.
log
(
"
请求headers增加token:
"
,
token
);
}
return
reqConf
;
},
error
=>
{
return
Promise
.
reject
(
error
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录