♻️ Refactoring code. 优化登录日志记录时间

This commit is contained in:
lbw committed 2022-08-21 11:40:15 +08:00
1 parent ef57c77719
commit a77bc1a099
7 files changed
+53 -3

No files matched your search

@@ -17,8 +17,10 @@
package com.pig4cloud.pigx.auth.handler;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
import com.pig4cloud.pigx.admin.api.feign.RemoteLogService;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.KeyStrResolver;
import com.pig4cloud.pigx.common.core.util.WebUtils;
@@ -65,6 +67,13 @@ public class PigxAuthenticationFailureLogEventHandler implements AuthenticationF
HttpServletRequest request, HttpServletResponse response) {
String username = authentication.getName();
SysLogDTO sysLog = SysLogUtils.getSysLog(request, username);
String startTimeStr = request.getHeader(CommonConstants.REQUEST_START_TIME);
if (StrUtil.isNotBlank(startTimeStr)) {
Long startTime = Long.parseLong(startTimeStr);
Long endTime = System.currentTimeMillis();
sysLog.setTime(endTime - startTime);
}
sysLog.setTitle(username + "用户登录");
sysLog.setLogType(LogTypeEnum.ERROR.getType());
sysLog.setParams(username);
@@ -17,8 +17,10 @@
package com.pig4cloud.pigx.auth.handler;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
import com.pig4cloud.pigx.admin.api.feign.RemoteLogService;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.log.util.SysLogUtils;
import com.pig4cloud.pigx.common.security.handler.AuthenticationLogoutHandler;
@@ -64,6 +66,13 @@ public class PigxAuthenticationLogoutEventHandler implements AuthenticationLogou
sysLog.setTitle(pigxUser.getUsername() + "用户登录");
sysLog.setParams(pigxUser.getUsername());
String startTimeStr = request.getHeader(CommonConstants.REQUEST_START_TIME);
if (StrUtil.isNotBlank(startTimeStr)) {
Long startTime = Long.parseLong(startTimeStr);
Long endTime = System.currentTimeMillis();
sysLog.setTime(endTime - startTime);
}
// 获取clientId 信息
OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication;
sysLog.setServiceId(auth2Authentication.getOAuth2Request().getClientId());
@@ -17,8 +17,10 @@
package com.pig4cloud.pigx.auth.handler;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
import com.pig4cloud.pigx.admin.api.feign.RemoteLogService;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.KeyStrResolver;
import com.pig4cloud.pigx.common.core.util.WebUtils;
@@ -64,6 +66,14 @@ public class PigxAuthenticationSuccessLogEventHandler implements AuthenticationS
SysLogDTO sysLog = SysLogUtils.getSysLog(request, username);
sysLog.setTitle(username + "用户登录");
sysLog.setParams(username);
String startTimeStr = request.getHeader(CommonConstants.REQUEST_START_TIME);
if (StrUtil.isNotBlank(startTimeStr)) {
Long startTime = Long.parseLong(startTimeStr);
Long endTime = System.currentTimeMillis();
sysLog.setTime(endTime - startTime);
}
String header = request.getHeader(HttpHeaders.AUTHORIZATION);
sysLog.setServiceId(WebUtils.extractClientId(header).orElse("N/A"));
sysLog.setTenantId(Long.parseLong(tenantKeyStrResolver.key()));
@@ -120,4 +120,9 @@ public interface CommonConstants {
*/
String ONLINE_QUANTITY = "online_quantity";
/**
* 请求开始时间
*/
String REQUEST_START_TIME = "REQUEST-START-TIME";
}
@@ -46,7 +46,8 @@ public class GlobalExceptionConfiguration implements ErrorWebExceptionHandler {
return response.writeWith(Mono.fromSupplier(() -> {
DataBufferFactory bufferFactory = response.bufferFactory();
try {
log.error("Error Spring Cloud Gateway : {}", ex.getMessage());
log.error("Error rquest :{} Error Spring Cloud Gateway : {}", exchange.getRequest().getPath(),
ex.getMessage());
return bufferFactory.wrap(objectMapper.writeValueAsBytes(R.failed(ex.getMessage())));
}
catch (JsonProcessingException e) {
@@ -17,6 +17,7 @@
package com.pig4cloud.pigx.gateway.filter;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
@@ -28,6 +29,7 @@ import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Collectors;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
@@ -56,8 +58,12 @@ public class PigxRequestGlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// 1. 清洗请求头中from 参数
ServerHttpRequest request = exchange.getRequest().mutate()
.headers(httpHeaders -> httpHeaders.remove(SecurityConstants.FROM)).build();
ServerHttpRequest request = exchange.getRequest().mutate().headers(httpHeaders -> {
httpHeaders.remove(SecurityConstants.FROM);
// 设置请求时间
httpHeaders.put(CommonConstants.REQUEST_START_TIME,
Collections.singletonList(String.valueOf(System.currentTimeMillis())));
}).build();
// 2. 重写StripPrefix
addOriginalRequestUrl(exchange, request.getURI());
@@ -22,6 +22,7 @@ import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.druid.DruidConfig;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.codegen.config.MagicApiConfiguration;
import com.pig4cloud.pigx.codegen.entity.GenDatasourceConf;
import com.pig4cloud.pigx.codegen.mapper.GenDatasourceConfMapper;
import com.pig4cloud.pigx.codegen.service.GenDatasourceConfService;
@@ -54,6 +55,8 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
private final DataSourceCreator druidDataSourceCreator;
private MagicApiConfiguration apiConfiguration;
/**
* 保存数据源并且加密
* @param conf
@@ -72,6 +75,9 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
// 更新数据库配置
conf.setPassword(stringEncryptor.encrypt(conf.getPassword()));
this.baseMapper.insert(conf);
// 更新magic-api 数据源
apiConfiguration.afterPropertiesSet();
return Boolean.TRUE;
}
@@ -97,6 +103,8 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
conf.setPassword(stringEncryptor.encrypt(conf.getPassword()));
}
this.baseMapper.updateById(conf);
// 更新magic-api 数据源
apiConfiguration.afterPropertiesSet();
return Boolean.TRUE;
}
@@ -110,6 +118,8 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
DynamicRoutingDataSource dynamicRoutingDataSource = SpringContextHolder.getBean(DynamicRoutingDataSource.class);
dynamicRoutingDataSource.removeDataSource(baseMapper.selectById(dsId).getName());
this.baseMapper.deleteById(dsId);
// 更新magic-api 数据源
apiConfiguration.afterPropertiesSet();
return Boolean.TRUE;
}