Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
polysoft-nafmii
/
polysoft-parent
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit b88cb5dc
authored
Apr 28, 2021
by
ypenglv
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
新增工具类
1 parent
cdd1ef5a
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
24 additions
and
12 deletions
nafmii-app/src/main/java/org/nafmii/app/user/service/impl/UserServiceImpl.java
nafmii-common/src/main/java/org/nafmii/common/constant/HttpClientHeaderEnum.java
nafmii-common/src/main/java/org/nafmii/common/utils/JwtUtils.java
nafmii-common/src/main/java/org/nafmii/common/utils/RequestUtils.java
nafmii-app/src/main/java/org/nafmii/app/user/service/impl/UserServiceImpl.java
View file @
b88cb5d
...
...
@@ -57,6 +57,7 @@ public class UserServiceImpl implements UserService {
@Override
public
ApiResponse
<
UserVO
>
login
()
{
ApiResponse
result
=
new
ApiResponse
();
long
EXPIRITION
=
1000
*
60
*
60
*
24
*
7
;
//假设验证数据通过
UserVO
userVO
=
new
UserVO
();
userVO
.
setLoginPwd
(
"zzz"
);
...
...
@@ -79,7 +80,7 @@ public class UserServiceImpl implements UserService {
String
substring
=
sRole
.
substring
(
0
,
sRole
.
lastIndexOf
(
","
));
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
HttpClientHeaderEnum
.
USER_HEADER_USER_ID
.
getCode
(),
userVO
.
getId
());
String
token
=
JwtUtils
.
generateJsonWebToken
(
map
);
String
token
=
JwtUtils
.
generateJsonWebToken
(
map
,
EXPIRITION
);
// userVO.setToken(token);
result
.
setData
(
userVO
);
return
result
;
...
...
@@ -140,6 +141,7 @@ public class UserServiceImpl implements UserService {
@Override
public
ApiResponse
<
UserVO
>
userLogin
(
LoginDto
loginDto
)
{
UserVO
userVo
=
new
UserVO
();
long
EXPIRITION
=
1000
*
60
*
60
*
24
*
7
;
//首先判断登陆方式
if
(
loginDto
.
getType
().
equals
(
"0"
)){
//密码登陆
...
...
@@ -164,7 +166,7 @@ public class UserServiceImpl implements UserService {
}
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
HttpClientHeaderEnum
.
USER_HEADER_USER_ID
.
getCode
(),
userVo
.
getId
());
String
token
=
JwtUtils
.
generateJsonWebToken
(
map
);
String
token
=
JwtUtils
.
generateJsonWebToken
(
map
,
EXPIRITION
);
userVo
.
setAuthorization
(
token
);
ApiResponse
apiResponse
=
new
ApiResponse
();
apiResponse
.
setData
(
userVo
);
...
...
nafmii-common/src/main/java/org/nafmii/common/constant/HttpClientHeaderEnum.java
View file @
b88cb5d
...
...
@@ -8,7 +8,8 @@ public enum HttpClientHeaderEnum {
*/
USER_HEADER_USER_ID
(
"userId"
,
"用户ID"
),
USER_HEADER_USER_TOKEN
(
"Authorization"
,
"用户token"
),
USER_HEADER_MODE
(
"mode"
,
"设备:PC,APP,PAD"
);
USER_HEADER_MODE
(
"mode"
,
"设备:PC,APP,PAD"
),
USER_VO
(
"USERVO"
,
"用户信息"
);
HttpClientHeaderEnum
(
String
code
,
String
name
){
this
.
code
=
code
;
...
...
nafmii-common/src/main/java/org/nafmii/common/utils/JwtUtils.java
View file @
b88cb5d
...
...
@@ -18,15 +18,18 @@ public class JwtUtils {
public
static
final
String
APPSECRET_KEY
=
"qwertyuiop123456780"
;
public
static
String
generateJsonWebToken
(
Map
<
String
,
Object
>
param
)
{
public
static
String
generateJsonWebToken
(
Map
<
String
,
Object
>
param
,
long
timeOut
)
{
if
(
timeOut
<
0
){
timeOut
=
EXPIRITION
;
}
if
(
param
.
isEmpty
())
{
return
null
;
}
return
Jwts
.
builder
()
.
claim
(
HttpClientHeaderEnum
.
USER_
HEADER_USER_ID
.
getCode
(),
param
.
get
(
HttpClientHeaderEnum
.
USER_HEADER_USER_ID
.
getCode
())
)
.
claim
(
HttpClientHeaderEnum
.
USER_
VO
.
getCode
(),
param
)
.
setIssuedAt
(
new
Date
())
.
setExpiration
(
new
Date
(
System
.
currentTimeMillis
()
+
EXPIRITION
))
.
setExpiration
(
new
Date
(
System
.
currentTimeMillis
()
+
timeOut
))
.
signWith
(
SignatureAlgorithm
.
HS256
,
APPSECRET_KEY
).
compact
();
}
...
...
@@ -68,10 +71,11 @@ public class JwtUtils {
public
static
void
main
(
String
[]
args
)
{
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"userId"
,
12312321313L
);
String
token
=
generateJsonWebToken
(
map
);
String
token
=
generateJsonWebToken
(
map
,
EXPIRITION
);
System
.
out
.
println
(
token
);
Claims
claims
=
getUserInfo
(
token
);
System
.
out
.
println
(
claims
.
get
(
HttpClientHeaderEnum
.
USER_HEADER_USER_ID
.
getCode
()));
Map
<
String
,
Object
>
map1
=
(
Map
<
String
,
Object
>)
claims
.
get
(
HttpClientHeaderEnum
.
USER_VO
.
getCode
());
System
.
out
.
println
(
map
.
get
(
HttpClientHeaderEnum
.
USER_HEADER_USER_ID
.
getCode
()));
System
.
out
.
println
(
isExpiration
(
token
));
}
}
nafmii-common/src/main/java/org/nafmii/common/utils/RequestUtils.java
View file @
b88cb5d
package
org
.
nafmii
.
common
.
utils
;
import
org.apache.commons.lang.StringUtils
;
import
org.nafmii.common.constant.MessageEnum
;
import
org.nafmii.common.constant.HttpClientHeaderEnum
;
import
org.nafmii.common.exception.BusinessException
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.context.request.RequestAttributes
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.util.Map
;
public
class
RequestUtils
{
...
...
@@ -28,15 +29,19 @@ public class RequestUtils {
public
static
Long
getUserId
(){
//token 获取用户信息
String
token
=
getToken
();
String
userId
=
JwtUtils
.
getUserInfo
(
token
).
get
(
HttpClientHeaderEnum
.
USER_HEADER_USER_ID
.
getCode
()).
toString
();
if
(!
StringUtils
.
isEmpty
(
userId
)){
if
(
StringUtils
.
isNotBlank
(
token
)){
throw
new
BusinessException
(
"令牌不能为空"
);
}
Map
<
String
,
Object
>
map
=
(
Map
<
String
,
Object
>)
JwtUtils
.
getUserInfo
(
token
).
get
(
HttpClientHeaderEnum
.
USER_VO
.
getCode
());
String
userId
=
map
.
get
(
HttpClientHeaderEnum
.
USER_HEADER_USER_ID
.
getCode
()).
toString
();
if
(
StringUtils
.
isNotBlank
(
userId
)){
try
{
return
Long
.
valueOf
(
userId
);
}
catch
(
NumberFormatException
e
)
{
return
null
;
}
}
return
null
;
throw
new
BusinessException
(
"用户主键不能为空"
)
;
}
public
static
String
getToken
(){
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment