123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package com.fdkankan.scene.aop;
- import com.fdkankan.common.constant.ErrorCode;
- import com.fdkankan.common.exception.BusinessException;
- import com.fdkankan.scene.annotation.CheckCurrentUser;
- import com.fdkankan.scene.entity.Camera;
- import com.fdkankan.scene.entity.SceneCooperation;
- import com.fdkankan.scene.entity.ScenePro;
- import com.fdkankan.scene.service.ICameraService;
- import com.fdkankan.scene.service.ISceneCooperationService;
- import com.fdkankan.scene.service.ISceneProService;
- import com.fdkankan.web.user.SSOLoginHelper;
- import com.fdkankan.web.user.SSOUser;
- import java.io.IOException;
- import java.lang.reflect.Method;
- import java.util.Objects;
- import javax.servlet.http.HttpServletRequest;
- import lombok.extern.log4j.Log4j2;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.annotation.Order;
- import org.springframework.stereotype.Component;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- /**
- * 记录注册用户操作记录、异常记录
- */
- @Log4j2
- @Aspect
- @Component
- @Order(2)
- public class CheckCurrenUserAspect {
- @Autowired
- private ISceneProService sceneProService;
- @Autowired
- private ICameraService cameraService;
- @Autowired
- private SSOLoginHelper ssoLoginHelper;
- @Autowired
- private ISceneCooperationService sceneCooperationService;
- // Service层切点
- @Pointcut("@annotation(com.fdkankan.scene.annotation.CheckCurrentUser)")
- public void checkUserAspect() {
- }
- /**
- * 前置通知 用于拦截Controller层记录用户的操作
- *
- * @param joinPoint
- * 切点
- * @throws IOException
- */
- @Before("checkUserAspect()")
- public void doBefore(JoinPoint joinPoint) throws Exception {
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
- .getRequest();
- // 读取session中的用户
- SSOUser user = ssoLoginHelper.loginCheckV3(request.getHeader("token"));
- String sceneNum = request.getParameter("sceneNum");
- ScenePro entity = sceneProService.findBySceneNum(sceneNum);
- if(user == null){
- log.info(getCheckUserMthodDescription(joinPoint));
- log.info("不是当前用户的方法:"
- + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
- throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- }
- if("18750226207".equals(user.getUserName())){
- log.info("18750226207该账号默认超级管理员,可以操作所有场景");
- return;
- }
- if(user.getId() == null){
- Camera cameraEntity = cameraService.findByChildName(user.getUserName());
- if((cameraEntity != null && entity != null) && (cameraEntity.getId().longValue() != entity.getCameraId().longValue())){
- log.info(getCheckUserMthodDescription(joinPoint));
- log.info("不是当前用户的方法:"
- + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
- throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- }
- }
- else if((user != null && entity != null) && entity.getUserId() != null && (user.getId().longValue() != entity.getUserId().longValue())){
- SceneCooperation sceneCooperation = sceneCooperationService.getByNumAndUserId(sceneNum, user.getId());
- if(Objects.nonNull(sceneCooperation)){
- log.info(getCheckUserMthodDescription(joinPoint));
- log.info("不是当前用户的方法:"
- + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
- throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- }
- }
- }
- /**
- * 获取注解中对方法的描述信息
- *
- * @param joinPoint
- * 切点
- * @return 方法描述
- * @throws Exception
- */
- public static String getCheckUserMthodDescription(JoinPoint joinPoint) throws Exception {
- String targetName = joinPoint.getTarget().getClass().getName();
- String methodName = joinPoint.getSignature().getName();
- Object[] arguments = joinPoint.getArgs();
- Class targetClass = Class.forName(targetName);
- Method[] methods = targetClass.getMethods();
- String description = "";
- for (Method method : methods) {
- if (method.getName().equals(methodName)) {
- Class[] clazzs = method.getParameterTypes();
- if (clazzs.length == arguments.length) {
- description = method.getAnnotation(CheckCurrentUser.class).description();
- break;
- }
- }
- }
- return description;
- }
- }
|