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
;
package
com
.
soss
.
framework
.
web
.
service
;
import
java.util.HashMap
;
import
com.alibaba.fastjson.JSON
;
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.soss.common.constant.Constants
;
import
com.soss.common.constant.Constants
;
import
com.soss.common.core.domain.model.LoginUser
;
import
com.soss.common.core.domain.model.LoginUser
;
import
com.soss.common.core.redis.RedisCache
;
import
com.soss.common.core.redis.RedisCache
;
...
@@ -21,6 +13,15 @@ import eu.bitwalker.useragentutils.UserAgent;
...
@@ -21,6 +13,15 @@ import eu.bitwalker.useragentutils.UserAgent;
import
io.jsonwebtoken.Claims
;
import
io.jsonwebtoken.Claims
;
import
io.jsonwebtoken.Jwts
;
import
io.jsonwebtoken.Jwts
;
import
io.jsonwebtoken.SignatureAlgorithm
;
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验证处理
* token验证处理
...
@@ -29,8 +30,7 @@ import io.jsonwebtoken.SignatureAlgorithm;
...
@@ -29,8 +30,7 @@ import io.jsonwebtoken.SignatureAlgorithm;
*/
*/
@Component
@Component
@Slf4j
@Slf4j
public
class
TokenService
public
class
TokenService
{
{
// 令牌自定义标识
// 令牌自定义标识
@Value
(
"${token.header}"
)
@Value
(
"${token.header}"
)
private
String
header
;
private
String
header
;
...
@@ -57,23 +57,19 @@ public class TokenService
...
@@ -57,23 +57,19 @@ public class TokenService
*
*
* @return 用户信息
* @return 用户信息
*/
*/
public
LoginUser
getLoginUser
(
HttpServletRequest
request
)
public
LoginUser
getLoginUser
(
HttpServletRequest
request
)
{
{
// 获取请求携带的令牌
// 获取请求携带的令牌
String
token
=
getToken
(
request
);
String
token
=
getToken
(
request
);
if
(
StringUtils
.
isNotEmpty
(
token
))
if
(
StringUtils
.
isNotEmpty
(
token
))
{
{
try
{
try
{
Claims
claims
=
parseToken
(
token
);
Claims
claims
=
parseToken
(
token
);
// 解析对应的权限以及用户信息
// 解析对应的权限以及用户信息
String
uuid
=
(
String
)
claims
.
get
(
Constants
.
LOGIN_USER_KEY
);
String
uuid
=
(
String
)
claims
.
get
(
Constants
.
LOGIN_USER_KEY
);
String
userKey
=
getTokenKey
(
uuid
);
String
userKey
=
getTokenKey
(
uuid
);
LoginUser
user
=
redisCache
.
getCacheObject
(
userKey
);
LoginUser
user
=
redisCache
.
getCacheObject
(
userKey
);
log
.
info
(
"getUser, {}"
,
JSON
.
toJSON
(
user
));
return
user
;
return
user
;
}
}
catch
(
Exception
e
)
{
catch
(
Exception
e
)
{
log
.
error
(
""
,
e
);
log
.
error
(
""
,
e
);
}
}
}
}
...
@@ -83,10 +79,8 @@ public class TokenService
...
@@ -83,10 +79,8 @@ public class TokenService
/**
/**
* 设置用户身份信息
* 设置用户身份信息
*/
*/
public
void
setLoginUser
(
LoginUser
loginUser
)
public
void
setLoginUser
(
LoginUser
loginUser
)
{
{
if
(
StringUtils
.
isNotNull
(
loginUser
)
&&
StringUtils
.
isNotEmpty
(
loginUser
.
getToken
()))
{
if
(
StringUtils
.
isNotNull
(
loginUser
)
&&
StringUtils
.
isNotEmpty
(
loginUser
.
getToken
()))
{
refreshToken
(
loginUser
);
refreshToken
(
loginUser
);
}
}
}
}
...
@@ -94,10 +88,8 @@ public class TokenService
...
@@ -94,10 +88,8 @@ public class TokenService
/**
/**
* 删除用户身份信息
* 删除用户身份信息
*/
*/
public
void
delLoginUser
(
String
token
)
public
void
delLoginUser
(
String
token
)
{
{
if
(
StringUtils
.
isNotEmpty
(
token
))
{
if
(
StringUtils
.
isNotEmpty
(
token
))
{
String
userKey
=
getTokenKey
(
token
);
String
userKey
=
getTokenKey
(
token
);
redisCache
.
deleteObject
(
userKey
);
redisCache
.
deleteObject
(
userKey
);
}
}
...
@@ -109,8 +101,7 @@ public class TokenService
...
@@ -109,8 +101,7 @@ public class TokenService
* @param loginUser 用户信息
* @param loginUser 用户信息
* @return 令牌
* @return 令牌
*/
*/
public
String
createToken
(
LoginUser
loginUser
)
public
String
createToken
(
LoginUser
loginUser
)
{
{
String
token
=
IdUtils
.
fastUUID
();
String
token
=
IdUtils
.
fastUUID
();
loginUser
.
setToken
(
token
);
loginUser
.
setToken
(
token
);
setUserAgent
(
loginUser
);
setUserAgent
(
loginUser
);
...
@@ -127,12 +118,10 @@ public class TokenService
...
@@ -127,12 +118,10 @@ public class TokenService
* @param loginUser
* @param loginUser
* @return 令牌
* @return 令牌
*/
*/
public
void
verifyToken
(
LoginUser
loginUser
)
public
void
verifyToken
(
LoginUser
loginUser
)
{
{
long
expireTime
=
loginUser
.
getExpireTime
();
long
expireTime
=
loginUser
.
getExpireTime
();
long
currentTime
=
System
.
currentTimeMillis
();
long
currentTime
=
System
.
currentTimeMillis
();
if
(
expireTime
-
currentTime
<=
MILLIS_MINUTE_TEN
)
if
(
expireTime
-
currentTime
<=
MILLIS_MINUTE_TEN
)
{
{
refreshToken
(
loginUser
);
refreshToken
(
loginUser
);
}
}
}
}
...
@@ -142,8 +131,7 @@ public class TokenService
...
@@ -142,8 +131,7 @@ public class TokenService
*
*
* @param loginUser 登录信息
* @param loginUser 登录信息
*/
*/
public
void
refreshToken
(
LoginUser
loginUser
)
public
void
refreshToken
(
LoginUser
loginUser
)
{
{
loginUser
.
setLoginTime
(
System
.
currentTimeMillis
());
loginUser
.
setLoginTime
(
System
.
currentTimeMillis
());
loginUser
.
setExpireTime
(
loginUser
.
getLoginTime
()
+
expireTime
*
MILLIS_MINUTE
);
loginUser
.
setExpireTime
(
loginUser
.
getLoginTime
()
+
expireTime
*
MILLIS_MINUTE
);
// 根据uuid将loginUser缓存
// 根据uuid将loginUser缓存
...
@@ -156,8 +144,7 @@ public class TokenService
...
@@ -156,8 +144,7 @@ public class TokenService
*
*
* @param loginUser 登录信息
* @param loginUser 登录信息
*/
*/
public
void
setUserAgent
(
LoginUser
loginUser
)
public
void
setUserAgent
(
LoginUser
loginUser
)
{
{
UserAgent
userAgent
=
UserAgent
.
parseUserAgentString
(
ServletUtils
.
getRequest
().
getHeader
(
"User-Agent"
));
UserAgent
userAgent
=
UserAgent
.
parseUserAgentString
(
ServletUtils
.
getRequest
().
getHeader
(
"User-Agent"
));
String
ip
=
IpUtils
.
getIpAddr
(
ServletUtils
.
getRequest
());
String
ip
=
IpUtils
.
getIpAddr
(
ServletUtils
.
getRequest
());
loginUser
.
setIpaddr
(
ip
);
loginUser
.
setIpaddr
(
ip
);
...
@@ -172,8 +159,7 @@ public class TokenService
...
@@ -172,8 +159,7 @@ public class TokenService
* @param claims 数据声明
* @param claims 数据声明
* @return 令牌
* @return 令牌
*/
*/
private
String
createToken
(
Map
<
String
,
Object
>
claims
)
private
String
createToken
(
Map
<
String
,
Object
>
claims
)
{
{
String
token
=
Jwts
.
builder
()
String
token
=
Jwts
.
builder
()
.
setClaims
(
claims
)
.
setClaims
(
claims
)
.
signWith
(
SignatureAlgorithm
.
HS512
,
secret
).
compact
();
.
signWith
(
SignatureAlgorithm
.
HS512
,
secret
).
compact
();
...
@@ -186,8 +172,7 @@ public class TokenService
...
@@ -186,8 +172,7 @@ public class TokenService
* @param token 令牌
* @param token 令牌
* @return 数据声明
* @return 数据声明
*/
*/
private
Claims
parseToken
(
String
token
)
private
Claims
parseToken
(
String
token
)
{
{
return
Jwts
.
parser
()
return
Jwts
.
parser
()
.
setSigningKey
(
secret
)
.
setSigningKey
(
secret
)
.
parseClaimsJws
(
token
)
.
parseClaimsJws
(
token
)
...
@@ -200,8 +185,7 @@ public class TokenService
...
@@ -200,8 +185,7 @@ public class TokenService
* @param token 令牌
* @param token 令牌
* @return 用户名
* @return 用户名
*/
*/
public
String
getUsernameFromToken
(
String
token
)
public
String
getUsernameFromToken
(
String
token
)
{
{
Claims
claims
=
parseToken
(
token
);
Claims
claims
=
parseToken
(
token
);
return
claims
.
getSubject
();
return
claims
.
getSubject
();
}
}
...
@@ -212,18 +196,15 @@ public class TokenService
...
@@ -212,18 +196,15 @@ public class TokenService
* @param request
* @param request
* @return token
* @return token
*/
*/
private
String
getToken
(
HttpServletRequest
request
)
private
String
getToken
(
HttpServletRequest
request
)
{
{
String
token
=
request
.
getHeader
(
header
);
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
,
""
);
token
=
token
.
replace
(
Constants
.
TOKEN_PREFIX
,
""
);
}
}
return
token
;
return
token
;
}
}
private
String
getTokenKey
(
String
uuid
)
private
String
getTokenKey
(
String
uuid
)
{
{
return
Constants
.
LOGIN_TOKEN_KEY
+
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