Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
ioc_sixiang_license
Project
Project
Details
Activity
Releases
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
zengtianlai3
ioc_sixiang_license
Commits
7371e857
Commit
7371e857
authored
Aug 05, 2022
by
zengtianlai3
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
解决敏感信息泄漏-JWT认证头
parent
104db084
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
20 deletions
+52
-20
LoginController.java
.../java/iot/sixiang/license/controller/LoginController.java
+2
-0
JwtFilter.java
license/src/main/java/iot/sixiang/license/jwt/JwtFilter.java
+7
-4
JwtUtil.java
license/src/main/java/iot/sixiang/license/jwt/JwtUtil.java
+8
-2
UserUtils.java
license/src/main/java/iot/sixiang/license/jwt/UserUtils.java
+35
-14
No files found.
license/src/main/java/iot/sixiang/license/controller/LoginController.java
View file @
7371e857
...
@@ -55,6 +55,7 @@ public class LoginController {
...
@@ -55,6 +55,7 @@ public class LoginController {
LoginVo
loginVo
=
new
LoginVo
();
LoginVo
loginVo
=
new
LoginVo
();
loginVo
.
setAuthorization
(
token
);
loginVo
.
setAuthorization
(
token
);
UserUtils
.
setToken
(
dbUser
.
getUserId
(),
token
);
UserUtils
.
setToken
(
dbUser
.
getUserId
(),
token
);
UserUtils
.
setTokenExp
(
dbUser
.
getUserId
(),
JwtUtil
.
getTokenExp
());
return
ResResult
.
success
().
goRecord
(
loginVo
);
return
ResResult
.
success
().
goRecord
(
loginVo
);
}
}
}
}
...
@@ -67,6 +68,7 @@ public class LoginController {
...
@@ -67,6 +68,7 @@ public class LoginController {
public
BaseResult
logout
()
{
public
BaseResult
logout
()
{
String
loginUserId
=
UserUtils
.
getLoginUserId
();
String
loginUserId
=
UserUtils
.
getLoginUserId
();
UserUtils
.
removeToken
(
loginUserId
);
UserUtils
.
removeToken
(
loginUserId
);
UserUtils
.
removeTokenExp
(
loginUserId
);
return
BaseResult
.
success
();
return
BaseResult
.
success
();
}
}
...
...
license/src/main/java/iot/sixiang/license/jwt/JwtFilter.java
View file @
7371e857
...
@@ -11,6 +11,7 @@ import javax.servlet.annotation.WebFilter;
...
@@ -11,6 +11,7 @@ import javax.servlet.annotation.WebFilter;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.util.Date
;
import
java.util.Map
;
import
java.util.Map
;
@Slf4j
@Slf4j
...
@@ -58,13 +59,11 @@ public class JwtFilter implements Filter {
...
@@ -58,13 +59,11 @@ public class JwtFilter implements Filter {
if
(
StringUtils
.
isEmpty
(
token
))
{
if
(
StringUtils
.
isEmpty
(
token
))
{
request
.
setAttribute
(
"msg"
,
"认证信息不能为空"
);
request
.
setAttribute
(
"msg"
,
"认证信息不能为空"
);
request
.
getRequestDispatcher
(
"/fail"
).
forward
(
request
,
response
);
request
.
getRequestDispatcher
(
"/fail"
).
forward
(
request
,
response
);
return
;
}
else
{
}
else
{
DecodedJWT
jwt
=
JwtUtil
.
verifyToken
(
token
);
DecodedJWT
jwt
=
JwtUtil
.
verifyToken
(
token
);
if
(
jwt
==
null
)
{
if
(
jwt
==
null
)
{
request
.
setAttribute
(
"msg"
,
"认证信息非法"
);
request
.
setAttribute
(
"msg"
,
"认证信息非法"
);
request
.
getRequestDispatcher
(
"/fail"
).
forward
(
request
,
response
);
request
.
getRequestDispatcher
(
"/fail"
).
forward
(
request
,
response
);
return
;
}
else
{
}
else
{
Map
<
String
,
Claim
>
userData
=
jwt
.
getClaims
();
Map
<
String
,
Claim
>
userData
=
jwt
.
getClaims
();
if
(
userData
==
null
)
{
if
(
userData
==
null
)
{
...
@@ -74,13 +73,17 @@ public class JwtFilter implements Filter {
...
@@ -74,13 +73,17 @@ public class JwtFilter implements Filter {
}
}
String
userId
=
userData
.
get
(
"userId"
).
asString
();
String
userId
=
userData
.
get
(
"userId"
).
asString
();
String
userName
=
userData
.
get
(
"userName"
).
asString
();
String
userName
=
userData
.
get
(
"userName"
).
asString
();
String
password
=
userData
.
get
(
"password"
).
asString
()
;
String
password
=
""
;
LoginUser
loginUser
=
new
LoginUser
(
userId
,
userName
,
password
);
LoginUser
loginUser
=
new
LoginUser
(
userId
,
userName
,
password
);
if
(
token
.
equals
(
UserUtils
.
getToken
(
userId
)))
{
Date
curDate
=
new
Date
();
Date
tokenExp
=
UserUtils
.
getTokenExp
(
userId
);
if
(
token
.
equals
(
UserUtils
.
getToken
(
userId
))
&&
curDate
.
before
(
tokenExp
))
{
UserUtils
.
setLoginUser
(
loginUser
);
UserUtils
.
setLoginUser
(
loginUser
);
UserUtils
.
setUri
(
uri
);
UserUtils
.
setUri
(
uri
);
filterChain
.
doFilter
(
request
,
response
);
filterChain
.
doFilter
(
request
,
response
);
}
else
{
}
else
{
UserUtils
.
removeToken
(
userId
);
UserUtils
.
removeTokenExp
(
userId
);
request
.
setAttribute
(
"msg"
,
"token已失效"
);
request
.
setAttribute
(
"msg"
,
"token已失效"
);
request
.
getRequestDispatcher
(
"/fail"
).
forward
(
request
,
response
);
request
.
getRequestDispatcher
(
"/fail"
).
forward
(
request
,
response
);
}
}
...
...
license/src/main/java/iot/sixiang/license/jwt/JwtUtil.java
View file @
7371e857
...
@@ -39,8 +39,8 @@ public class JwtUtil {
...
@@ -39,8 +39,8 @@ public class JwtUtil {
//可以把数据存在claim中
//可以把数据存在claim中
.
withClaim
(
"userId"
,
user
.
getUserId
())
.
withClaim
(
"userId"
,
user
.
getUserId
())
.
withClaim
(
"userName"
,
user
.
getUserName
())
.
withClaim
(
"userName"
,
user
.
getUserName
())
.
withClaim
(
"password"
,
user
.
getPassword
())
//
.withClaim("password", user.getPassword())
.
withExpiresAt
(
expireDate
)
//超时设置,设置过期的日期
//
.withExpiresAt(expireDate) //超时设置,设置过期的日期
.
withIssuedAt
(
new
Date
())
//签发时间
.
withIssuedAt
(
new
Date
())
//签发时间
.
sign
(
Algorithm
.
HMAC256
(
SECRET
));
//SECRET加密
.
sign
(
Algorithm
.
HMAC256
(
SECRET
));
//SECRET加密
return
token
;
return
token
;
...
@@ -76,4 +76,10 @@ public class JwtUtil {
...
@@ -76,4 +76,10 @@ public class JwtUtil {
}
}
return
jwt
;
return
jwt
;
}
}
public
static
Date
getTokenExp
()
{
//过期时间
Date
expireDate
=
new
Date
(
System
.
currentTimeMillis
()
+
EXPIRATION
*
1000
);
return
expireDate
;
}
}
}
license/src/main/java/iot/sixiang/license/jwt/UserUtils.java
View file @
7371e857
package
iot
.
sixiang
.
license
.
jwt
;
package
iot
.
sixiang
.
license
.
jwt
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -10,6 +11,7 @@ import java.util.Map;
...
@@ -10,6 +11,7 @@ import java.util.Map;
public
abstract
class
UserUtils
{
public
abstract
class
UserUtils
{
static
Map
<
String
,
String
>
tokenMap
=
new
HashMap
<>();
static
Map
<
String
,
String
>
tokenMap
=
new
HashMap
<>();
static
Map
<
String
,
Date
>
tokenExpTimeMap
=
new
HashMap
<>();
//线程变量,存放user实体类信息,即使是静态的与其他线程也是隔离的
//线程变量,存放user实体类信息,即使是静态的与其他线程也是隔离的
private
static
ThreadLocal
<
LoginUser
>
userThreadLocal
=
new
ThreadLocal
<>();
private
static
ThreadLocal
<
LoginUser
>
userThreadLocal
=
new
ThreadLocal
<>();
//线程变量,存放uri,即使是静态的与其他线程也是隔离的
//线程变量,存放uri,即使是静态的与其他线程也是隔离的
...
@@ -40,6 +42,30 @@ public abstract class UserUtils {
...
@@ -40,6 +42,30 @@ public abstract class UserUtils {
userThreadLocal
.
set
(
user
);
userThreadLocal
.
set
(
user
);
}
}
//清除userThreadLocal线程变量
public
static
void
removeUser
()
{
userThreadLocal
.
remove
();
}
//为当前的线程变量赋值上uri信息
public
static
void
setUri
(
String
uri
)
{
uriThreadLocal
.
set
(
uri
);
}
/**
* 获取当前访问方法的uri
* @return
*/
public
static
String
getUri
()
{
String
uri
=
uriThreadLocal
.
get
();
return
uri
;
}
//清除uriThreadLocal线程变量
public
static
void
removeUri
()
{
uriThreadLocal
.
remove
();
}
//为当前的线程变量赋值上token信息
//为当前的线程变量赋值上token信息
public
static
void
setToken
(
String
uId
,
String
token
)
{
public
static
void
setToken
(
String
uId
,
String
token
)
{
tokenMap
.
put
(
uId
,
token
);
tokenMap
.
put
(
uId
,
token
);
...
@@ -61,27 +87,22 @@ public abstract class UserUtils {
...
@@ -61,27 +87,22 @@ public abstract class UserUtils {
tokenMap
.
remove
(
uId
);
tokenMap
.
remove
(
uId
);
}
}
//清除userThreadLocal线程变量
public
static
void
removeUser
()
{
userThreadLocal
.
remove
();
}
//为当前的线程变量赋值上
uri
信息
//为当前的线程变量赋值上
token
信息
public
static
void
set
Uri
(
String
uri
)
{
public
static
void
set
TokenExp
(
String
uId
,
Date
tokenExpTime
)
{
uriThreadLocal
.
set
(
uri
);
tokenExpTimeMap
.
put
(
uId
,
tokenExpTime
);
}
}
/**
/**
* 获取当前访问方法的
uri
* 获取当前访问方法的
token
* @return
* @return
*/
*/
public
static
String
getUri
()
{
public
static
Date
getTokenExp
(
String
uId
)
{
String
uri
=
uriThreadLocal
.
get
();
return
tokenExpTimeMap
.
get
(
uId
);
return
uri
;
}
}
//清除
uri
ThreadLocal线程变量
//清除
token
ThreadLocal线程变量
public
static
void
remove
Uri
(
)
{
public
static
void
remove
TokenExp
(
String
uId
)
{
uriThreadLocal
.
remove
(
);
tokenExpTimeMap
.
remove
(
uId
);
}
}
}
}
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