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
8f3721d0
Commit
8f3721d0
authored
Sep 23, 2020
by
fukai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
提交访问计数器
parent
c719ffce
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
2 deletions
+59
-2
ErrorCodes.java
...va/org/sss/presentation/noui/api/response/ErrorCodes.java
+2
-0
NoUiVersion.java
...a/org/sss/presentation/noui/api/response/NoUiVersion.java
+10
-0
TokenInterceptor.java
.../java/org/sss/presentation/noui/jwt/TokenInterceptor.java
+47
-2
No files found.
src/main/java/org/sss/presentation/noui/api/response/ErrorCodes.java
View file @
8f3721d0
...
...
@@ -29,6 +29,8 @@ public class ErrorCodes {
public
static
final
String
UNKNOEW_TRANS
=
"R0017"
;
public
static
final
String
FORBIDDEN_TRANS
=
"R0018"
;
public
static
final
String
GT_MAX_CURR_NUM
=
"R9990"
;
public
static
final
String
INTERRUPTED_ERROR
=
"R9997"
;
public
static
final
String
REDIS_CONNECTION_ERROR
=
"R9998"
;
public
static
final
String
ERROR
=
"R9999"
;
...
...
src/main/java/org/sss/presentation/noui/api/response/NoUiVersion.java
View file @
8f3721d0
...
...
@@ -4,6 +4,8 @@ public class NoUiVersion {
private
String
version
=
"1.0.0"
;
private
int
curr_max_num
=
3
;
//最大并发数
private
String
rootFilePath
;
public
String
getRootFilePath
()
{
...
...
@@ -21,4 +23,12 @@ public class NoUiVersion {
public
void
setVersion
(
String
version
)
{
this
.
version
=
version
;
}
public
int
getCurr_max_num
()
{
return
curr_max_num
;
}
public
void
setCurr_max_num
(
int
curr_max_num
)
{
this
.
curr_max_num
=
curr_max_num
;
}
}
src/main/java/org/sss/presentation/noui/jwt/TokenInterceptor.java
View file @
8f3721d0
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
;
...
...
@@ -22,10 +24,13 @@ import com.google.gson.Gson;
public
class
TokenInterceptor
implements
HandlerInterceptor
{
private
static
Map
<
String
,
Integer
>
CounterMap
=
new
ConcurrentHashMap
<>();
//计数map
@Autowired
private
NoUiVersion
noUiVersion
;
public
void
afterCompletion
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
,
Exception
arg3
)
throws
Exception
{
addCount
(
NoUiUtils
.
getToken
(),-
1
);
//计算器减1
NoUiUtils
.
clearLoginInfo
();
}
...
...
@@ -58,7 +63,6 @@ public class TokenInterceptor implements HandlerInterceptor {
return
true
;
}
JwtLogin
login
=
JWT
.
unsign
(
token
,
JwtLogin
.
class
);
if
(
login
==
null
||
(!
userId
.
equals
((
login
.
getUserId
()))))
{
Result
rt
=
new
Result
(
ErrorCodes
.
LOGIN_TOKEN_CHECKERROR
,
"用户token或ID验证不通过"
,
null
,
noUiVersion
.
getVersion
());
...
...
@@ -86,7 +90,13 @@ public class TokenInterceptor implements HandlerInterceptor {
responseMessage
(
response
,
response
.
getWriter
(),
rt
);
return
false
;
}
//超过最大并发数限制
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
());
RedisUtil
.
set
(
Constants
.
SESSION
+
"."
+
userId
+
"."
+
terminalType
,
nweRedisLoginInfo
);
...
...
@@ -105,4 +115,38 @@ public class TokenInterceptor implements HandlerInterceptor {
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