Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
soss
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
hooloo
ms
soss
Commits
0e6988c6
Commit
0e6988c6
authored
Apr 17, 2023
by
weijiguang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加错误日志
parent
c222f237
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
50 deletions
+31
-50
soss-framework/src/main/java/com/soss/framework/web/service/TokenService.java
+31
-50
No files found.
soss-framework/src/main/java/com/soss/framework/web/service/TokenService.java
View file @
0e6988c6
package
com
.
soss
.
framework
.
web
.
service
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.concurrent.TimeUnit
;
import
javax.servlet.http.HttpServletRequest
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
com.alibaba.fastjson.JSON
;
import
com.soss.common.constant.Constants
;
import
com.soss.common.core.domain.model.LoginUser
;
import
com.soss.common.core.redis.RedisCache
;
...
...
@@ -21,6 +13,15 @@ import eu.bitwalker.useragentutils.UserAgent;
import
io.jsonwebtoken.Claims
;
import
io.jsonwebtoken.Jwts
;
import
io.jsonwebtoken.SignatureAlgorithm
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
javax.servlet.http.HttpServletRequest
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.concurrent.TimeUnit
;
/**
* token验证处理
...
...
@@ -29,8 +30,7 @@ import io.jsonwebtoken.SignatureAlgorithm;
*/
@Component
@Slf4j
public
class
TokenService
{
public
class
TokenService
{
// 令牌自定义标识
@Value
(
"${token.header}"
)
private
String
header
;
...
...
@@ -57,23 +57,19 @@ public class TokenService
*
* @return 用户信息
*/
public
LoginUser
getLoginUser
(
HttpServletRequest
request
)
{
public
LoginUser
getLoginUser
(
HttpServletRequest
request
)
{
// 获取请求携带的令牌
String
token
=
getToken
(
request
);
if
(
StringUtils
.
isNotEmpty
(
token
))
{
try
{
if
(
StringUtils
.
isNotEmpty
(
token
))
{
try
{
Claims
claims
=
parseToken
(
token
);
// 解析对应的权限以及用户信息
String
uuid
=
(
String
)
claims
.
get
(
Constants
.
LOGIN_USER_KEY
);
String
userKey
=
getTokenKey
(
uuid
);
LoginUser
user
=
redisCache
.
getCacheObject
(
userKey
);
log
.
info
(
"getUser, {}"
,
JSON
.
toJSON
(
user
));
return
user
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
log
.
error
(
""
,
e
);
}
}
...
...
@@ -83,10 +79,8 @@ public class TokenService
/**
* 设置用户身份信息
*/
public
void
setLoginUser
(
LoginUser
loginUser
)
{
if
(
StringUtils
.
isNotNull
(
loginUser
)
&&
StringUtils
.
isNotEmpty
(
loginUser
.
getToken
()))
{
public
void
setLoginUser
(
LoginUser
loginUser
)
{
if
(
StringUtils
.
isNotNull
(
loginUser
)
&&
StringUtils
.
isNotEmpty
(
loginUser
.
getToken
()))
{
refreshToken
(
loginUser
);
}
}
...
...
@@ -94,10 +88,8 @@ public class TokenService
/**
* 删除用户身份信息
*/
public
void
delLoginUser
(
String
token
)
{
if
(
StringUtils
.
isNotEmpty
(
token
))
{
public
void
delLoginUser
(
String
token
)
{
if
(
StringUtils
.
isNotEmpty
(
token
))
{
String
userKey
=
getTokenKey
(
token
);
redisCache
.
deleteObject
(
userKey
);
}
...
...
@@ -109,8 +101,7 @@ public class TokenService
* @param loginUser 用户信息
* @return 令牌
*/
public
String
createToken
(
LoginUser
loginUser
)
{
public
String
createToken
(
LoginUser
loginUser
)
{
String
token
=
IdUtils
.
fastUUID
();
loginUser
.
setToken
(
token
);
setUserAgent
(
loginUser
);
...
...
@@ -127,12 +118,10 @@ public class TokenService
* @param loginUser
* @return 令牌
*/
public
void
verifyToken
(
LoginUser
loginUser
)
{
public
void
verifyToken
(
LoginUser
loginUser
)
{
long
expireTime
=
loginUser
.
getExpireTime
();
long
currentTime
=
System
.
currentTimeMillis
();
if
(
expireTime
-
currentTime
<=
MILLIS_MINUTE_TEN
)
{
if
(
expireTime
-
currentTime
<=
MILLIS_MINUTE_TEN
)
{
refreshToken
(
loginUser
);
}
}
...
...
@@ -142,8 +131,7 @@ public class TokenService
*
* @param loginUser 登录信息
*/
public
void
refreshToken
(
LoginUser
loginUser
)
{
public
void
refreshToken
(
LoginUser
loginUser
)
{
loginUser
.
setLoginTime
(
System
.
currentTimeMillis
());
loginUser
.
setExpireTime
(
loginUser
.
getLoginTime
()
+
expireTime
*
MILLIS_MINUTE
);
// 根据uuid将loginUser缓存
...
...
@@ -156,8 +144,7 @@ public class TokenService
*
* @param loginUser 登录信息
*/
public
void
setUserAgent
(
LoginUser
loginUser
)
{
public
void
setUserAgent
(
LoginUser
loginUser
)
{
UserAgent
userAgent
=
UserAgent
.
parseUserAgentString
(
ServletUtils
.
getRequest
().
getHeader
(
"User-Agent"
));
String
ip
=
IpUtils
.
getIpAddr
(
ServletUtils
.
getRequest
());
loginUser
.
setIpaddr
(
ip
);
...
...
@@ -172,8 +159,7 @@ public class TokenService
* @param claims 数据声明
* @return 令牌
*/
private
String
createToken
(
Map
<
String
,
Object
>
claims
)
{
private
String
createToken
(
Map
<
String
,
Object
>
claims
)
{
String
token
=
Jwts
.
builder
()
.
setClaims
(
claims
)
.
signWith
(
SignatureAlgorithm
.
HS512
,
secret
).
compact
();
...
...
@@ -186,8 +172,7 @@ public class TokenService
* @param token 令牌
* @return 数据声明
*/
private
Claims
parseToken
(
String
token
)
{
private
Claims
parseToken
(
String
token
)
{
return
Jwts
.
parser
()
.
setSigningKey
(
secret
)
.
parseClaimsJws
(
token
)
...
...
@@ -200,8 +185,7 @@ public class TokenService
* @param token 令牌
* @return 用户名
*/
public
String
getUsernameFromToken
(
String
token
)
{
public
String
getUsernameFromToken
(
String
token
)
{
Claims
claims
=
parseToken
(
token
);
return
claims
.
getSubject
();
}
...
...
@@ -212,18 +196,15 @@ public class TokenService
* @param request
* @return token
*/
private
String
getToken
(
HttpServletRequest
request
)
{
private
String
getToken
(
HttpServletRequest
request
)
{
String
token
=
request
.
getHeader
(
header
);
if
(
StringUtils
.
isNotEmpty
(
token
)
&&
token
.
startsWith
(
Constants
.
TOKEN_PREFIX
))
{
if
(
StringUtils
.
isNotEmpty
(
token
)
&&
token
.
startsWith
(
Constants
.
TOKEN_PREFIX
))
{
token
=
token
.
replace
(
Constants
.
TOKEN_PREFIX
,
""
);
}
return
token
;
}
private
String
getTokenKey
(
String
uuid
)
{
private
String
getTokenKey
(
String
uuid
)
{
return
Constants
.
LOGIN_TOKEN_KEY
+
uuid
;
}
}
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