CheckCurrenUserAspect.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.fdkankan.scene.aop;
  2. import com.fdkankan.common.constant.ErrorCode;
  3. import com.fdkankan.common.exception.BusinessException;
  4. import com.fdkankan.scene.annotation.CheckCurrentUser;
  5. import com.fdkankan.scene.entity.Camera;
  6. import com.fdkankan.scene.entity.SceneCooperation;
  7. import com.fdkankan.scene.entity.ScenePro;
  8. import com.fdkankan.scene.service.ICameraService;
  9. import com.fdkankan.scene.service.ISceneCooperationService;
  10. import com.fdkankan.scene.service.ISceneProService;
  11. import com.fdkankan.web.user.SSOLoginHelper;
  12. import com.fdkankan.web.user.SSOUser;
  13. import java.io.IOException;
  14. import java.lang.reflect.Method;
  15. import java.util.Objects;
  16. import javax.servlet.http.HttpServletRequest;
  17. import lombok.extern.log4j.Log4j2;
  18. import org.aspectj.lang.JoinPoint;
  19. import org.aspectj.lang.annotation.Aspect;
  20. import org.aspectj.lang.annotation.Before;
  21. import org.aspectj.lang.annotation.Pointcut;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.core.annotation.Order;
  24. import org.springframework.stereotype.Component;
  25. import org.springframework.web.context.request.RequestContextHolder;
  26. import org.springframework.web.context.request.ServletRequestAttributes;
  27. /**
  28. * 记录注册用户操作记录、异常记录
  29. */
  30. @Log4j2
  31. @Aspect
  32. @Component
  33. @Order(2)
  34. public class CheckCurrenUserAspect {
  35. @Autowired
  36. private ISceneProService sceneProService;
  37. @Autowired
  38. private ICameraService cameraService;
  39. @Autowired
  40. private SSOLoginHelper ssoLoginHelper;
  41. @Autowired
  42. private ISceneCooperationService sceneCooperationService;
  43. // Service层切点
  44. @Pointcut("@annotation(com.fdkankan.scene.annotation.CheckCurrentUser)")
  45. public void checkUserAspect() {
  46. }
  47. /**
  48. * 前置通知 用于拦截Controller层记录用户的操作
  49. *
  50. * @param joinPoint
  51. * 切点
  52. * @throws IOException
  53. */
  54. @Before("checkUserAspect()")
  55. public void doBefore(JoinPoint joinPoint) throws Exception {
  56. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
  57. .getRequest();
  58. // 读取session中的用户
  59. SSOUser user = ssoLoginHelper.loginCheckV3(request.getHeader("token"));
  60. String sceneNum = request.getParameter("sceneNum");
  61. ScenePro entity = sceneProService.findBySceneNum(sceneNum);
  62. if(user == null){
  63. log.info(getCheckUserMthodDescription(joinPoint));
  64. log.info("不是当前用户的方法:"
  65. + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
  66. throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  67. }
  68. if("18750226207".equals(user.getUserName())){
  69. log.info("18750226207该账号默认超级管理员,可以操作所有场景");
  70. return;
  71. }
  72. if(user.getId() == null){
  73. Camera cameraEntity = cameraService.findByChildName(user.getUserName());
  74. if((cameraEntity != null && entity != null) && (cameraEntity.getId().longValue() != entity.getCameraId().longValue())){
  75. log.info(getCheckUserMthodDescription(joinPoint));
  76. log.info("不是当前用户的方法:"
  77. + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
  78. throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  79. }
  80. }
  81. else if((user != null && entity != null) && entity.getUserId() != null && (user.getId().longValue() != entity.getUserId().longValue())){
  82. SceneCooperation sceneCooperation = sceneCooperationService.getByNumAndUserId(sceneNum, user.getId());
  83. if(Objects.nonNull(sceneCooperation)){
  84. log.info(getCheckUserMthodDescription(joinPoint));
  85. log.info("不是当前用户的方法:"
  86. + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
  87. throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  88. }
  89. }
  90. }
  91. /**
  92. * 获取注解中对方法的描述信息
  93. *
  94. * @param joinPoint
  95. * 切点
  96. * @return 方法描述
  97. * @throws Exception
  98. */
  99. public static String getCheckUserMthodDescription(JoinPoint joinPoint) throws Exception {
  100. String targetName = joinPoint.getTarget().getClass().getName();
  101. String methodName = joinPoint.getSignature().getName();
  102. Object[] arguments = joinPoint.getArgs();
  103. Class targetClass = Class.forName(targetName);
  104. Method[] methods = targetClass.getMethods();
  105. String description = "";
  106. for (Method method : methods) {
  107. if (method.getName().equals(methodName)) {
  108. Class[] clazzs = method.getParameterTypes();
  109. if (clazzs.length == arguments.length) {
  110. description = method.getAnnotation(CheckCurrentUser.class).description();
  111. break;
  112. }
  113. }
  114. }
  115. return description;
  116. }
  117. }