mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2025-01-03 17:42:20 +08:00
feat:删除登录记录
This commit is contained in:
parent
eb6a762e2e
commit
174d78a96d
@ -34,10 +34,12 @@ public class SearchController {
|
||||
})
|
||||
@GetMapping("/{name}")
|
||||
public Result searchByName(@PathVariable String name) {
|
||||
SearchSourceBuilder builder = new SearchSourceBuilder();
|
||||
/* SearchSourceBuilder builder = new SearchSourceBuilder();
|
||||
builder.query(QueryBuilders.matchQuery("name", name));
|
||||
List<PmsProduct> list = elasticSearchService.search(builder.query(), PmsProduct.class, "pms_spu");
|
||||
return Result.success(list);
|
||||
return Result.success(list);*/
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.youlai.admin.pojo.domain;
|
||||
|
||||
import com.youlai.common.base.BaseDocument;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
@ -7,9 +8,7 @@ import lombok.Data;
|
||||
* @date 2021-03-09
|
||||
*/
|
||||
@Data
|
||||
public class LoginRecord {
|
||||
|
||||
private String _id;
|
||||
public class LoginRecord extends BaseDocument {
|
||||
|
||||
private String description;
|
||||
|
||||
|
@ -95,7 +95,7 @@
|
||||
<version>1.0.0</version>
|
||||
<executions>
|
||||
<!--执行mvn package,即执行 mvn clean package docker:build-->
|
||||
<!--<execution>
|
||||
<!-- <execution>
|
||||
<id>build-image</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
|
@ -6,9 +6,9 @@ package com.youlai.admin.common.constant;
|
||||
*/
|
||||
public interface ESConstants {
|
||||
|
||||
String INDEX_LOGIN_PREFIX = "youlai-auth-login-";
|
||||
String LOGIN_INDEX_PREFIX = "youlai-auth-login-";
|
||||
|
||||
|
||||
String INDEX_LOGIN_PATTERN = "youlai-auth-login-*";
|
||||
String LOGIN_INDEX_PATTERN = "youlai-auth-login-*";
|
||||
|
||||
}
|
||||
|
@ -26,8 +26,8 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author hxr
|
||||
* @description 首页控制台
|
||||
* @author hxr
|
||||
* @date 2021-03-08
|
||||
*/
|
||||
@Api(tags = "首页控制台")
|
||||
@ -54,14 +54,13 @@ public class DashboardController {
|
||||
for (int i = 0; i < days; i++) {
|
||||
String date = now.plusDays(-i).format(formatter);
|
||||
xData[i] = date;
|
||||
indices[i] = ESConstants.INDEX_LOGIN_PREFIX + date;
|
||||
indices[i] = ESConstants.LOGIN_INDEX_PREFIX + date;
|
||||
}
|
||||
|
||||
// 查询条件,范围内日期统计
|
||||
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("date").from(startDate).to(endDate);
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
|
||||
.must(rangeQueryBuilder)
|
||||
/*.must(QueryBuilders.wildcardQuery("accessToken", "*"))*/; // 登录成功统计
|
||||
.must(rangeQueryBuilder);
|
||||
|
||||
// 总数统计
|
||||
Map<String, Long> totalCountMap = elasticSearchService.dateHistogram(
|
||||
@ -82,7 +81,7 @@ public class DashboardController {
|
||||
Long[] totalCount = new Long[days];
|
||||
Long[] myCount= new Long[days];
|
||||
|
||||
Arrays.sort(xData);// 默认升序排序
|
||||
Arrays.sort(xData);// 默认升序
|
||||
for (int i = 0; i < days; i++) {
|
||||
String key = xData[i];
|
||||
totalCount[i] = Convert.toLong(totalCountMap.get(key), 0l);
|
||||
|
@ -3,6 +3,7 @@ package com.youlai.admin.controller;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.youlai.admin.common.constant.ESConstants;
|
||||
import com.youlai.admin.pojo.domain.LoginRecord;
|
||||
import com.youlai.common.base.BaseDocument;
|
||||
import com.youlai.common.elasticsearch.service.ElasticSearchService;
|
||||
import com.youlai.common.result.Result;
|
||||
import io.swagger.annotations.Api;
|
||||
@ -18,6 +19,8 @@ import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -26,7 +29,7 @@ import java.util.List;
|
||||
*/
|
||||
@Api(tags = "登录记录")
|
||||
@RestController
|
||||
@RequestMapping("/api.admin/v1/records/login")
|
||||
@RequestMapping("/api.admin/v1/login_records")
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
public class LoginRecordController {
|
||||
@ -34,7 +37,6 @@ public class LoginRecordController {
|
||||
ElasticSearchService elasticSearchService;
|
||||
|
||||
|
||||
|
||||
@ApiOperation(value = "列表分页", httpMethod = "GET")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "Long"),
|
||||
@ -68,25 +70,22 @@ public class LoginRecordController {
|
||||
queryBuilder.must(QueryBuilders.wildcardQuery("clientIP", "*" + clientIP + "*"));
|
||||
}
|
||||
// 总记录数
|
||||
long count = elasticSearchService.count(queryBuilder, ESConstants.INDEX_LOGIN_PATTERN);
|
||||
long count = elasticSearchService.count(queryBuilder, ESConstants.LOGIN_INDEX_PATTERN);
|
||||
|
||||
// 排序
|
||||
FieldSortBuilder sortBuilder = new FieldSortBuilder("@timestamp").order(SortOrder.DESC);
|
||||
|
||||
// 分页数
|
||||
List<LoginRecord> list = elasticSearchService.search(queryBuilder, sortBuilder, page, limit, LoginRecord.class, ESConstants.INDEX_LOGIN_PATTERN);
|
||||
// 分页查询
|
||||
List<LoginRecord> list = elasticSearchService.search(queryBuilder, sortBuilder, page, limit, LoginRecord.class, ESConstants.LOGIN_INDEX_PATTERN);
|
||||
return Result.success(list, count);
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation(value = "删除登录记录", httpMethod = "DELETE")
|
||||
@ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String")
|
||||
@DeleteMapping("/{ids}")
|
||||
public Result delete(@PathVariable String ids) {
|
||||
return Result.judge(true);
|
||||
@DeleteMapping
|
||||
public Result delete(@RequestBody List<BaseDocument> documents) {
|
||||
documents.forEach(document -> elasticSearchService.deleteById(document.getId(), document.getIndex()));
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -155,13 +155,13 @@
|
||||
<version>1.0.0</version>
|
||||
<executions>
|
||||
<!--执行mvn package,即执行 mvn clean package docker:build-->
|
||||
<!--<execution>
|
||||
<execution>
|
||||
<id>build-image</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>build</goal>
|
||||
</goals>
|
||||
</execution>-->
|
||||
</execution>
|
||||
</executions>
|
||||
|
||||
<configuration>
|
||||
|
@ -0,0 +1,22 @@
|
||||
package com.youlai.common.base;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @description document 是 ES 里的一个 JSON 对象,包括零个或多个field,类比关系数据库的一行记录
|
||||
* @author haoxr
|
||||
* @createTime 2021/3/9 22:14
|
||||
*/
|
||||
@Data
|
||||
public class BaseDocument {
|
||||
|
||||
/**
|
||||
* 数据唯一标识
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 索引名称
|
||||
*/
|
||||
private String index;
|
||||
}
|
@ -13,6 +13,11 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.youlai</groupId>
|
||||
<artifactId>common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||
|
@ -2,9 +2,12 @@ package com.youlai.common.elasticsearch.service;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.youlai.common.base.BaseDocument;
|
||||
import com.youlai.common.elasticsearch.constant.ESConstants;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.delete.DeleteResponse;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
@ -12,7 +15,12 @@ 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.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.index.query.TermQueryBuilder;
|
||||
import org.elasticsearch.index.reindex.BulkByScrollResponse;
|
||||
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.SearchHits;
|
||||
import org.elasticsearch.search.aggregations.AggregationBuilder;
|
||||
@ -24,6 +32,7 @@ import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.search.sort.SortBuilder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@ -105,20 +114,21 @@ public class ElasticSearchService {
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public <T> List<T> search(QueryBuilder queryBuilder, Class<T> clazz, String... indices) {
|
||||
public <T extends BaseDocument> List<T> search(QueryBuilder queryBuilder, Class<T> clazz, String... indices) {
|
||||
List<T> list = this.search(queryBuilder, null, 1, ESConstants.DEFAULT_PAGE_SIZE, clazz, indices);
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public <T> List<T> search(QueryBuilder queryBuilder, Integer page, Integer size, Class<T> clazz, String... indices) {
|
||||
public <T extends BaseDocument> List<T> search(QueryBuilder queryBuilder, Integer page, Integer size, Class<T> clazz, String... indices) {
|
||||
|
||||
List<T> list = this.search(queryBuilder, null, 1, ESConstants.DEFAULT_PAGE_SIZE, clazz, indices);
|
||||
return list;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public <T> List<T> search(QueryBuilder queryBuilder, SortBuilder sortBuilder, Integer page, Integer size, Class<T> clazz, String... indices) {
|
||||
public <T extends BaseDocument> List<T> search(QueryBuilder queryBuilder, SortBuilder sortBuilder, Integer page, Integer size, Class<T> clazz, String... indices) {
|
||||
// 构造SearchSourceBuilder
|
||||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||
searchSourceBuilder.query(queryBuilder);
|
||||
@ -136,9 +146,18 @@ public class ElasticSearchService {
|
||||
List<T> list = CollectionUtil.newArrayList();
|
||||
for (SearchHit hit : searchHits) {
|
||||
T t = JSONUtil.toBean(hit.getSourceAsString(), clazz);
|
||||
t.setId(hit.getId()); // 数据的唯一标识
|
||||
t.setIndex(hit.getIndex());// 索引
|
||||
list.add(t);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public boolean deleteById(String id, String index) {
|
||||
DeleteRequest deleteRequest = new DeleteRequest(index,id);
|
||||
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -50,7 +50,6 @@
|
||||
<artifactId>ip2region</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.dozermapper</groupId>
|
||||
<artifactId>dozer-core</artifactId>
|
||||
|
@ -74,6 +74,7 @@ public class LoginLogAspect {
|
||||
token = jsonObject.getStr("value");
|
||||
}
|
||||
String clientIP = IPUtils.getClientIP(request); // 客户端请求IP(注意:如果使用Nginx代理需配置)
|
||||
log.info("222. {}",clientIP);
|
||||
String region = IPUtils.ip2region(clientIP); // IP对应的城市信息
|
||||
|
||||
// MDC 扩展logback字段,具体请看logback-spring.xml的自定义日志输出格式
|
||||
|
@ -91,23 +91,24 @@ public class IPUtils {
|
||||
return Strings.EMPTY;
|
||||
}
|
||||
|
||||
log.info("1. {}",ip);
|
||||
if(LOCAL_IP.equals(ip)){
|
||||
return "本地访问";
|
||||
}
|
||||
|
||||
log.info("2. {}",ip);
|
||||
String filePath = IPUtils.class.getResource("/data/ip2region.db").getPath();
|
||||
File file = new File(filePath);
|
||||
if (file.exists() == false) {
|
||||
return Strings.EMPTY;
|
||||
}
|
||||
|
||||
log.info("3. {}",ip);
|
||||
try {
|
||||
DbConfig config = new DbConfig();
|
||||
DbSearcher searcher = new DbSearcher(config, filePath);
|
||||
|
||||
log.info("4. {}",ip);
|
||||
Method method = searcher.getClass().getMethod("btreeSearch", String.class);
|
||||
DataBlock dataBlock = (DataBlock) method.invoke(searcher, ip);
|
||||
|
||||
log.info("5. {}",ip);
|
||||
return dataBlock.getRegion();
|
||||
|
||||
} catch (Exception e) {
|
||||
@ -117,4 +118,5 @@ public class IPUtils {
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -128,13 +128,13 @@
|
||||
<version>1.0.0</version>
|
||||
<executions>
|
||||
<!--执行mvn package,即执行 mvn clean package docker:build-->
|
||||
<execution>
|
||||
<!--<execution>
|
||||
<id>build-image</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>build</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</execution>-->
|
||||
</executions>
|
||||
|
||||
<configuration>
|
||||
|
Loading…
Reference in New Issue
Block a user