Skip to content

Commit

Permalink
日志加入加入IP来源,支持多字段模糊搜索,升级七牛云存储版本
Browse files Browse the repository at this point in the history
  • Loading branch information
elunez committed Aug 22, 2019
1 parent 1b574b5 commit e471a9d
Show file tree
Hide file tree
Showing 33 changed files with 205 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
*/
Join join() default Join.LEFT;

/**
* 多字段模糊搜索,仅支持String类型字段,多个用逗号隔开, 如@Query(blurry = "email,username")
* @return
*/
String blurry() default "";

enum Type {
/** jie 2019/6/4 相等 */
EQUAL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public class ElAdminConstant {

public static final String RESET_MAIL = "重置邮箱";

/**
* 用于IP定位转换
*/
public static final String REGION = "内网IP|内网IP";

/**
* 常用接口
*/
Expand Down
27 changes: 25 additions & 2 deletions eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import cn.hutool.core.util.IdUtil;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;

import java.io.*;
import java.text.DecimalFormat;

/**
Expand Down Expand Up @@ -116,4 +116,27 @@ public static String getSize(int size){
}
return resultSize;
}

/**
* inputStream 转 File
* @param ins
* @param name
* @return
* @throws Exception
*/
public static File inputStreamToFile(InputStream ins, String name) throws Exception{
File file = new File(System.getProperty("java.io.tmpdir") + name);
if (file.exists()) {
return file;
}
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
return file;
}
}
13 changes: 13 additions & 0 deletions eladmin-common/src/main/java/me/zhengjie/utils/QueryHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,26 @@ public static <R, Q> Predicate getPredicate(Root<R> root, Q query, CriteriaBuild
if (q != null) {
String propName = q.propName();
String joinName = q.joinName();
String blurry = q.blurry();
String attributeName = isBlank(propName) ? field.getName() : propName;
Class<?> fieldType = field.getType();
Object val = field.get(query);
if (ObjectUtil.isNull(val)) {
continue;
}
Join join = null;
// 模糊多字段
if (ObjectUtil.isNotEmpty(blurry)) {
String[] blurrys = blurry.split(",");
List<Predicate> orPredicate = new ArrayList<>();
for (String s : blurrys) {
orPredicate.add(cb.like(root.get(s)
.as(String.class), "%" + val.toString() + "%"));
}
Predicate[] p = new Predicate[orPredicate.size()];
list.add(cb.or(orPredicate.toArray(p)));
continue;
}
if (ObjectUtil.isNotEmpty(joinName)) {
switch (q.join()) {
case LEFT:
Expand Down
51 changes: 50 additions & 1 deletion eladmin-common/src/main/java/me/zhengjie/utils/StringUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package me.zhengjie.utils;

import cn.hutool.core.io.resource.ClassPathResource;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Date;

Expand Down Expand Up @@ -132,7 +139,49 @@ public static String getIP(HttpServletRequest request) {
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return "0:0:0:0:0:0:0:1".equals(ip)?"127.0.0.1":ip;
String[] ips = ip.split(",");
return "0:0:0:0:0:0:0:1".equals(ips[0])?"127.0.0.1":ips[0];
}

/**
* 根据ip获取详细地址
* @param ip
* @return
*/
public static String getCityInfo(String ip) {
try {
String path = "ip2region/ip2region.db";
String name = "ip2region.db";
int algorithm = DbSearcher.BTREE_ALGORITHM;
DbConfig config = new DbConfig();
File file = FileUtil.inputStreamToFile(new ClassPathResource(path).getStream(), name);
DbSearcher searcher = new DbSearcher(config, file.getPath());
Method method = null;
switch (algorithm) {
case DbSearcher.BTREE_ALGORITHM:
method = searcher.getClass().getMethod("btreeSearch", String.class);
break;
case DbSearcher.BINARY_ALGORITHM:
method = searcher.getClass().getMethod("binarySearch", String.class);
break;
case DbSearcher.MEMORY_ALGORITYM:
method = searcher.getClass().getMethod("memorySearch", String.class);
break;
default:
method = searcher.getClass().getMethod("memorySearch", String.class);
break;
}
DataBlock dataBlock = null;
dataBlock = (DataBlock) method.invoke(searcher, ip);
String address = dataBlock.getRegion().replace("0|","");
if(address.charAt(address.length()-1) == '|'){
address = address.substring(0,address.length() - 1);
}
return address.equals(ElAdminConstant.REGION)?"内网IP":address;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

/**
Expand Down
3 changes: 3 additions & 0 deletions eladmin-logging/src/main/java/me/zhengjie/domain/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public class Log implements Serializable {
@Column(name = "request_ip")
private String requestIp;

@Column(name = "address")
private String address;

/**
* 请求耗时
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ResponseEntity getLogs(LogQueryCriteria criteria, Pageable pageable){
@GetMapping(value = "/logs/user")
public ResponseEntity getUserLogs(LogQueryCriteria criteria, Pageable pageable){
criteria.setLogType("INFO");
criteria.setUsername(SecurityUtils.getUsername());
criteria.setBlurry(SecurityUtils.getUsername());
return new ResponseEntity(logService.queryAllByUser(criteria,pageable), HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class LogErrorDTO implements Serializable {
*/
private String requestIp;

private String address;


/**
* 创建日期
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
@Data
public class LogQueryCriteria {

@Query(type = Query.Type.INNER_LIKE)
private String username;
// 多字段模糊
@Query(blurry = "username,description,address,requestIp,method,params")
private String blurry;

@Query
private String logType;

@Query(type = Query.Type.INNER_LIKE)
private String description;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class LogSmallDTO implements Serializable {
*/
private Long time;

private String address;

/**
* 创建日期
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import me.zhengjie.service.mapper.LogSmallMapper;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -92,6 +93,7 @@ public void save(String username, String ip, ProceedingJoinPoint joinPoint, Log
e.printStackTrace();
}
}
log.setAddress(StringUtils.getCityInfo(log.getRequestIp()));
log.setMethod(methodName);
log.setUsername(username);
log.setParams(params + " }");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import me.zhengjie.modules.system.domain.Dict;
import me.zhengjie.modules.system.service.DictService;
import me.zhengjie.modules.system.service.dto.DictDTO;
import me.zhengjie.modules.system.service.dto.DictQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
Expand All @@ -29,7 +30,7 @@ public class DictController {
@Log("查询字典")
@GetMapping(value = "/dict")
@PreAuthorize("hasAnyRole('ADMIN','DICT_ALL','DICT_SELECT')")
public ResponseEntity getDicts(DictDTO resources, Pageable pageable){
public ResponseEntity getDicts(DictQueryCriteria resources, Pageable pageable){
return new ResponseEntity(dictService.queryAll(resources,pageable),HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import me.zhengjie.modules.system.service.MenuService;
import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.CommonQueryCriteria;
import me.zhengjie.modules.system.service.dto.MenuDTO;
import me.zhengjie.modules.system.service.dto.MenuQueryCriteria;
import me.zhengjie.modules.system.service.dto.UserDTO;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -62,7 +62,7 @@ public ResponseEntity getMenuTree(){
@Log("查询菜单")
@GetMapping(value = "/menus")
@PreAuthorize("hasAnyRole('ADMIN','MENU_ALL','MENU_SELECT')")
public ResponseEntity getMenus(CommonQueryCriteria criteria){
public ResponseEntity getMenus(MenuQueryCriteria criteria){
List<MenuDTO> menuDTOList = menuService.queryAll(criteria);
return new ResponseEntity(menuService.buildTree(menuDTOList),HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import me.zhengjie.modules.system.domain.Permission;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.service.PermissionService;
import me.zhengjie.modules.system.service.dto.CommonQueryCriteria;
import me.zhengjie.modules.system.service.dto.PermissionDTO;
import me.zhengjie.modules.system.service.dto.PermissionQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -41,7 +41,7 @@ public ResponseEntity getTree(){
@Log("查询权限")
@GetMapping(value = "/permissions")
@PreAuthorize("hasAnyRole('ADMIN','PERMISSION_ALL','PERMISSION_SELECT')")
public ResponseEntity getPermissions(CommonQueryCriteria criteria){
public ResponseEntity getPermissions(PermissionQueryCriteria criteria){
List<PermissionDTO> permissionDTOS = permissionService.queryAll(criteria);
return new ResponseEntity(permissionService.buildTree(permissionDTOS),HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.dto.CommonQueryCriteria;
import me.zhengjie.modules.system.service.dto.MenuQueryCriteria;
import me.zhengjie.modules.system.service.dto.RoleQueryCriteria;
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -58,7 +59,7 @@ public ResponseEntity getAll(@PageableDefault(value = 2000, sort = {"level"}, di
@Log("查询角色")
@GetMapping(value = "/roles")
@PreAuthorize("hasAnyRole('ADMIN','ROLES_ALL','ROLES_SELECT')")
public ResponseEntity getRoles(CommonQueryCriteria criteria, Pageable pageable){
public ResponseEntity getRoles(RoleQueryCriteria criteria, Pageable pageable){
return new ResponseEntity(roleService.queryAll(criteria,pageable),HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.zhengjie.modules.system.domain.Dict;
import me.zhengjie.modules.system.service.dto.DictDTO;
import me.zhengjie.modules.system.service.dto.DictQueryCriteria;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
Expand All @@ -21,7 +22,7 @@ public interface DictService {
* @return
*/
@Cacheable(keyGenerator = "keyGenerator")
Object queryAll(DictDTO dict, Pageable pageable);
Object queryAll(DictQueryCriteria dict, Pageable pageable);

/**
* findById
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package me.zhengjie.modules.system.service;

import me.zhengjie.modules.system.domain.Menu;
import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.modules.system.service.dto.CommonQueryCriteria;
import me.zhengjie.modules.system.service.dto.MenuDTO;
import me.zhengjie.modules.system.service.dto.MenuQueryCriteria;
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author Zheng Jie
Expand All @@ -25,7 +23,7 @@ public interface MenuService {
* @return
*/
@Cacheable(keyGenerator = "keyGenerator")
List<MenuDTO> queryAll(CommonQueryCriteria criteria);
List<MenuDTO> queryAll(MenuQueryCriteria criteria);

/**
* get
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package me.zhengjie.modules.system.service;

import me.zhengjie.modules.system.domain.Permission;
import me.zhengjie.modules.system.service.dto.CommonQueryCriteria;
import me.zhengjie.modules.system.service.dto.PermissionDTO;
import me.zhengjie.modules.system.service.dto.PermissionQueryCriteria;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import java.util.Collection;
import java.util.List;

/**
Expand Down Expand Up @@ -76,5 +75,5 @@ public interface PermissionService {
* @return
*/
@Cacheable(keyGenerator = "keyGenerator")
List<PermissionDTO> queryAll(CommonQueryCriteria criteria);
List<PermissionDTO> queryAll(PermissionQueryCriteria criteria);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import me.zhengjie.modules.system.domain.Menu;
import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.modules.system.service.dto.CommonQueryCriteria;
import me.zhengjie.modules.system.service.dto.RoleDTO;
import me.zhengjie.modules.system.service.dto.RoleQueryCriteria;
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
Expand Down Expand Up @@ -94,5 +94,5 @@ public interface RoleService {
* @param criteria
* @return
*/
Object queryAll(CommonQueryCriteria criteria, Pageable pageable);
Object queryAll(RoleQueryCriteria criteria, Pageable pageable);
}
Loading

0 comments on commit e471a9d

Please sign in to comment.