Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
有来技术
youlai-mall
提交
efdbf422
Y
youlai-mall
项目概览
有来技术
/
youlai-mall
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
youlai-mall
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
efdbf422
编写于
3月 06, 2021
作者:
H
haoxr
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat:SpringBoot整合HighLevelClient实现登录统计
上级
c218a105
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
63 addition
and
34 deletion
+63
-34
youlai-admin/admin-boot/src/test/java/com/youlai/admin/ElasticSearchTests.java
...ot/src/test/java/com/youlai/admin/ElasticSearchTests.java
+7
-1
youlai-common/common-elasticsearch/pom.xml
youlai-common/common-elasticsearch/pom.xml
+2
-1
youlai-common/common-elasticsearch/src/main/java/com/youlai/common/elasticsearch/constant/EsConstants.java
...com/youlai/common/elasticsearch/constant/EsConstants.java
+2
-2
youlai-common/common-elasticsearch/src/main/java/com/youlai/common/elasticsearch/service/ElasticSearchService.java
...ai/common/elasticsearch/service/ElasticSearchService.java
+24
-10
youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/LoginLogAspect.java
...ain/java/com/youlai/common/web/aspect/LoginLogAspect.java
+17
-15
youlai-common/common-web/src/main/java/com/youlai/common/web/pojo/domain/LoginLog.java
...main/java/com/youlai/common/web/pojo/domain/LoginLog.java
+3
-3
youlai-common/common-web/src/main/resources/logback-spring.xml
...i-common/common-web/src/main/resources/logback-spring.xml
+8
-2
未找到文件。
youlai-admin/admin-boot/src/test/java/com/youlai/admin/ElasticSearchTests.java
浏览文件 @
efdbf422
package
com.youlai.admin
;
import
com.youlai.common.elasticsearch.service.ElasticSearchService
;
import
com.youlai.common.web.pojo.domain.LoginLog
;
import
lombok.extern.slf4j.Slf4j
;
import
org.elasticsearch.index.query.QueryBuilder
;
import
org.elasticsearch.index.query.QueryBuilders
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
java.util.List
;
@SpringBootTest
@Slf4j
public
class
ElasticSearchTests
{
...
...
@@ -15,6 +20,7 @@ public class ElasticSearchTests {
@Test
public
void
search
()
{
elasticSearchService
.
search
(
null
,
null
,
"logstash-login-2021.03.05"
);
List
<
LoginLog
>
list
=
elasticSearchService
.
search
(
null
,
LoginLog
.
class
,
"youlai-auth-login-2021-03-06"
);
System
.
out
.
println
(
list
.
toString
());
}
}
youlai-common/common-elasticsearch/pom.xml
浏览文件 @
efdbf422
...
...
@@ -39,10 +39,11 @@
<artifactId>
elasticsearch-rest-client
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-elasticsearch
</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
</project>
youlai-common/common-elasticsearch/src/main/java/com/youlai/common/elasticsearch/constant/E
lasticSearch
Constants.java
→
youlai-common/common-elasticsearch/src/main/java/com/youlai/common/elasticsearch/constant/E
s
Constants.java
浏览文件 @
efdbf422
...
...
@@ -4,8 +4,8 @@ package com.youlai.common.elasticsearch.constant;
* @author hxr
* @date 2021-03-05
*/
public
interface
E
lasticSearch
Constants
{
public
interface
E
s
Constants
{
int
DEFAULT_PAGE_SIZE
=
Integer
.
MAX_VALUE
;
int
DEFAULT_PAGE_SIZE
=
10
;
}
youlai-common/common-elasticsearch/src/main/java/com/youlai/common/elasticsearch/service/ElasticSearchService.java
浏览文件 @
efdbf422
...
...
@@ -2,13 +2,15 @@ package com.youlai.common.elasticsearch.service;
import
cn.hutool.core.collection.CollectionUtil
;
import
cn.hutool.json.JSONUtil
;
import
com.youlai.common.elasticsearch.constant.E
lasticSearch
Constants
;
import
com.youlai.common.elasticsearch.constant.E
s
Constants
;
import
lombok.AllArgsConstructor
;
import
lombok.SneakyThrows
;
import
org.elasticsearch.action.search.SearchRequest
;
import
org.elasticsearch.action.search.SearchResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.client.core.CountRequest
;
import
org.elasticsearch.client.core.CountResponse
;
import
org.elasticsearch.index.query.QueryBuilder
;
import
org.elasticsearch.search.SearchHit
;
import
org.elasticsearch.search.SearchHits
;
...
...
@@ -23,28 +25,40 @@ import java.util.List;
*/
@Service
@AllArgsConstructor
public
class
ElasticSearchService
<
T
>
{
public
class
ElasticSearchService
{
private
RestHighLevelClient
client
;
@SneakyThrows
public
SearchResponse
search
(
SearchRequest
searchRequest
)
{
SearchResponse
searchResponse
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
);
return
searchResponse
;
public
long
count
(
QueryBuilder
queryBuilder
,
String
...
indices
){
SearchSourceBuilder
searchSourceBuilder
=
new
SearchSourceBuilder
();
searchSourceBuilder
.
query
(
queryBuilder
);
CountRequest
countRequest
=
new
CountRequest
(
indices
);
countRequest
.
source
(
searchSourceBuilder
);
CountResponse
countResponse
=
client
.
count
(
countRequest
,
RequestOptions
.
DEFAULT
);
long
count
=
countResponse
.
getCount
();
return
count
;
}
@SneakyThrows
public
List
search
(
QueryBuilder
queryBuilder
,
Class
<
T
>
clazz
,
String
...
indexes
)
{
SearchRequest
searchRequest
=
new
SearchRequest
(
indexes
);
@SneakyThrows
public
<
T
>
List
<
T
>
search
(
QueryBuilder
queryBuilder
,
Class
<
T
>
clazz
,
String
...
indices
)
{
// 构造请求参数
SearchSourceBuilder
searchSourceBuilder
=
new
SearchSourceBuilder
();
searchSourceBuilder
.
query
(
queryBuilder
);
searchSourceBuilder
.
from
(
0
);
searchSourceBuilder
.
size
(
ElasticSearchConstants
.
DEFAULT_PAGE_SIZE
);
searchSourceBuilder
.
size
(
EsConstants
.
DEFAULT_PAGE_SIZE
);
SearchRequest
searchRequest
=
new
SearchRequest
(
indices
);
searchRequest
.
source
(
searchSourceBuilder
);
// 执行
搜索
请求
// 执行请求
SearchResponse
searchResponse
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
);
SearchHits
hits
=
searchResponse
.
getHits
();
SearchHit
[]
searchHits
=
hits
.
getHits
();
...
...
youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/LoginLogAspect.java
浏览文件 @
efdbf422
package
com.youlai.common.web.aspect
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.extra.servlet.ServletUtil
;
import
cn.hutool.json.JSONUtil
;
import
com.youlai.common.web.pojo.domain.LoginLog
;
...
...
@@ -11,6 +12,7 @@ import org.aspectj.lang.annotation.Around;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.aspectj.lang.reflect.MethodSignature
;
import
org.slf4j.MDC
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.context.request.RequestContextHolder
;
...
...
@@ -26,7 +28,7 @@ import javax.servlet.http.HttpServletRequest;
@Component
@AllArgsConstructor
@Slf4j
@ConditionalOnProperty
(
value
=
"spring.application.name"
,
havingValue
=
"youlai-auth"
)
@ConditionalOnProperty
(
value
=
"spring.application.name"
,
havingValue
=
"youlai-auth"
)
public
class
LoginLogAspect
{
@Pointcut
(
"execution(public * com.youlai.auth.controller.AuthController.postAccessToken(..))"
)
...
...
@@ -38,29 +40,29 @@ public class LoginLogAspect {
// 时间统计
long
startTime
=
System
.
currentTimeMillis
();
Object
result
=
joinPoint
.
proceed
();
long
endTime
=
System
.
currentTimeMillis
();
long
endTime
=
System
.
currentTimeMillis
();
long
elapsedTime
=
endTime
-
startTime
;
// 获取方法签名
MethodSignature
signature
=
(
MethodSignature
)
joinPoint
.
getSignature
();
MethodSignature
signature
=
(
MethodSignature
)
joinPoint
.
getSignature
();
String
description
=
signature
.
getMethod
().
getAnnotation
(
ApiOperation
.
class
).
value
();
// 获取请求信息
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletRequest
request
=
attributes
.
getRequest
();
String
requestIp
=
ServletUtil
.
getClientIP
(
request
);
String
requestUrl
=
request
.
getRequestURL
().
toString
();
String
requestMethod
=
request
.
getMethod
();
String
clientIP
=
ServletUtil
.
getClientIP
(
request
);
String
requestUrl
=
request
.
getRequestURL
().
toString
();
String
method
=
request
.
getMethod
();
MDC
.
put
(
"startTime"
,
StrUtil
.
toString
(
startTime
));
MDC
.
put
(
"elapsedTime"
,
StrUtil
.
toString
(
elapsedTime
));
MDC
.
put
(
"description"
,
description
);
MDC
.
put
(
"clientIP"
,
clientIP
);
MDC
.
put
(
"url"
,
requestUrl
);
MDC
.
put
(
"method"
,
method
);
log
.
info
(
StrUtil
.
toString
(
result
));
// logstash收集必要打印
LoginLog
loginLog
=
new
LoginLog
();
loginLog
.
setStartTime
(
startTime
);
loginLog
.
setElapsedTime
(
elapsedTime
);
loginLog
.
setDescription
(
description
);
loginLog
.
setRequestIp
(
requestIp
);
loginLog
.
setRequestUrl
(
requestUrl
);
loginLog
.
setRequestMethod
(
requestMethod
);
loginLog
.
setResult
(
result
);
log
.
info
(
JSONUtil
.
toJsonStr
(
loginLog
));
return
result
;
}
}
youlai-common/common-web/src/main/java/com/youlai/common/web/pojo/domain/LoginLog.java
浏览文件 @
efdbf422
...
...
@@ -12,11 +12,11 @@ public class LoginLog {
private
String
description
;
private
String
requestIp
;
private
String
clientIP
;
private
String
requestU
rl
;
private
String
u
rl
;
private
String
requestM
ethod
;
private
String
m
ethod
;
private
long
startTime
;
...
...
youlai-common/common-web/src/main/resources/logback-spring.xml
浏览文件 @
efdbf422
...
...
@@ -14,7 +14,7 @@
</encoder>
</appender>
<!-- Logstash收集日志输出到ElasticSearch -->
<!-- Logstash收集
登录
日志输出到ElasticSearch -->
<appender
name=
"LOGIN_LOGSTASH"
class=
"net.logstash.logback.appender.LogstashTcpSocketAppender"
>
<destination>
g.youlai.store:5044
</destination>
<encoder
charset=
"UTF-8"
class=
"net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder"
>
...
...
@@ -27,12 +27,18 @@
<pattern>
{
"project": "${APP_NAME}",
"action":"login",
"level": "%level",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger",
"message": "%message",
"action": "login"
<!-- 自定义字段 登录日志索引名 logstash-%{action}-yyyy.MM.dd -->
"startTime": "%X{startTime}",
"elapsedTime": "%X{elapsedTime}",
"description": "%X{description}",
"clientIP": "%X{clientIP}",
"url": "%X{url}",
"method": "%X{method}"
}
</pattern>
</pattern>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录