Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nouiWithSpringMVC
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gechengyang
nouiWithSpringMVC
Commits
e8cde8e0
Commit
e8cde8e0
authored
Sep 23, 2020
by
WeiCong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复后台用户空
parent
541b9147
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
51 deletions
+48
-51
TokenInterceptor.java
.../java/org/sss/presentation/noui/jwt/TokenInterceptor.java
+48
-51
No files found.
src/main/java/org/sss/presentation/noui/jwt/TokenInterceptor.java
View file @
e8cde8e0
package
org
.
sss
.
presentation
.
noui
.
jwt
;
import
java.io.PrintWriter
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
com.google.gson.Gson
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.servlet.HandlerInterceptor
;
import
org.springframework.web.servlet.ModelAndView
;
...
...
@@ -20,7 +14,11 @@ import org.sss.presentation.noui.util.NumericUtil;
import
org.sss.presentation.noui.util.RedisUtil
;
import
org.sss.presentation.noui.util.StringUtil
;
import
com.google.gson.Gson
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.PrintWriter
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
public
class
TokenInterceptor
implements
HandlerInterceptor
{
...
...
@@ -29,8 +27,44 @@ public class TokenInterceptor implements HandlerInterceptor {
@Autowired
private
NoUiVersion
noUiVersion
;
public
static
int
compareAndSet
(
String
token
,
int
cnt
,
int
max_curr_cnt
)
{
synchronized
(
CounterMap
)
{
int
count
=
getCount
(
token
);
if
(
count
>=
max_curr_cnt
)
return
-
1
;
count
+=
cnt
;
if
(
count
<=
0
)
CounterMap
.
remove
(
token
);
else
CounterMap
.
put
(
token
,
count
);
return
count
;
}
}
public
static
int
addCount
(
String
token
,
int
cnt
)
{
synchronized
(
CounterMap
)
{
int
count
=
getCount
(
token
);
count
+=
cnt
;
if
(
count
<=
0
)
CounterMap
.
remove
(
token
);
else
CounterMap
.
put
(
token
,
count
);
return
count
;
}
}
public
static
int
getCount
(
String
token
)
{
Integer
count
=
CounterMap
.
get
(
token
);
if
(
count
==
null
)
return
0
;
return
count
;
}
public
void
afterCompletion
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
,
Exception
arg3
)
throws
Exception
{
addCount
(
NoUiUtils
.
getToken
(),-
1
);
//计算器减1
String
token
=
request
.
getHeader
(
"token"
);
if
(!
StringUtil
.
isEmpty
(
token
)
&&
!
token
.
startsWith
(
Constants
.
BACKGROUND_FLAG
)){
addCount
(
token
,
-
1
);
//计算器减1
}
NoUiUtils
.
clearLoginInfo
();
}
...
...
@@ -77,7 +111,7 @@ public class TokenInterceptor implements HandlerInterceptor {
return
false
;
}
//验证token是否一致
if
(!
token
.
equals
(
redisLoginInfo
.
getToken
()))
{
if
(!
token
.
equals
(
redisLoginInfo
.
getToken
()))
{
Result
rt
=
new
Result
(
ErrorCodes
.
LOGIN_TOKEN_CHECKERROR
,
"token失效,该用户被强迫下线"
,
null
,
noUiVersion
.
getVersion
());
responseMessage
(
response
,
response
.
getWriter
(),
rt
);
return
false
;
...
...
@@ -85,20 +119,19 @@ public class TokenInterceptor implements HandlerInterceptor {
// 验证登录时间
if
(
System
.
currentTimeMillis
()
>
redisLoginInfo
.
getExpiredTime
())
{
Result
rt
=
new
Result
(
ErrorCodes
.
LOGIN_TIMEOUT
,
"会话超时,请重新登录。超时时间戳:"
+
redisLoginInfo
.
getExpiredTime
()
+
",当前时间戳:"
+
System
.
currentTimeMillis
(),
null
,
noUiVersion
.
getVersion
());
Result
rt
=
new
Result
(
ErrorCodes
.
LOGIN_TIMEOUT
,
"会话超时,请重新登录。超时时间戳:"
+
redisLoginInfo
.
getExpiredTime
()
+
",当前时间戳:"
+
System
.
currentTimeMillis
(),
null
,
noUiVersion
.
getVersion
());
responseMessage
(
response
,
response
.
getWriter
(),
rt
);
return
false
;
}
//超过最大并发数限制
if
(
compareAndSet
(
noUiRequest
.
getToken
(),
1
,
noUiVersion
.
getCurr_max_num
())
<
0
)
{
if
(
compareAndSet
(
noUiRequest
.
getToken
(),
1
,
noUiVersion
.
getCurr_max_num
())
<
0
)
{
Result
rt
=
new
Result
(
ErrorCodes
.
GT_MAX_CURR_NUM
,
"超过单个会话并发数"
,
null
,
noUiVersion
.
getVersion
());
responseMessage
(
response
,
response
.
getWriter
(),
rt
);
return
false
;
}
// 重新刷入登陆时间
RedisLoginInfo
nweRedisLoginInfo
=
new
RedisLoginInfo
(
userId
,
token
,
NumericUtil
.
sessionTimeOut
(),
redisLoginInfo
.
getSysmod
(),
noUiRequest
.
getTerminalType
());
RedisLoginInfo
nweRedisLoginInfo
=
new
RedisLoginInfo
(
userId
,
token
,
NumericUtil
.
sessionTimeOut
(),
redisLoginInfo
.
getSysmod
(),
noUiRequest
.
getTerminalType
());
RedisUtil
.
set
(
Constants
.
SESSION
+
"."
+
userId
+
"."
+
terminalType
,
nweRedisLoginInfo
);
NoUiUtils
.
setLoginInfo
(
nweRedisLoginInfo
);
...
...
@@ -114,39 +147,4 @@ public class TokenInterceptor implements HandlerInterceptor {
out
.
flush
();
out
.
close
();
}
public
static
int
compareAndSet
(
String
token
,
int
cnt
,
int
max_curr_cnt
)
{
synchronized
(
CounterMap
)
{
int
count
=
getCount
(
token
);
if
(
count
>=
max_curr_cnt
)
return
-
1
;
count
+=
cnt
;
if
(
count
<=
0
)
CounterMap
.
remove
(
token
);
else
CounterMap
.
put
(
token
,
count
);
return
count
;
}
}
public
static
int
addCount
(
String
token
,
int
cnt
)
{
synchronized
(
CounterMap
)
{
int
count
=
getCount
(
token
);
count
+=
cnt
;
if
(
count
<=
0
)
CounterMap
.
remove
(
token
);
else
CounterMap
.
put
(
token
,
count
);
return
count
;
}
}
public
static
int
getCount
(
String
token
)
{
Integer
count
=
CounterMap
.
get
(
token
);
if
(
count
==
null
)
return
0
;
return
count
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment