Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sv-springboot
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bsoft
sv-springboot
Commits
386f003b
Commit
386f003b
authored
Mar 22, 2021
by
宋振民
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:审计日志新增登录时长
parent
365c8769
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
90 additions
and
37 deletions
+90
-37
hs-admin/src/main/java/com/hs/admin/common/Constants.java
+1
-0
hs-admin/src/main/java/com/hs/admin/common/configurations/WebMvcConfig.java
+21
-0
hs-admin/src/main/java/com/hs/admin/mapper/AuditLogMapper.java
+6
-1
hs-admin/src/main/java/com/hs/admin/model/AuditLog.java
+1
-0
hs-admin/src/main/java/com/hs/admin/model/reqmodel/AuditLogReq.java
+7
-0
hs-admin/src/main/java/com/hs/admin/service/impl/AuditLogServiceImpl.java
+16
-25
hs-admin/src/main/resources/mapper/AuditLogMapper.xml
+15
-0
hs-admin/src/test/java/com/hs/admin/AuditLogServiceTest.java
+23
-11
No files found.
hs-admin/src/main/java/com/hs/admin/common/Constants.java
View file @
386f003b
...
...
@@ -2,4 +2,5 @@ package com.hs.admin.common;
public
interface
Constants
{
String
TOKEN_KEY
=
"Authorization"
;
String
DEFAULT_ONLINE_TIME
=
"0小时0分钟"
;
}
hs-admin/src/main/java/com/hs/admin/common/configurations/WebMvcConfig.java
0 → 100644
View file @
386f003b
package
com
.
hs
.
admin
.
common
.
configurations
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
;
import
java.text.SimpleDateFormat
;
@Configuration
public
class
WebMvcConfig
{
@Bean
MappingJackson2HttpMessageConverter
mappingJackson2HttpMessageConverter
()
{
MappingJackson2HttpMessageConverter
converter
=
new
MappingJackson2HttpMessageConverter
();
ObjectMapper
mapper
=
new
ObjectMapper
();
mapper
.
setDateFormat
(
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
));
converter
.
setObjectMapper
(
mapper
);
return
converter
;
}
}
\ No newline at end of file
hs-admin/src/main/java/com/hs/admin/mapper/AuditLogMapper.java
View file @
386f003b
package
com
.
hs
.
admin
.
mapper
;
import
com.hs.admin.model.AuditLog
;
import
org.apache.ibatis.annotations.Param
;
import
org.mapstruct.Mapper
;
import
java.util.Date
;
import
java.util.List
;
@Mapper
...
...
@@ -10,5 +12,7 @@ public interface AuditLogMapper {
int
insert
(
AuditLog
auditLog
);
List
<
AuditLog
>
getAllAuditLogs
();
List
<
AuditLog
>
getAllAuditLogs
(
@Param
(
"startDate"
)
Date
startDate
,
@Param
(
"endDate"
)
Date
endDate
);
List
<
AuditLog
>
getAllTokens
(
String
token
);
}
\ No newline at end of file
hs-admin/src/main/java/com/hs/admin/model/AuditLog.java
View file @
386f003b
...
...
@@ -5,6 +5,7 @@ import lombok.Setter;
import
org.springframework.stereotype.Component
;
import
java.util.Date
;
import
java.util.List
;
@Getter
@Component
...
...
hs-admin/src/main/java/com/hs/admin/model/reqmodel/AuditLogReq.java
View file @
386f003b
...
...
@@ -6,6 +6,7 @@ import lombok.Data;
import
javax.validation.constraints.NotBlank
;
import
javax.validation.constraints.NotNull
;
import
java.util.Date
;
import
java.util.List
;
@Data
...
...
@@ -18,4 +19,10 @@ public class AuditLogReq {
@NotBlank
(
message
=
"用户名"
)
private
String
userName
;
@ApiModelProperty
(
value
=
"开始时间"
,
required
=
false
)
private
Date
startDate
;
@ApiModelProperty
(
value
=
"结束时间"
,
required
=
false
)
private
Date
endDate
;
}
hs-admin/src/main/java/com/hs/admin/service/impl/AuditLogServiceImpl.java
View file @
386f003b
...
...
@@ -2,6 +2,7 @@ package com.hs.admin.service.impl;
import
com.github.pagehelper.PageHelper
;
import
com.github.pagehelper.PageInfo
;
import
com.hs.admin.common.Constants
;
import
com.hs.admin.common.base.PageRequest
;
import
com.hs.admin.common.base.PageResult
;
import
com.hs.admin.common.utils.DateUtils
;
...
...
@@ -37,52 +38,42 @@ public class AuditLogServiceImpl implements AuditLogService {
public
PageResult
<
AuditLog
>
getAllAuditLog
(
AuditLogReq
req
)
{
PageRequest
page
=
req
.
getPage
();
PageHelper
.
startPage
(
page
.
getPageNum
(),
page
.
getPageSize
());
List
<
AuditLog
>
allAuditLogs
=
auditLogMapper
.
getAllAuditLogs
();
List
<
AuditLog
>
allAuditLogs
=
auditLogMapper
.
getAllAuditLogs
(
req
.
getStartDate
(),
req
.
getEndDate
()
);
PageResult
pageResult
=
PageUtil
.
getPageResult
(
page
,
new
PageInfo
<
AuditLog
>(
allAuditLogs
));
getOnlineTime
(
pageResult
.
getContent
());
return
pageResult
;
}
private
void
getOnlineTime
(
List
<
AuditLog
>
auditLogs
)
{
Map
<
String
,
String
>
onlineTimeMap
=
new
HashMap
<>();
Map
<
String
,
List
<
AuditLog
>>
map
=
auditLogs
.
stream
()
.
collect
(
groupingBy
(
AuditLog:
:
getToken
));
for
(
String
token
:
map
.
keySet
())
{
List
<
AuditLog
>
auditLogList
=
map
.
get
(
token
);
for
(
AuditLog
auditLog:
auditLogs
)
{
List
<
AuditLog
>
tokenList
=
auditLogMapper
.
getAllTokens
(
auditLog
.
getToken
());
Date
loginTime
=
null
;
Date
logoutTime
=
null
;
Date
lastRefreshTIme
=
null
;
System
.
out
.
println
(
"token="
+
token
);
for
(
AuditLog
auditLog:
auditLog
List
)
{
Date
createDate
=
a
uditLog
.
getCreateDate
();
if
(
a
uditLog
.
getOperateType
().
equals
(
"login"
))
{
loginTime
=
createDate
;
}
else
if
(
a
uditLog
.
getOperateType
().
equals
(
"logout"
))
{
logoutTime
=
createDate
;
}
else
if
(
a
uditLog
.
getOperateType
().
equals
(
"refresh"
))
{
for
(
AuditLog
tokenAuditLog:
token
List
)
{
Date
createDate
=
tokenA
uditLog
.
getCreateDate
();
if
(
tokenA
uditLog
.
getOperateType
().
equals
(
"login"
))
{
loginTime
=
createDate
;
}
else
if
(
tokenA
uditLog
.
getOperateType
().
equals
(
"logout"
))
{
logoutTime
=
createDate
;
}
else
if
(
tokenA
uditLog
.
getOperateType
().
equals
(
"refresh"
))
{
if
(
lastRefreshTIme
==
null
||
createDate
.
getTime
()>
lastRefreshTIme
.
getTime
())
{
lastRefreshTIme
=
createDate
;
lastRefreshTIme
=
createDate
;
}
}
}
//有登录、退出时间的
if
(
loginTime
!=
null
&&
logoutTime
!=
null
)
{
String
dateMinus
=
DateUtils
.
dateDiff
(
loginTime
,
logoutTime
);
System
.
out
.
println
(
"时间差为:"
+
dateMinus
);
onlineTimeMap
.
put
(
token
,
dateMinus
);
auditLog
.
setOnlineTime
(
dateMinus
);
}
else
if
(
loginTime
!=
null
&&
logoutTime
==
null
&&
lastRefreshTIme
!=
null
)
{
String
dateMinus
=
DateUtils
.
dateDiff
(
loginTime
,
lastRefreshTIme
);
System
.
out
.
println
(
"时间差为:"
+
dateMinus
);
onlineTimeMap
.
put
(
token
,
dateMinus
);
auditLog
.
setOnlineTime
(
dateMinus
);
}
else
{
onlineTimeMap
.
put
(
token
,
"0分钟"
);
auditLog
.
setOnlineTime
(
Constants
.
DEFAULT_ONLINE_TIME
);
}
}
System
.
out
.
println
(
onlineTimeMap
);
for
(
AuditLog
auditLog:
auditLogs
)
{
auditLog
.
setOnlineTime
(
onlineTimeMap
.
get
(
auditLog
.
getToken
()));
}
}
...
...
hs-admin/src/main/resources/mapper/AuditLogMapper.xml
View file @
386f003b
...
...
@@ -36,7 +36,21 @@
WHERE
al.state = 1 and al.operate_type in ('login','logout')
and al.token is not null and al.token
<![CDATA[ <> ]]>
""
<if
test=
"startDate!=null"
>
and al.create_date
<![CDATA[ >= ]]>
#{startDate}
</if >
<if
test=
"endDate!=null"
>
and al.create_date
<![CDATA[ <= ]]>
#{endDate}
</if >
ORDER BY
al.create_date DESC
</select>
<select
id=
"getAllTokens"
resultMap=
"BaseResultMap"
>
SELECT
al.*
FROM
audit_log al
WHERE
al.state = 1 and al.token=#{token}
</select>
</mapper>
\ No newline at end of file
hs-admin/src/test/java/com/hs/admin/AuditLogServiceTest.java
View file @
386f003b
package
com
.
hs
.
admin
;
import
com.hs.admin.common.base.PageRequest
;
import
com.hs.admin.common.base.PageResult
;
import
com.hs.admin.model.AuditLog
;
import
com.hs.admin.model.reqmodel.AuditLogReq
;
import
com.hs.admin.model.reqmodel.UserReq
;
import
com.hs.admin.service.AuditLogService
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
...
...
@@ -7,6 +12,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.List
;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
classes
=
HsAdminApplicationTests
.
class
)
public
class
AuditLogServiceTest
{
...
...
@@ -14,17 +24,19 @@ public class AuditLogServiceTest {
private
AuditLogService
auditLogService
;
@Test
public
void
testInfo
()
{
/*List<AuditLog> list = auditLogService.getAllAuditLog();
System.out.println(list);
UserReq.GetUserReq userReq = new UserReq.GetUserReq();
PageRequest page = new PageRequest();
page.setPageNum(1);
page.setPageSize(10);
userReq.setUserName("hospital");
userReq.setPage(page);
PageResult all = userService.getAllByPage(userReq);
System.out.println(all);*/
public
void
testInfo
()
throws
ParseException
{
SimpleDateFormat
sf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
Date
startDate
=
sf
.
parse
(
"2021-03-22 16:08:44"
);
Date
endDate
=
sf
.
parse
(
"2021-03-22 17:50:52"
);
AuditLogReq
req
=
new
AuditLogReq
();
PageRequest
pageRequest
=
new
PageRequest
();
pageRequest
.
setPageNum
(
1
);
pageRequest
.
setPageSize
(
14
);
req
.
setStartDate
(
startDate
);
req
.
setEndDate
(
endDate
);
req
.
setPage
(
pageRequest
);
PageResult
allAuditLog
=
auditLogService
.
getAllAuditLog
(
req
);
System
.
out
.
println
(
allAuditLog
.
getContent
().
size
());
}
}
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