Commit e471a9da authored by dqjdda's avatar dqjdda
Browse files

日志加入加入IP来源,支持多字段模糊搜索,升级七牛云存储版本

parent 1b574b59
......@@ -30,6 +30,12 @@ public @interface Query {
*/
Join join() default Join.LEFT;
/**
* 多字段模糊搜索,仅支持String类型字段,多个用逗号隔开, 如@Query(blurry = "email,username")
* @return
*/
String blurry() default "";
enum Type {
/** jie 2019/6/4 相等 */
EQUAL
......
......@@ -11,6 +11,11 @@ public class ElAdminConstant {
public static final String RESET_MAIL = "重置邮箱";
/**
* 用于IP定位转换
*/
public static final String REGION = "内网IP|内网IP";
/**
* 常用接口
*/
......
......@@ -2,8 +2,8 @@ package me.zhengjie.utils;
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;
/**
......@@ -116,4 +116,27 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
}
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;
}
}
......@@ -37,6 +37,7 @@ public class QueryHelp {
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);
......@@ -44,6 +45,18 @@ public class QueryHelp {
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:
......
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;
......@@ -132,7 +139,49 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
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 "";
}
/**
......
......@@ -54,6 +54,9 @@ public class Log implements Serializable {
@Column(name = "request_ip")
private String requestIp;
@Column(name = "address")
private String address;
/**
* 请求耗时
*/
......
......@@ -34,7 +34,7 @@ public class LogController {
@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);
}
......
......@@ -38,6 +38,8 @@ public class LogErrorDTO implements Serializable {
*/
private String requestIp;
private String address;
/**
* 创建日期
......
......@@ -11,12 +11,10 @@ import me.zhengjie.annotation.Query;
@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;
}
......@@ -27,6 +27,8 @@ public class LogSmallDTO implements Serializable {
*/
private Long time;
private String address;
/**
* 创建日期
*/
......
......@@ -10,6 +10,7 @@ import me.zhengjie.service.mapper.LogErrorMapper;
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;
......@@ -92,6 +93,7 @@ public class LogServiceImpl implements LogService {
e.printStackTrace();
}
}
log.setAddress(StringUtils.getCityInfo(log.getRequestIp()));
log.setMethod(methodName);
log.setUsername(username);
log.setParams(params + " }");
......
......@@ -5,6 +5,7 @@ import me.zhengjie.exception.BadRequestException;
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;
......@@ -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);
}
......
......@@ -6,8 +6,8 @@ import me.zhengjie.exception.BadRequestException;
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;
......@@ -62,7 +62,7 @@ public class MenuController {
@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);
}
......
......@@ -4,8 +4,8 @@ import me.zhengjie.aop.log.Log;
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;
......@@ -41,7 +41,7 @@ public class PermissionController {
@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);
}
......
......@@ -5,7 +5,8 @@ import me.zhengjie.aop.log.Log;
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;
......@@ -58,7 +59,7 @@ public class RoleController {
@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);
}
......
......@@ -2,6 +2,7 @@ package me.zhengjie.modules.system.service;
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;
......@@ -21,7 +22,7 @@ public interface DictService {
* @return
*/
@Cacheable(keyGenerator = "keyGenerator")
Object queryAll(DictDTO dict, Pageable pageable);
Object queryAll(DictQueryCriteria dict, Pageable pageable);
/**
* findById
......
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
......@@ -25,7 +23,7 @@ public interface MenuService {
* @return
*/
@Cacheable(keyGenerator = "keyGenerator")
List<MenuDTO> queryAll(CommonQueryCriteria criteria);
List<MenuDTO> queryAll(MenuQueryCriteria criteria);
/**
* get
......
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;
/**
......@@ -76,5 +75,5 @@ public interface PermissionService {
* @return
*/
@Cacheable(keyGenerator = "keyGenerator")
List<PermissionDTO> queryAll(CommonQueryCriteria criteria);
List<PermissionDTO> queryAll(PermissionQueryCriteria criteria);
}
......@@ -2,8 +2,8 @@ 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.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;
......@@ -94,5 +94,5 @@ public interface RoleService {
* @param criteria
* @return
*/
Object queryAll(CommonQueryCriteria criteria, Pageable pageable);
Object queryAll(RoleQueryCriteria criteria, Pageable pageable);
}
package me.zhengjie.modules.system.service.dto;
import lombok.Data;
import me.zhengjie.annotation.Query;
import java.io.Serializable;
/**
......@@ -17,12 +15,10 @@ public class DictDTO implements Serializable {
/**
* 字典名称
*/
@Query(type = Query.Type.INNER_LIKE)
private String name;
/**
* 描述
*/
@Query(type = Query.Type.INNER_LIKE)
private String remark;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment