package cn.timer.api.aspect; import java.lang.reflect.Method; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.lang.Console; import cn.timer.api.aspect.lang.annotation.BindingResultCtrol; import cn.timer.api.aspect.lang.annotation.Role; import cn.timer.api.bean.qyzx.QyzxEmpEntAsso; import cn.timer.api.bean.qyzx.QyzxEmpLogin; import cn.timer.api.config.enums.SysRoleType; import cn.timer.api.dao.qyzx.QyzxEmpEntAssoMapper; import cn.timer.api.utils.ResultUtil; /** * 角色权限 处理 * * @author Tang */ @Aspect @Component public class RoleAspect { // private static final Logger log = LoggerFactory.getLogger(RoleAspect.class); @Autowired private QyzxEmpEntAssoMapper qyzxEmpEntAssoMapper; @Resource private HttpSession session; // 配置织入点 @Pointcut("@annotation(cn.timer.api.aspect.lang.annotation.Role)") public void RolePointCut() { } @Around("RolePointCut()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { Object retVal; // 获得注解 Role role = getAnnotationLog(joinPoint); String httpMethodName = role.httpMethod().name(); List<Integer> sysRoleType = ListUtil.toList(); for (SysRoleType t : role.sysRoleType()) { sysRoleType.add(t.getType()); } QyzxEmpLogin eld = BeanUtil.toBean(session.getAttribute("ui"), QyzxEmpLogin.class); Integer orgCode = eld.getOrgId(); Integer count = new LambdaQueryChainWrapper<QyzxEmpEntAsso>(qyzxEmpEntAssoMapper) .eq(QyzxEmpEntAsso::getEmpNum, eld.getId()) .eq(QyzxEmpEntAsso::getOrgCode, orgCode) .in(QyzxEmpEntAsso::getUserType, sysRoleType) .count(); if (count <= 0) { switch (httpMethodName) { case "POST": retVal = ResultUtil.error("无权限操作"); break; case "PUT": retVal = ResultUtil.error("无权限编辑"); break; case "GET": retVal = ResultUtil.error("无权限查看"); break; case "DELETE": retVal = ResultUtil.error("无权限删除"); break; default: retVal = ResultUtil.error("无权限操作"); break; } return retVal; }else { return retVal = joinPoint.proceed(joinPoint.getArgs()); } } /** * 是否存在注解,如果存在就获取 */ private Role getAnnotationLog(JoinPoint joinPoint) { Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); if (method != null) { return method.getAnnotation(Role.class); } return null; } }