| package com.dy.common.aop; | 
|   | 
| import com.dy.common.contant.Constant; | 
| import com.dy.common.multiDataSource.DataSourceContext; | 
| import com.dy.common.webFilter.UserTokenContext; | 
| import com.dy.common.webUtil.BaseResponseUtils; | 
| import com.mysql.cj.util.StringUtils; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.aspectj.lang.ProceedingJoinPoint; | 
| import org.aspectj.lang.annotation.Around; | 
| import org.aspectj.lang.annotation.Aspect; | 
| import org.aspectj.lang.annotation.Pointcut; | 
| import org.aspectj.lang.reflect.MethodSignature; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.core.annotation.Order; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import java.lang.reflect.Method; | 
| import java.util.Objects; | 
|   | 
| @Slf4j | 
| @Aspect | 
| @Order(Constant.AspectOrderSsoAutho) | 
| @Component | 
| public class SsoAspect { | 
|   | 
|     @Value("${pipIrr.global.dev}") | 
|     public String isDevStage ;//是否为开发阶段 | 
|   | 
|     @Autowired | 
|     private SsoCheck ssoCheck ; | 
|   | 
|     @Pointcut("@annotation(com.dy.common.aop.SsoAop)") | 
|     public void ssoPointCut() { | 
|     } | 
|   | 
|     @Around("ssoPointCut()") | 
|     public Object execute(ProceedingJoinPoint point) throws Throwable { | 
|         if(isDevStage != null && !isDevStage.trim().equals("") && isDevStage.trim().equalsIgnoreCase("true")){ | 
|             return point.proceed(); | 
|         }else{ | 
|             MethodSignature signature = (MethodSignature) point.getSignature(); | 
|             Method method = signature.getMethod(); | 
|             SsoAop aop = method.getAnnotation(SsoAop.class) ; | 
|             if (Objects.nonNull(aop)){ | 
|                 String token = UserTokenContext.get() ; | 
|                 Object rObj = this.ssoCheck.check(token); | 
|                 if(rObj != null){ | 
|                     if(rObj instanceof SsoVo ssoVo){ | 
|                         if(ssoVo.logined){ | 
|                             if(ssoVo.hasPower){ | 
|                                 if(!StringUtils.isNullOrEmpty(ssoVo.dataSourceName)){ | 
|                                     DataSourceContext.set(ssoVo.dataSourceName); | 
|                                     Object obj = point.proceed(); | 
|                                     DataSourceContext.remove(); | 
|                                     return obj ; | 
|                                 }else{ | 
|                                     //无数据源 | 
|                                     return BaseResponseUtils.buildError("后端系统出错,未得到当前登录用户所属机构标签(数据源名)") ; | 
|                                 } | 
|                             }else{ | 
|                                 //无权限 | 
|                                 return BaseResponseUtils.buildNoPower() ; | 
|                             } | 
|                         }else{ | 
|                             //未登录 | 
|                             return BaseResponseUtils.buildToLogin() ; | 
|                         } | 
|                     }else{ | 
|                         return rObj ; | 
|                     } | 
|                 }else{ | 
|                     return BaseResponseUtils.buildError("后端系统出错,check方法返回null") ; | 
|                 } | 
|             }else{ | 
|                 //已经进入注解处理了,还得不到注解,这种情况是不可能的。 | 
|                 return BaseResponseUtils.buildError("后端系统出错,DyAop注解为null") ; | 
|             } | 
|         } | 
|     } | 
|   | 
|   | 
| } |