Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
szpt
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
以墨为白
szpt
Commits
4c42d9ed
Commit
4c42d9ed
authored
Jan 08, 2025
by
以墨为白
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
获取用户信息
parent
0ed54532
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
164 additions
and
7 deletions
+164
-7
src/main/java/com/zksy/szpt/domain/CurrentUserInfo.java
src/main/java/com/zksy/szpt/domain/CurrentUserInfo.java
+82
-0
src/main/java/com/zksy/szpt/domain/po/AppStore.java
src/main/java/com/zksy/szpt/domain/po/AppStore.java
+9
-0
src/main/java/com/zksy/szpt/filter/SignatureVerificationFilter.java
...ava/com/zksy/szpt/filter/SignatureVerificationFilter.java
+23
-1
src/main/java/com/zksy/szpt/handler/SzptMetaObjectHandler.java
...ain/java/com/zksy/szpt/handler/SzptMetaObjectHandler.java
+17
-6
src/main/java/com/zksy/szpt/service/AppStoreService.java
src/main/java/com/zksy/szpt/service/AppStoreService.java
+10
-0
src/main/java/com/zksy/szpt/util/UserContextHolder.java
src/main/java/com/zksy/szpt/util/UserContextHolder.java
+23
-0
No files found.
src/main/java/com/zksy/szpt/domain/CurrentUserInfo.java
0 → 100644
View file @
4c42d9ed
package
com
.
zksy
.
szpt
.
domain
;
public
class
CurrentUserInfo
{
private
String
userId
;
private
String
userName
;
private
String
deptCode
;
private
String
ip
;
public
CurrentUserInfo
()
{
}
private
CurrentUserInfo
(
CurrentUserInfoBuilder
builder
)
{
this
.
userId
=
builder
.
userId
;
this
.
userName
=
builder
.
userName
;
this
.
deptCode
=
builder
.
deptCode
;
this
.
ip
=
builder
.
ip
;
}
// 建造器类
public
static
class
CurrentUserInfoBuilder
{
private
String
userId
;
// 必选项,用 final 修饰
private
String
userName
;
// 必选项,用 final 修饰
private
String
deptCode
;
private
String
ip
;
// 可选项,不用 final 修饰
public
CurrentUserInfoBuilder
userId
(
String
userId
)
{
// 可选参数
this
.
userId
=
userId
;
return
this
;
}
public
CurrentUserInfoBuilder
userName
(
String
userName
)
{
// 可选参数
this
.
userName
=
userName
;
return
this
;
}
public
CurrentUserInfoBuilder
deptCode
(
String
deptCode
)
{
// 可选参数
this
.
deptCode
=
deptCode
;
return
this
;
}
public
CurrentUserInfoBuilder
ip
(
String
ip
)
{
// 可选参数
this
.
ip
=
ip
;
return
this
;
}
public
CurrentUserInfo
build
()
{
return
new
CurrentUserInfo
(
this
);
}
}
public
String
getUserId
()
{
return
userId
;
}
public
void
setUserId
(
String
userId
)
{
this
.
userId
=
userId
;
}
public
String
getUserName
()
{
return
userName
;
}
public
void
setUserName
(
String
userName
)
{
this
.
userName
=
userName
;
}
public
String
getDeptCode
()
{
return
deptCode
;
}
public
void
setDeptCode
(
String
deptCode
)
{
this
.
deptCode
=
deptCode
;
}
public
String
getIp
()
{
return
ip
;
}
public
void
setIp
(
String
ip
)
{
this
.
ip
=
ip
;
}
}
\ No newline at end of file
src/main/java/com/zksy/szpt/domain/po/AppStore.java
View file @
4c42d9ed
...
...
@@ -13,6 +13,7 @@ public class AppStore {
private
String
appSecret
;
@TableField
(
fill
=
FieldFill
.
INSERT
)
private
Date
createTime
;
private
String
deptCode
;
public
Long
getId
()
{
return
id
;
...
...
@@ -45,4 +46,12 @@ public class AppStore {
public
void
setCreateTime
(
Date
createTime
)
{
this
.
createTime
=
createTime
;
}
public
String
getDeptCode
()
{
return
deptCode
;
}
public
void
setDeptCode
(
String
deptCode
)
{
this
.
deptCode
=
deptCode
;
}
}
src/main/java/com/zksy/szpt/filter/SignatureVerificationFilter.java
View file @
4c42d9ed
...
...
@@ -5,11 +5,14 @@ import cn.hutool.crypto.digest.DigestUtil;
import
cn.hutool.json.JSONUtil
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.zksy.szpt.domain.CurrentUserInfo
;
import
com.zksy.szpt.domain.HttpResult
;
import
com.zksy.szpt.domain.HttpResultState
;
import
com.zksy.szpt.domain.po.AppStore
;
import
com.zksy.szpt.service.AppStoreService
;
import
com.zksy.szpt.util.EncryptUtil
;
import
com.zksy.szpt.util.SignatureUtil
;
import
com.zksy.szpt.util.UserContextHolder
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.data.redis.core.RedisTemplate
;
...
...
@@ -96,12 +99,16 @@ public class SignatureVerificationFilter extends OncePerRequestFilter {
}
// 校验appId
String
secretKey
=
this
.
appStoreService
.
getAppSecretByAppKey
(
appId
);
AppStore
appStore
=
this
.
appStoreService
.
getAppSecretInfo
(
appId
);
String
secretKey
=
appStore
.
getAppSecret
();
if
(!
StringUtils
.
hasText
(
secretKey
))
{
this
.
write
(
response
,
"appId无效"
);
return
false
;
}
//验证单位 todo
// String deptCode = appStore.getDeptCode();
// 请求体
String
body
=
StreamUtils
.
copyToString
(
request
.
getInputStream
(),
StandardCharsets
.
UTF_8
);
body
=
objectMapper
.
writeValueAsString
(
objectMapper
.
readValue
(
body
,
Map
.
class
));
...
...
@@ -113,6 +120,9 @@ public class SignatureVerificationFilter extends OncePerRequestFilter {
write
(
response
,
"签名有误,generatedSignature:"
+
generatedSignature
+
",sign: "
+
sign
+
",appId:"
+
appId
+
",nonce:"
+
nonce
+
",timestamp:"
+
timestampStr
);
return
false
;
}
// 签名验证通过
addUserInfo
(
request
);
return
true
;
}
...
...
@@ -131,5 +141,17 @@ public class SignatureVerificationFilter extends OncePerRequestFilter {
response
.
setCharacterEncoding
(
StandardCharsets
.
UTF_8
.
name
());
response
.
getWriter
().
write
(
JSONUtil
.
toJsonStr
(
httpResult
));
}
/**
* 增加用户信息
* @param request
*/
private
void
addUserInfo
(
HttpServletRequest
request
)
{
// 增加用户信息
String
userId
=
request
.
getHeader
(
SignatureUtil
.
APPID
);
String
ip
=
request
.
getRemoteAddr
();
CurrentUserInfo
currentUserInfo
=
new
CurrentUserInfo
.
CurrentUserInfoBuilder
().
userId
(
userId
).
userName
(
userId
).
ip
(
ip
).
build
();
UserContextHolder
.
set
(
currentUserInfo
);
}
}
src/main/java/com/zksy/szpt/handler/SzptMetaObjectHandler.java
View file @
4c42d9ed
...
...
@@ -2,6 +2,8 @@ package com.zksy.szpt.handler;
import
com.baomidou.mybatisplus.core.handlers.MetaObjectHandler
;
import
com.baomidou.mybatisplus.core.handlers.StrictFill
;
import
com.zksy.szpt.domain.CurrentUserInfo
;
import
com.zksy.szpt.util.UserContextHolder
;
import
org.apache.ibatis.reflection.MetaObject
;
import
org.springframework.stereotype.Component
;
...
...
@@ -19,6 +21,8 @@ public class SzptMetaObjectHandler implements MetaObjectHandler {
// 新增时自动填充 create_time, update_time 字段
// this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
// this.strictInsertFill(metaObject, "cjsj", Date.class, new Date());
CurrentUserInfo
currentUserInfo
=
UserContextHolder
.
get
();
List
<
StrictFill
>
fields
=
Arrays
.
asList
(
StrictFill
.
of
(
"createTime"
,
Date
.
class
,
new
Date
()),
StrictFill
.
of
(
"cjsj"
,
Date
.
class
,
new
Date
()),
...
...
@@ -28,11 +32,14 @@ public class SzptMetaObjectHandler implements MetaObjectHandler {
StrictFill
.
of
(
"sjgsdwmc"
,
String
.
class
,
"1"
),
StrictFill
.
of
(
"createGajgjgdm"
,
String
.
class
,
"1"
),
StrictFill
.
of
(
"createGajgmc"
,
String
.
class
,
"1"
),
StrictFill
.
of
(
"createId"
,
String
.
class
,
"1"
),
// StrictFill.of("createId", String.class, "1"),
StrictFill
.
of
(
"createId"
,
String
.
class
,
currentUserInfo
.
getUserId
()),
StrictFill
.
of
(
"cjrid"
,
String
.
class
,
"1"
),
StrictFill
.
of
(
"createBy"
,
String
.
class
,
"1"
),
// StrictFill.of("createBy", String.class, "1"),
StrictFill
.
of
(
"createBy"
,
String
.
class
,
currentUserInfo
.
getUserId
()),
StrictFill
.
of
(
"createTerminal"
,
String
.
class
,
"1"
),
StrictFill
.
of
(
"createIp"
,
String
.
class
,
"1"
),
// StrictFill.of("createIp", String.class, "1"),
StrictFill
.
of
(
"createIp"
,
String
.
class
,
currentUserInfo
.
getIp
()),
StrictFill
.
of
(
"delFlag"
,
Integer
.
class
,
0
)
// StrictFill.of("del_flag", Integer.class,0)
...
...
@@ -42,6 +49,7 @@ public class SzptMetaObjectHandler implements MetaObjectHandler {
@Override
public
void
updateFill
(
MetaObject
metaObject
)
{
CurrentUserInfo
currentUserInfo
=
UserContextHolder
.
get
();
List
<
StrictFill
>
fields
=
Arrays
.
asList
(
StrictFill
.
of
(
"updateTime"
,
Date
.
class
,
new
Date
()),
StrictFill
.
of
(
"gxrid"
,
String
.
class
,
"1"
),
...
...
@@ -49,11 +57,14 @@ public class SzptMetaObjectHandler implements MetaObjectHandler {
StrictFill
.
of
(
"gxrxm"
,
String
.
class
,
"测试"
),
StrictFill
.
of
(
"update_gajgjgdm"
,
String
.
class
,
'1'
),
StrictFill
.
of
(
"update_gajgmc"
,
String
.
class
,
'1'
),
StrictFill
.
of
(
"update_id"
,
String
.
class
,
'1'
),
StrictFill
.
of
(
"update_by"
,
String
.
class
,
'1'
),
// StrictFill.of("update_id", String.class, '1'),
StrictFill
.
of
(
"update_id"
,
String
.
class
,
currentUserInfo
.
getUserId
()),
// StrictFill.of("update_by", String.class, '1'),
StrictFill
.
of
(
"update_by"
,
String
.
class
,
currentUserInfo
.
getUserId
()),
StrictFill
.
of
(
"update_time"
,
Date
.
class
,
new
Date
()),
StrictFill
.
of
(
"update_terminal"
,
String
.
class
,
'1'
),
StrictFill
.
of
(
"update_ip"
,
String
.
class
,
'1'
)
// StrictFill.of("update_ip", String.class, '1'),
StrictFill
.
of
(
"update_ip"
,
String
.
class
,
currentUserInfo
.
getIp
())
// StrictFill.of("del_flag", Integer.class,0)
);
this
.
strictInsertFill
(
findTableInfo
(
metaObject
),
metaObject
,
fields
);
...
...
src/main/java/com/zksy/szpt/service/AppStoreService.java
View file @
4c42d9ed
...
...
@@ -31,4 +31,14 @@ public class AppStoreService {
AppStore
appStore
=
BeanMapperUtil
.
map
(
appStoreDTO
,
AppStore
.
class
);
return
appStoreMapper
.
insert
(
appStore
);
}
public
AppStore
getAppSecretInfo
(
String
appKey
)
{
LambdaQueryWrapper
<
AppStore
>
queryWrapper
=
new
LambdaQueryWrapper
<>();
queryWrapper
.
eq
(
AppStore:
:
getAppKey
,
appKey
);
List
<
AppStore
>
tbAppStorePOList
=
appStoreMapper
.
selectList
(
queryWrapper
);
if
(
tbAppStorePOList
!=
null
&&
!
tbAppStorePOList
.
isEmpty
())
{
return
tbAppStorePOList
.
get
(
0
);
}
return
null
;
}
}
src/main/java/com/zksy/szpt/util/UserContextHolder.java
0 → 100644
View file @
4c42d9ed
package
com
.
zksy
.
szpt
.
util
;
import
com.zksy.szpt.domain.CurrentUserInfo
;
/**
* 保存当前http请求的用户登录信息
*/
public
class
UserContextHolder
{
private
static
final
ThreadLocal
<
CurrentUserInfo
>
userThread
=
new
ThreadLocal
<>();
public
static
void
set
(
CurrentUserInfo
user
){
userThread
.
set
(
user
);
}
public
static
CurrentUserInfo
get
(){
return
userThread
.
get
();
}
//防止内存泄漏
public
static
void
remove
(){
userThread
.
remove
();
}
}
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