Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qq_45598856
white-jotter
提交
09af1804
W
white-jotter
项目概览
qq_45598856
/
white-jotter
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
white-jotter
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
09af1804
编写于
10月 19, 2019
作者:
E
Evan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: can't redirect after logout
上级
a74ac25c
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
61 addition
and
61 deletion
+61
-61
wj-vue/src/components/Register.vue
wj-vue/src/components/Register.vue
+1
-1
wj-vue/src/components/home/Carousel.vue
wj-vue/src/components/home/Carousel.vue
+4
-1
wj-vue/src/components/library/Books.vue
wj-vue/src/components/library/Books.vue
+1
-1
wj-vue/src/main.js
wj-vue/src/main.js
+11
-6
wj-vue/src/store/index.js
wj-vue/src/store/index.js
+5
-2
wj/pom.xml
wj/pom.xml
+6
-6
wj/src/main/java/com/gm/wj/config/MyWebConfigurer.java
wj/src/main/java/com/gm/wj/config/MyWebConfigurer.java
+12
-6
wj/src/main/java/com/gm/wj/controller/LoginController.java
wj/src/main/java/com/gm/wj/controller/LoginController.java
+10
-3
wj/src/main/java/com/gm/wj/interceptor/LoginInterceptor.java
wj/src/main/java/com/gm/wj/interceptor/LoginInterceptor.java
+11
-35
未找到文件。
wj-vue/src/components/Register.vue
浏览文件 @
09af1804
<
template
>
<body
id=
"paper"
>
<el-form
:rules=
"rules"
class=
"login-container"
label-position=
"left"
<el-form
class=
"login-container"
label-position=
"left"
label-width=
"0px"
v-loading=
"loading"
>
<h3
class=
"login_title"
>
用户注册
</h3>
<el-form-item>
...
...
wj-vue/src/components/home/Carousel.vue
浏览文件 @
09af1804
<
template
>
<el-card
class=
"card-carousel"
>
<el-carousel
class=
"push"
:interval=
"4000"
arrow=
"always"
>
<el-carousel-item
v-for=
"item in items"
:key=
"item"
>
<el-carousel-item
v-for=
"item in items"
:key=
"item
.id
"
>
<a
:href=
item.link
target=
"_blank"
>
<img
:src=
item.img
alt=
""
class=
"carousel_img"
>
</a>
...
...
@@ -53,15 +53,18 @@
data
:
function
()
{
return
{
items
:
[{
id
:
1
,
title
:
'
How2J.cn - Java 全栈学习网站
'
,
img
:
'
../../../static/img/carousel/how2j.png
'
,
link
:
'
http://how2j.cn?p=50613
'
},
{
id
:
2
,
title
:
'
Vue.js - 渐进式 JavaScript 框架
'
,
img
:
'
../../../static/img/carousel/vue.png
'
,
link
:
'
https://cn.vuejs.org/
'
},
{
id
:
3
,
title
:
'
element-ui - 网站快速成型工具
'
,
img
:
'
../../../static/img/carousel/element.png
'
,
link
:
'
http://element-cn.eleme.io/#/zh-CN
'
...
...
wj-vue/src/components/library/Books.vue
浏览文件 @
09af1804
...
...
@@ -61,7 +61,7 @@
methods
:
{
loadBooks
()
{
var
_this
=
this
this
.
$axios
.
get
(
'
/books
'
).
then
(
resp
=>
{
this
.
$axios
.
get
(
'
/books
'
,
{
withCredentials
:
true
}
).
then
(
resp
=>
{
if
(
resp
&&
resp
.
status
===
200
)
{
_this
.
books
=
resp
.
data
}
...
...
wj-vue/src/main.js
浏览文件 @
09af1804
...
...
@@ -11,6 +11,8 @@ import store from './store'
var
axios
=
require
(
'
axios
'
)
axios
.
defaults
.
baseURL
=
'
http://localhost:8443/api
'
// 使请求带上凭证信息
// axios.defaults.withCredentials = true
Vue
.
prototype
.
$axios
=
axios
Vue
.
config
.
productionTip
=
false
...
...
@@ -25,8 +27,7 @@ Vue.use(mavonEditor)
router
.
beforeEach
((
to
,
from
,
next
)
=>
{
if
(
to
.
meta
.
requireAuth
)
{
if
(
store
.
state
.
user
.
token
)
{
console
.
log
(
store
.
state
.
user
.
token
)
axios
.
post
(
'
/authentication
'
)
// axios.post('/authentication')
next
()
}
else
{
next
({
...
...
@@ -39,20 +40,24 @@ router.beforeEach((to, from, next) => {
}
}
)
// http request 拦截器
// http request拦截器,会先于state的更新执行,以保证发送logout请求时也带上正确的token
axios
.
interceptors
.
request
.
use
(
config
=>
{
// 输出当前状态下的 token
// console.log(store.state.user.token)
if
(
store
.
state
.
user
.
token
)
{
// 判断是否存在token,如果存在的话,则每个http header都加上token
// 判断
当前
是否存在token,如果存在的话,则每个http header都加上token
// config.headers.Token = `token ${JSON.stringify(store.state.user.token)}`
config
.
headers
.
Token
=
JSON
.
stringify
(
store
.
state
.
user
.
token
)
}
else
{
config
.
headers
.
Token
=
null
}
return
config
},
err
=>
{
return
Promise
.
reject
(
err
)
})
}
)
// http response 拦截器
axios
.
interceptors
.
response
.
use
(
...
...
wj-vue/src/store/index.js
浏览文件 @
09af1804
...
...
@@ -21,11 +21,14 @@ export default new Vuex.Store({
login
(
state
,
data
)
{
state
.
user
=
data
window
.
localStorage
.
setItem
(
'
user
'
,
JSON
.
stringify
(
data
))
window
.
document
.
cookie
=
'
have something
'
},
logout
(
state
)
{
// 注意不能用 null 清除,否则将无法判断 user 里具体的内容
state
.
user
=
[]
window
.
localStorage
.
removeItem
(
'
user
'
)
state
.
user
=
null
state
.
routes
=
[]
window
.
document
.
cookie
=
null
//
state.routes = []
}
},
actions
:
{
...
...
wj/pom.xml
浏览文件 @
09af1804
...
...
@@ -13,6 +13,12 @@
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- shiro -->
<dependency>
<groupId>
org.apache.shiro
</groupId>
<artifactId>
shiro-spring
</artifactId>
<version>
1.4.1
</version>
</dependency>
<!-- springboot web -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
...
...
@@ -101,12 +107,6 @@
<artifactId>
commons-lang
</artifactId>
<version>
2.6
</version>
</dependency>
<!-- shiro -->
<dependency>
<groupId>
org.apache.shiro
</groupId>
<artifactId>
shiro-spring
</artifactId>
<version>
1.3.2
</version>
</dependency>
<!-- hsqldb -->
<dependency>
<groupId>
org.hsqldb
</groupId>
...
...
wj/src/main/java/com/gm/wj/config/MyWebConfigurer.java
浏览文件 @
09af1804
...
...
@@ -16,17 +16,23 @@ public class MyWebConfigurer implements WebMvcConfigurer {
}
@Override
public
void
addInterceptors
(
InterceptorRegistry
registry
){
registry
.
addInterceptor
(
getLoginIntercepter
()).
addPathPatterns
(
"/**"
).
excludePathPatterns
(
"/index.html"
);
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
registry
.
addInterceptor
(
getLoginIntercepter
())
.
addPathPatterns
(
"/**"
)
.
excludePathPatterns
(
"/index.html"
)
.
excludePathPatterns
(
"/api/login"
)
.
excludePathPatterns
(
"/api/logout"
);
}
@Override
public
void
addCorsMappings
(
CorsRegistry
registry
)
{
//所有请求都允许跨域
//所有请求都允许跨域
,使用这种配置方法就不能在 interceptor 中再配置 header 了
registry
.
addMapping
(
"/**"
)
.
allowedOrigins
(
"*"
)
.
allowedMethods
(
"*"
)
.
allowedHeaders
(
"*"
);
.
allowedOrigins
(
"http://localhost:8080"
)
.
allowedMethods
(
"POST"
,
"GET"
,
"PUT"
,
"OPTIONS"
,
"DELETE"
)
.
allowCredentials
(
true
)
.
allowedHeaders
(
"*"
)
.
maxAge
(
3600
);
}
@Override
...
...
wj/src/main/java/com/gm/wj/controller/LoginController.java
浏览文件 @
09af1804
...
...
@@ -9,6 +9,7 @@ import com.gm.wj.result.Result;
import
com.gm.wj.result.ResultFactory
;
import
com.gm.wj.service.UserService
;
import
com.gm.wj.util.TokenUtil
;
import
org.apache.http.HttpResponse
;
import
org.apache.shiro.SecurityUtils
;
import
org.apache.shiro.authc.AuthenticationException
;
import
org.apache.shiro.authc.UsernamePasswordToken
;
...
...
@@ -91,8 +92,14 @@ public class LoginController {
@ResponseBody
@PostMapping
(
value
=
"api/authentication"
)
public
String
authentication
(
@RequestHeader
(
"Token"
)
String
token
){
// System.out.println(user.getUsername());
return
"authentication success"
;
public
String
authentication
(
@RequestHeader
(
"Token"
)
String
token
,
HttpSession
session
,
HttpResponse
response
){
System
.
out
.
println
(
token
);
System
.
out
.
println
(
session
.
getAttribute
(
"token"
));
if
(
token
==
session
.
getAttribute
(
"token"
))
{
return
"身份认证成功"
;
}
else
{
response
.
setStatusCode
(
401
);
return
"认证失败,请重新登录"
;
}
}
}
wj/src/main/java/com/gm/wj/interceptor/LoginInterceptor.java
浏览文件 @
09af1804
...
...
@@ -12,49 +12,25 @@ import javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpSession
;
public
class
LoginInterceptor
implements
HandlerInterceptor
{
public
class
LoginInterceptor
implements
HandlerInterceptor
{
@Override
public
boolean
preHandle
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
Object
o
)
throws
Exception
{
public
boolean
preHandle
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
Object
o
)
throws
Exception
{
HttpSession
session
=
httpServletRequest
.
getSession
();
String
contextPath
=
session
.
getServletContext
().
getContextPath
();
String
[]
requireAuthPages
=
new
String
[]{
"index"
,
"jotter"
,
"library"
,
"applibrary"
};
String
uri
=
httpServletRequest
.
getRequestURI
();
uri
=
StringUtils
.
remove
(
uri
,
contextPath
+
"/"
);
String
page
=
uri
;
// 使用 shiro,仅对后端拦截有效
if
(
begingWith
(
page
,
requireAuthPages
)){
Subject
subject
=
SecurityUtils
.
getSubject
();
if
(!
subject
.
isAuthenticated
())
{
httpServletResponse
.
sendRedirect
(
"login"
);
return
false
;
}
Subject
subject
=
SecurityUtils
.
getSubject
();
System
.
out
.
println
(
session
.
getId
());
if
(!
subject
.
isAuthenticated
())
{
System
.
out
.
println
(
false
);
// 预处理不能重定向
// httpServletResponse.sendRedirect("login");
// 明天在再吧。。。先放水
return
true
;
}
return
true
;
}
private
boolean
begingWith
(
String
page
,
String
[]
requiredAuthPages
)
{
boolean
result
=
false
;
for
(
String
requiredAuthPage
:
requiredAuthPages
)
{
if
(
StringUtils
.
startsWith
(
page
,
requiredAuthPage
))
{
result
=
true
;
break
;
}
}
return
result
;
}
@Override
public
void
postHandle
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
Object
o
,
ModelAndView
modelAndView
)
throws
Exception
{
public
void
postHandle
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
Object
o
,
ModelAndView
modelAndView
)
throws
Exception
{
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录