Compare commits

...

3 Commits

3 changed files with 23 additions and 7 deletions

View File

@ -1,7 +1,9 @@
package com.electromagnetic.industry.software.manage.aop; package com.electromagnetic.industry.software.manage.aop;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.electromagnetic.industry.software.common.pojo.UserLoginInfo;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult; import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
import com.electromagnetic.industry.software.common.util.UserThreadLocal; import com.electromagnetic.industry.software.common.util.UserThreadLocal;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
@ -17,6 +19,7 @@ import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
@Aspect @Aspect
@Component @Component
@ -66,11 +69,19 @@ public class ServiceAspect {
StopWatch stopwatch = new StopWatch(); StopWatch stopwatch = new StopWatch();
stopwatch.start("接口:" + methodInfo); stopwatch.start("接口:" + methodInfo);
Object rvt = jp.proceed(); Object rvt = jp.proceed();
if (Objects.isNull(UserThreadLocal.getUser())) {
UserLoginInfo userLoginInfo = new UserLoginInfo();
UserThreadLocal.set(userLoginInfo);
}
UserThreadLocal.setReqArgs(paramInfo);
if (rvt instanceof ResponseEntity) { if (rvt instanceof ResponseEntity) {
UserThreadLocal.setRes(ElectromagneticResultUtil.success(""));
return rvt; return rvt;
} }
UserThreadLocal.setRes((ElectromagneticResult)rvt); UserThreadLocal.setRes((ElectromagneticResult)rvt);
UserThreadLocal.setReqArgs(paramInfo);
String returnInfo = JSONUtil.toJsonStr(rvt); String returnInfo = JSONUtil.toJsonStr(rvt);
log.info("请求接口结束:{},返回参数:{},接口耗时:{}", methodInfo, returnInfo, (System.currentTimeMillis() - startTime) + "毫秒"); log.info("请求接口结束:{},返回参数:{},接口耗时:{}", methodInfo, returnInfo, (System.currentTimeMillis() - startTime) + "毫秒");
stopwatch.stop(); stopwatch.stop();

View File

@ -100,10 +100,10 @@ public class LoginInterceptor implements HandlerInterceptor {
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
long accessStartTime = (long)request.getSession().getAttribute("accessStartTime"); long accessStartTime = (long)request.getSession().getAttribute("accessStartTime");
ElectromagneticResult<?> result = UserThreadLocal.getResult();
String reqArgs = UserThreadLocal.getReqArgs();
long accessEndTime = System.currentTimeMillis(); long accessEndTime = System.currentTimeMillis();
UserOperation userOperation = ((HandlerMethod) handler).getMethod().getAnnotation(UserOperation.class); UserOperation userOperation = ((HandlerMethod) handler).getMethod().getAnnotation(UserOperation.class);
String reqArgs = UserThreadLocal.getReqArgs();
UserAccessLog userAccessLog = UserAccessLog.builder() UserAccessLog userAccessLog = UserAccessLog.builder()
.id(IdWorker.getSnowFlakeIdString()) .id(IdWorker.getSnowFlakeIdString())
.userId(UserThreadLocal.getUserId()) .userId(UserThreadLocal.getUserId())
@ -117,11 +117,16 @@ public class LoginInterceptor implements HandlerInterceptor {
.accessSuccess(true) .accessSuccess(true)
.createTime(new Date()) .createTime(new Date())
.build(); .build();
ElectromagneticResult<?> result = UserThreadLocal.getResult();
if (result != null) {
if (!result.getSuccess()) { if (!result.getSuccess()) {
userAccessLog.setAccessSuccess(false); userAccessLog.setAccessSuccess(false);
userAccessLog.setFailureReason(result.getErrorMessage()); userAccessLog.setFailureReason(result.getErrorMessage());
} }
// TODO 如果是下载的文件那么返回的是ResponseEntity而不是ElectromagneticResult } else { // 返回为ResponseEntity且状态为失败
userAccessLog.setAccessSuccess(false);
}
userAccessLogMapper.insert(userAccessLog); userAccessLogMapper.insert(userAccessLog);
UserThreadLocal.remove(); UserThreadLocal.remove();
} }

View File

@ -133,7 +133,7 @@ public class EdFileInfoController {
return edFileInfoService.queryChildFolder(parentId); return edFileInfoService.queryChildFolder(parentId);
} }
@RequiredPermission(value = FilePermission.VIEW) @RequiredPermission(value = FilePermission.DOWNLOAD)
@RequestMapping(value = "preview", method = RequestMethod.GET) @RequestMapping(value = "preview", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> preview(@RequestParam String id, HttpServletResponse response) { public ResponseEntity<InputStreamResource> preview(@RequestParam String id, HttpServletResponse response) {
return edFileInfoService.preview(id, response); return edFileInfoService.preview(id, response);