开发操作日志查询功能.

pull/10/head
chen.ma 3 years ago
parent a53d857503
commit 5acd0ab7f8

@ -1,9 +1,10 @@
package cn.hippo4j.auth.filter;
import com.alibaba.fastjson.JSON;
import cn.hippo4j.auth.toolkit.JwtTokenUtil;
import cn.hippo4j.common.toolkit.UserContext;
import cn.hippo4j.common.web.base.Results;
import cn.hippo4j.common.web.exception.ServiceException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@ -53,6 +54,7 @@ public class JWTAuthorizationFilter extends BasicAuthenticationFilter {
response.getWriter().flush();
return;
}
super.doFilterInternal(request, response, chain);
}
@ -70,6 +72,8 @@ public class JWTAuthorizationFilter extends BasicAuthenticationFilter {
}
String username = JwtTokenUtil.getUsername(token);
UserContext.setUserName(username);
String role = JwtTokenUtil.getUserRole(token);
if (username != null) {
return new UsernamePasswordAuthenticationToken(username, null,

@ -0,0 +1,21 @@
package cn.hippo4j.common.toolkit;
/**
* User context (Transition scheme).
*
* @author chen.ma
* @date 2021/11/17 21:13
*/
public class UserContext {
private static String username;
public static void setUserName(String username) {
UserContext.username = username;
}
public static String getUserName() {
return username;
}
}

@ -1,5 +1,6 @@
package cn.hippo4j.config.model;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
@ -51,4 +52,9 @@ public class ConfigAllInfo extends ConfigInfo implements PoolParameter {
@TableField(fill = FieldFill.INSERT)
private Integer delFlag;
@Override
public String toString() {
return JSON.toJSONString(this);
}
}

@ -0,0 +1,30 @@
package cn.hippo4j.config.model.biz.log;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
/**
* .
*
* @author chen.ma
* @date 2021/11/17 21:43
*/
@Data
public class LogRecordQueryReqDTO extends Page {
/**
*
*/
private String bizNo;
/**
*
*/
private String category;
/**
*
*/
private String operator;
}

@ -0,0 +1,42 @@
package cn.hippo4j.config.model.biz.log;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
/**
* .
*
* @author chen.ma
* @date 2021/11/17 21:37
*/
@Data
public class LogRecordRespDTO {
/**
*
*/
private String bizNo;
/**
*
*/
private String action;
/**
*
*/
private String operator;
/**
*
*/
private String category;
/**
* gmtCreate
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
}

@ -0,0 +1,23 @@
package cn.hippo4j.config.service.biz;
import cn.hippo4j.config.model.biz.log.LogRecordQueryReqDTO;
import cn.hippo4j.config.model.biz.log.LogRecordRespDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
/**
* .
*
* @author chen.ma
* @date 2021/11/17 21:41
*/
public interface LogRecordBizService {
/**
* .
*
* @param pageQuery
* @return
*/
IPage<LogRecordRespDTO> queryPage(LogRecordQueryReqDTO pageQuery);
}

@ -1,19 +1,22 @@
package cn.hippo4j.config.service.biz.impl;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.toolkit.ConditionUtil;
import cn.hippo4j.common.toolkit.ContentUtil;
import cn.hippo4j.common.toolkit.Md5Util;
import cn.hippo4j.common.toolkit.UserContext;
import cn.hippo4j.config.event.LocalDataChangeEvent;
import cn.hippo4j.config.mapper.ConfigInfoMapper;
import cn.hippo4j.config.model.ConfigAllInfo;
import cn.hippo4j.config.model.ConfigInfoBase;
import cn.hippo4j.config.service.ConfigChangePublisher;
import cn.hippo4j.config.service.biz.ConfigService;
import cn.hippo4j.tools.logrecord.annotation.LogRecord;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import cn.hippo4j.common.toolkit.ConditionUtil;
import cn.hippo4j.common.toolkit.ContentUtil;
import cn.hippo4j.common.toolkit.Md5Util;
import cn.hippo4j.config.service.biz.ConfigService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@ -50,21 +53,24 @@ public class ConfigServiceImpl implements ConfigService {
.eq(ConfigInfoBase::getTpId, configInfo.getTpId());
ConfigAllInfo existConfig = configInfoMapper.selectOne(queryWrapper);
String userName = UserContext.getUserName();
ConfigServiceImpl configService = ApplicationContextHolder.getBean(this.getClass());
try {
ConditionUtil
.condition(
existConfig == null,
() -> addConfigInfo(configInfo),
() -> updateConfigInfo(configInfo)
() -> configService.addConfigInfo(configInfo),
() -> configService.updateConfigInfo(userName, configInfo)
);
} catch (Exception ex) {
updateConfigInfo(configInfo);
updateConfigInfo(userName, configInfo);
}
ConfigChangePublisher.notifyConfigChange(new LocalDataChangeEvent(identify, ContentUtil.getGroupKey(configInfo)));
}
private Integer addConfigInfo(ConfigAllInfo config) {
public Integer addConfigInfo(ConfigAllInfo config) {
config.setContent(ContentUtil.getPoolContent(config));
config.setMd5(Md5Util.getTpContentMd5(config));
@ -80,7 +86,14 @@ public class ConfigServiceImpl implements ConfigService {
return null;
}
private void updateConfigInfo(ConfigAllInfo config) {
@LogRecord(
bizNo = "{{#config.itemId}}:{{#config.tpId}}",
category = "THREAD_POOL_UPDATE",
operator = "{{#operator}}",
success = "核心线程: {{#config.coreSize}}, 最大线程: {{#config.maxSize}}, 队列类型: {{#config.queueType}}, 队列容量: {{#config.capacity}}, 拒绝策略: {{#config.rejectedType}}",
detail = "{{#config.toString()}}"
)
public void updateConfigInfo(String operator, ConfigAllInfo config) {
LambdaUpdateWrapper<ConfigAllInfo> wrapper = Wrappers.lambdaUpdate(ConfigAllInfo.class)
.eq(ConfigAllInfo::getTpId, config.getTpId())
.eq(ConfigAllInfo::getItemId, config.getItemId())

@ -0,0 +1,39 @@
package cn.hippo4j.config.service.biz.impl;
import cn.hippo4j.config.mapper.LogRecordMapper;
import cn.hippo4j.config.model.biz.log.LogRecordQueryReqDTO;
import cn.hippo4j.config.model.biz.log.LogRecordRespDTO;
import cn.hippo4j.config.service.biz.LogRecordBizService;
import cn.hippo4j.config.toolkit.BeanUtil;
import cn.hippo4j.tools.logrecord.model.LogRecordInfo;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
/**
* .
*
* @author chen.ma
* @date 2021/11/17 21:50
*/
@Service
@AllArgsConstructor
public class LogRecordBizServiceImpl implements LogRecordBizService {
private final LogRecordMapper logRecordMapper;
@Override
public IPage<LogRecordRespDTO> queryPage(LogRecordQueryReqDTO pageQuery) {
LambdaQueryWrapper<LogRecordInfo> queryWrapper = Wrappers.lambdaQuery(LogRecordInfo.class)
.eq(StrUtil.isNotBlank(pageQuery.getBizNo()), LogRecordInfo::getBizNo, pageQuery.getBizNo())
.eq(StrUtil.isNotBlank(pageQuery.getCategory()), LogRecordInfo::getCategory, pageQuery.getCategory())
.eq(StrUtil.isNotBlank(pageQuery.getOperator()), LogRecordInfo::getOperator, pageQuery.getOperator())
.orderByDesc(LogRecordInfo::getCreateTime);
IPage<LogRecordInfo> selectPage = logRecordMapper.selectPage(pageQuery, queryWrapper);
return selectPage.convert(each -> BeanUtil.convert(each, LogRecordRespDTO.class));
}
}

@ -0,0 +1,34 @@
package cn.hippo4j.console.controller;
import cn.hippo4j.common.constant.Constants;
import cn.hippo4j.common.web.base.Result;
import cn.hippo4j.common.web.base.Results;
import cn.hippo4j.config.model.biz.log.LogRecordQueryReqDTO;
import cn.hippo4j.config.model.biz.log.LogRecordRespDTO;
import cn.hippo4j.config.service.biz.LogRecordBizService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Log record controller.
*
* @author chen.ma
* @date 2021/11/17 21:10
*/
@RestController
@AllArgsConstructor
@RequestMapping(Constants.BASE_PATH + "/log")
public class LogRecordController {
private final LogRecordBizService logRecordBizService;
@PostMapping("/query/page")
public Result<IPage<LogRecordRespDTO>> queryPage(@RequestBody LogRecordQueryReqDTO queryReqDTO) {
return Results.success(logRecordBizService.queryPage(queryReqDTO));
}
}
Loading…
Cancel
Save