From e93a3c1e0837567d959d8eb581208d5c9b33d3c7 Mon Sep 17 00:00:00 2001 From: hongawen <83944980@qq.com> Date: Thu, 14 Jul 2022 19:27:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A1=E8=AE=A1=E6=97=A5=E5=BF=97=E5=85=A5?= =?UTF-8?q?=E5=BA=93=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../njcn/auth/service/UserTokenService.java | 13 +- .../com/njcn/common/pojo/dto/LogInfoDTO.java | 6 + .../njcn/web/service/impl/LogServiceImpl.java | 28 +++-- .../java/com/njcn/web/utils/RequestUtil.java | 21 +++- .../service/Impl/TransientServiceImpl.java | 119 ++++++++++-------- .../java/com/njcn/system/pojo/po/UserLog.java | 6 +- 6 files changed, 120 insertions(+), 73 deletions(-) diff --git a/pqs-auth/src/main/java/com/njcn/auth/service/UserTokenService.java b/pqs-auth/src/main/java/com/njcn/auth/service/UserTokenService.java index 073fd654e..4a47ce0e2 100644 --- a/pqs-auth/src/main/java/com/njcn/auth/service/UserTokenService.java +++ b/pqs-auth/src/main/java/com/njcn/auth/service/UserTokenService.java @@ -54,10 +54,10 @@ public class UserTokenService { * @param oAuth2AccessToken 认证后的最新token信息 */ @Async("asyncExecutor") - public void recordUserInfo(OAuth2AccessToken oAuth2AccessToken,String ip) { + public void recordUserInfo(OAuth2AccessToken oAuth2AccessToken, String ip) { UserTokenInfo userTokenInfo = new UserTokenInfo(); String accessTokenValue = oAuth2AccessToken.getValue(); - JWSObject accessJwsObject ; + JWSObject accessJwsObject; try { accessJwsObject = JWSObject.parse(accessTokenValue); } catch (ParseException e) { @@ -66,6 +66,7 @@ public class UserTokenService { JSONObject accessJson = JSONUtil.parseObj(accessJwsObject.getPayload().toString()); String userIndex = accessJson.getStr(SecurityConstants.USER_INDEX_KEY); String nickName = accessJson.getStr(SecurityConstants.USER_NICKNAME_KEY); + String loginName = accessJson.getStr(SecurityConstants.USER_NAME_KEY); //查询是否有在线的当前用户 String onlineUserKey = SecurityConstants.TOKEN_ONLINE_PREFIX + userIndex; Object onlineTokenInfoOld = redisUtil.getObjectByKey(onlineUserKey); @@ -86,7 +87,7 @@ public class UserTokenService { } String accessJti = accessJson.getStr(SecurityConstants.JWT_JTI); OAuth2RefreshToken refreshToken = oAuth2AccessToken.getRefreshToken(); - JWSObject refreshJwsObject ; + JWSObject refreshJwsObject; try { refreshJwsObject = JWSObject.parse(refreshToken.getValue()); } catch (ParseException e) { @@ -97,13 +98,13 @@ public class UserTokenService { Long refreshExpireTime = refreshJson.getLong(SecurityConstants.JWT_EXP); userTokenInfo.setAccessTokenJti(accessJti); userTokenInfo.setRefreshToken(refreshToken.getValue()); - LocalDateTime refreshLifeTime =LocalDateTime.ofEpochSecond(refreshExpireTime,0,ZoneOffset.of("+8")); + LocalDateTime refreshLifeTime = LocalDateTime.ofEpochSecond(refreshExpireTime, 0, ZoneOffset.of("+8")); userTokenInfo.setRefreshTokenExpire(refreshLifeTime); //生命周期在refreshToken的基础上,延迟5分钟 redisUtil.saveByKeyWithExpire(onlineUserKey, userTokenInfo, refreshLifeTime.plusMinutes(5L).toEpochSecond(ZoneOffset.of("+8")) - LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"))); //记录成功登录后的信息 - LogInfoDTO logInfoDTO = new LogInfoDTO(nickName, ip, "登录认证", OperateType.AUTHENTICATE, 1, "",0, 1, generalInfo.getMicroServiceName(), userIndex); + LogInfoDTO logInfoDTO = new LogInfoDTO(loginName, nickName, ip, "登录认证", OperateType.AUTHENTICATE, 1, "", 0, 1, generalInfo.getMicroServiceName(), userIndex); publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false); } @@ -126,7 +127,7 @@ public class UserTokenService { if (CollectionUtils.isNotEmpty(blackUsers)) { blackUsers.forEach(temp -> { //存在当前的刷新token,则抛出业务异常 - if(temp.getRefreshToken().equalsIgnoreCase(refreshToken)){ + if (temp.getRefreshToken().equalsIgnoreCase(refreshToken)) { throw new BusinessException(CommonResponseEnum.TOKEN_EXPIRE_JWT); } }); diff --git a/pqs-common/common-core/src/main/java/com/njcn/common/pojo/dto/LogInfoDTO.java b/pqs-common/common-core/src/main/java/com/njcn/common/pojo/dto/LogInfoDTO.java index 61c2746f1..3ce0ad73f 100644 --- a/pqs-common/common-core/src/main/java/com/njcn/common/pojo/dto/LogInfoDTO.java +++ b/pqs-common/common-core/src/main/java/com/njcn/common/pojo/dto/LogInfoDTO.java @@ -21,8 +21,14 @@ public class LogInfoDTO implements Serializable { /** * 登录名 */ + private String loginName; + + /** + * 用户已登录:用户名 + */ private String userName; + private String ip; private String operate; diff --git a/pqs-common/common-web/src/main/java/com/njcn/web/service/impl/LogServiceImpl.java b/pqs-common/common-web/src/main/java/com/njcn/web/service/impl/LogServiceImpl.java index 3bf3752db..f9ccfa4c4 100644 --- a/pqs-common/common-web/src/main/java/com/njcn/web/service/impl/LogServiceImpl.java +++ b/pqs-common/common-web/src/main/java/com/njcn/web/service/impl/LogServiceImpl.java @@ -57,14 +57,17 @@ public class LogServiceImpl implements ILogService { @Async("asyncExecutor") public void recodeAdviceLog(ServerHttpRequest request, MethodParameter returnType, HttpResult httpResult, String methodDescribe) { //处理审计日志 + String loginName; String userName; - String userIndex ; + String userIndex; HttpServletRequest httpServletRequest = RequestUtil.getRequest(request); if (UN_LOGIN_METHOD.contains(methodDescribe)) { - userName = RequestUtil.getLoginName(httpServletRequest); + loginName = RequestUtil.getLoginName(httpServletRequest); + userName = ""; userIndex = userName; } else { - userName = RequestUtil.getLoginName(request); + loginName = RequestUtil.getLoginNameByPayload(request); + userName = RequestUtil.getUserNickname(request); userIndex = RequestUtil.getUserIndex(request); } String result = httpResult.getCode().equalsIgnoreCase(CommonResponseEnum.FAIL.getCode()) ? CommonResponseEnum.FAIL.getMessage() : CommonResponseEnum.SUCCESS.getMessage(); @@ -72,7 +75,7 @@ public class LogServiceImpl implements ILogService { String type = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateType(); String level = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateLevel(); String operateType = ReflectCommonUtil.getOperateTypeByMethod(returnType.getMethod()); - LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, "",levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex); + LogInfoDTO logInfoDTO = new LogInfoDTO(loginName, userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, "", levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex); publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false); } @@ -90,10 +93,11 @@ public class LogServiceImpl implements ILogService { LogInfoDTO tempLogInfo = RequestUtil.initLogInfo(request); //认证前,获取用户信息 String userIndex; - if (Objects.equals(tempLogInfo.getUserName(), LogInfo.UNKNOWN_USER)) { - tempLogInfo.setUserName(RequestUtil.getLoginName(request)); - userIndex = tempLogInfo.getUserName(); - }else{ + if (Objects.equals(tempLogInfo.getLoginName(), LogInfo.UNKNOWN_USER)) { + tempLogInfo.setLoginName(RequestUtil.getLoginName(request)); + tempLogInfo.setUserName(""); + userIndex = tempLogInfo.getLoginName(); + } else { userIndex = RequestUtil.getUserIndex(request); } //根据异常获取method方法 @@ -106,7 +110,7 @@ public class LogServiceImpl implements ILogService { String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType(); String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel(); String operateType = ReflectCommonUtil.getOperateTypeByMethod(method); - LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getUserName(), tempLogInfo.getIp(), ReflectCommonUtil.getMethodDescribeByMethod(method), operateType, result.equalsIgnoreCase("失败") ? 0 : 1,message, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex); + LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getLoginName(), tempLogInfo.getUserName(), tempLogInfo.getIp(), ReflectCommonUtil.getMethodDescribeByMethod(method), operateType, result.equalsIgnoreCase("失败") ? 0 : 1, message, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex); publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false); } @@ -116,11 +120,11 @@ public class LogServiceImpl implements ILogService { * @param exception 异常数据 * @param request 请求信息 * @param message 信息 - * @param userName 用户名 + * @param loginName 登录名 */ @Override @Async("asyncExecutor") - public void recodeAuthExceptionLog(Exception exception, HttpServletRequest request, String message, String userName) { + public void recodeAuthExceptionLog(Exception exception, HttpServletRequest request, String message, String loginName) { //根据异常获取method方法 Method method = ReflectCommonUtil.getMethod(exception); if (exception instanceof MethodArgumentNotValidException) { @@ -132,7 +136,7 @@ public class LogServiceImpl implements ILogService { String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType(); String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel(); String operateType = ReflectCommonUtil.getOperateTypeByMethod(method); - LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, ReflectCommonUtil.getMethodDescribeByMethod(method), operateType, result.equalsIgnoreCase("失败") ? 0 : 1,message, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userName); + LogInfoDTO logInfoDTO = new LogInfoDTO(loginName, "", ip, ReflectCommonUtil.getMethodDescribeByMethod(method), operateType, result.equalsIgnoreCase("失败") ? 0 : 1, message, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), loginName); publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false); } diff --git a/pqs-common/common-web/src/main/java/com/njcn/web/utils/RequestUtil.java b/pqs-common/common-web/src/main/java/com/njcn/web/utils/RequestUtil.java index e54e0ba63..14144a104 100644 --- a/pqs-common/common-web/src/main/java/com/njcn/web/utils/RequestUtil.java +++ b/pqs-common/common-web/src/main/java/com/njcn/web/utils/RequestUtil.java @@ -331,6 +331,19 @@ public class RequestUtil { return StrUtil.isBlank(loginName) ? LogInfo.UNKNOWN_USER : loginName; } + /** + * ServerHttpRequest获取在网关中存储的用户昵称 + */ + public static String getLoginNameByPayload(ServerHttpRequest request) { + String loginName = LogInfo.UNKNOWN_USER; + JSONObject jwtPayload = getJwtPayload(request); + if (Objects.nonNull(jwtPayload)) { + String loginNameTemp = jwtPayload.getString(SecurityConstants.USER_NAME_KEY); + loginName = StrUtil.isBlank(loginNameTemp) ? LogInfo.UNKNOWN_USER : loginNameTemp; + } + return loginName; + } + /** * ServerHttpRequest获取用户登录名 */ @@ -376,11 +389,15 @@ public class RequestUtil { LogInfoDTO temp = new LogInfoDTO(); temp.setIp(StrUtil.isBlank(ip) ? LogInfo.UNKNOWN_IP : ip); String username = LogInfo.UNKNOWN_USER; + String loginName = LogInfo.UNKNOWN_USER; JSONObject jwtPayload = getJwtPayload(request); if (Objects.nonNull(jwtPayload)) { - String userSignTemp = jwtPayload.getString(SecurityConstants.USER_NAME_KEY); - username = StrUtil.isBlank(userSignTemp) ? LogInfo.UNKNOWN_USER : userSignTemp; + String loginNameTemp = jwtPayload.getString(SecurityConstants.USER_NAME_KEY); + loginName = StrUtil.isBlank(loginNameTemp) ? LogInfo.UNKNOWN_USER : loginNameTemp; + String userNameTemp = jwtPayload.getString(SecurityConstants.USER_NICKNAME_KEY); + username = StrUtil.isBlank(userNameTemp) ? LogInfo.UNKNOWN_USER : userNameTemp; } + temp.setLoginName(loginName); temp.setUserName(username); return temp; } diff --git a/pqs-event/event-boot/src/main/java/com/njcn/event/service/Impl/TransientServiceImpl.java b/pqs-event/event-boot/src/main/java/com/njcn/event/service/Impl/TransientServiceImpl.java index ffa25a0a5..6a86009b1 100644 --- a/pqs-event/event-boot/src/main/java/com/njcn/event/service/Impl/TransientServiceImpl.java +++ b/pqs-event/event-boot/src/main/java/com/njcn/event/service/Impl/TransientServiceImpl.java @@ -68,9 +68,9 @@ public class TransientServiceImpl implements TransientService { if (!CollectionUtils.isEmpty(deviceList)) { List> lists = deviceList.stream().map(GeneralDeviceDTO::getLineIndexes).collect(Collectors.toList()); List lineList = new ArrayList<>(); - for (int i = 0; i strings1 = lists.get(i); - for (int a = 0; a data = eventDetailService.getEventDetail(lineList, transientParam.getSearchBeginTime(), transientParam.getSearchEndTime()); page.setTotal(data.size()); //分页总页数 - int pages = (int)Math.ceil(data.size()*1.0/transientParam.getPageSize()); + int pages = (int) Math.ceil(data.size() * 1.0 / transientParam.getPageSize()); page.setPages(pages); //influxDB分页查询 - List eventDetailData = eventDetailService.getEventDetailLimit(lineList, transientParam.getSearchBeginTime(), transientParam.getSearchEndTime(), transientParam.getPageSize(), transientParam.getPageNum()); + List eventDetailData = eventDetailService.getEventDetailLimit(lineList, transientParam.getSearchBeginTime(), transientParam.getSearchEndTime(), transientParam.getPageSize(), transientParam.getPageNum()); // List> partition = Lists.partition(eventDetailData, transientParam.getPageSize()); // List detailList = partition.get(transientParam.getPageNum() - 1); if (!CollectionUtils.isEmpty(eventDetailData)) { @@ -90,7 +90,7 @@ public class TransientServiceImpl implements TransientService { collect = collect.stream().distinct().collect(Collectors.toList()); List transientData = transientMapper.getTransientData(collect); int i = 1; - for (EventDetail eventDetail: eventDetailData) { + for (EventDetail eventDetail : eventDetailData) { if (!Objects.isNull(eventDetail)) { TransientVO transientVO = new TransientVO(); transientVO.setId(eventDetail.getLineId()); @@ -127,9 +127,9 @@ public class TransientServiceImpl implements TransientService { break; } Double value = new BigDecimal(eventDetail.getEventValue()).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); - transientVO.setEventValue(new BigDecimal(value*100).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue()); - transientVO.setEventValues(100-transientVO.getEventValue()); - transientVO.setPersistTime((Float.parseFloat(eventDetail.getPersistTime().toString()))/1000); + transientVO.setEventValue(new BigDecimal(value * 100).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue()); + transientVO.setEventValues(100 - transientVO.getEventValue()); + transientVO.setPersistTime((Float.parseFloat(eventDetail.getPersistTime().toString())) / 1000); Float eventValue = Float.parseFloat(eventDetail.getEventValue().toString()); Float persistTime = Float.parseFloat(eventDetail.getPersistTime().toString()); @@ -174,7 +174,7 @@ public class TransientServiceImpl implements TransientService { // waveDataVO.setName("变电站名称: "+ substation +" 监测点名称: "+ name +" 发生时刻: "+ timeId +" 暂降幅值: "+ v +"% 持续时间: "+ persistTime +"s"); // waveDataVO.setTargetName("相电压有效值"); AnalyWave analyWave = new AnalyWave(); - WaveDataDTO comtrade = analyWave.getComtrade(eventBaseConfig.getWavePath()+"\\"+ip+"\\"+waveName+".CFG", 1); + WaveDataDTO comtrade = analyWave.getComtrade(eventBaseConfig.getWavePath() + File.separator + ip + File.separator + waveName + ".CFG", 1); if (Objects.isNull(comtrade.getComtradeCfgDTO())) { throw new BusinessException(EventResponseEnum.ANALYSEWAVE_NOT_FOUND); } @@ -185,8 +185,8 @@ public class TransientServiceImpl implements TransientService { double pt2 = Double.parseDouble(lineDetailData.getPt().split("/")[1]); double ct1 = Double.parseDouble(lineDetailData.getCt().split("/")[0]); double ct2 = Double.parseDouble(lineDetailData.getCt().split("/")[1]); - waveDataDTO.setPt(pt1/pt2); - waveDataDTO.setCt(ct1/ct2); + waveDataDTO.setPt(pt1 / pt2); + waveDataDTO.setCt(ct1 / ct2); return waveDataDTO; } @@ -195,7 +195,7 @@ public class TransientServiceImpl implements TransientService { public HttpServletResponse downloadWaveFile(WaveFileParam waveFileParam, HttpServletResponse response) throws Exception { List lineId = waveFileParam.getLineId(); List timeId = waveFileParam.getTimeId(); - copyTempData(timeId,lineId); + copyTempData(timeId, lineId); zipCompress(new File("C:\\Users\\陈超\\Desktop\\YSWJ\\comtrade")); String zipPath = "C:\\Users\\陈超\\Desktop\\YSWJ\\comtrade.zip"; @@ -229,9 +229,11 @@ public class TransientServiceImpl implements TransientService { return response; } - /**复制保存文件 */ + /** + * 复制保存文件 + */ public void copyTempData(List timeId, List lineId) throws IOException { - for (int i=0; i pathList) { List list = new ArrayList<>(); - for (String string: pathList) { + for (String string : pathList) { String path = string; //待压缩的多个源文件 - File f1 = new File(path+".CFG"); - File f2 = new File(path+".DAT"); + File f1 = new File(path + ".CFG"); + File f2 = new File(path + ".DAT"); list.add(f1); list.add(f2); } @@ -401,6 +412,7 @@ public class TransientServiceImpl implements TransientService { /** * 压缩多个文件成一个zip文件 + * * @param srcfile:源文件列表 * @param zipfile:压缩后的文件 */ @@ -426,7 +438,9 @@ public class TransientServiceImpl implements TransientService { } } - /**删除文件(夹)和空文件夹 */ + /** + * 删除文件(夹)和空文件夹 + */ private void deleteDirectoryLegacyIO(File file) { File[] list = file.listFiles(); //无法做到list多层文件夹数据 if (list != null) { @@ -443,6 +457,7 @@ public class TransientServiceImpl implements TransientService { /** * 删除文件 + * * @param filePathAndName 指定得路径 */ public static void delFile(String filePathAndName) { diff --git a/pqs-system/system-api/src/main/java/com/njcn/system/pojo/po/UserLog.java b/pqs-system/system-api/src/main/java/com/njcn/system/pojo/po/UserLog.java index 2e27fc5a9..b16af61df 100644 --- a/pqs-system/system-api/src/main/java/com/njcn/system/pojo/po/UserLog.java +++ b/pqs-system/system-api/src/main/java/com/njcn/system/pojo/po/UserLog.java @@ -22,9 +22,13 @@ public class UserLog extends BaseEntity { */ private String id; + /** + * 登录名 + */ + private String loginName; + /** * 用户已登录:用户名 - * 用户未登录:登录名 */ private String userName;