1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
@Aspect @Component @Slf4j public class DataAuthAspect implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Autowired private NoFilters noFilters;
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; }
@Pointcut("execution(* com.lumi.iiap.controller..*.*(..))) && " + "args(@annotation(com.lumi.iiap.interceptor.dataauth.DataAuthAnnotation))") public void dataAuthMethod() {
}
@Around("dataAuthMethod()") public Object checkDataAuth(ProceedingJoinPoint joinPoint) throws Throwable { String url= SpringRequestHolderUtil.getRequest().getServletPath(); if(AuthUtils.matches(url,noFilters.getDataAuthWhiteList())){ return joinPoint.proceed(); } HttpCommomHeader httpCommomHeader= HttpCommomHeader.getHttpCommomHeaderForIiap(); String userId=httpCommomHeader.getUserid(); if(StringUtils.isBlank(userId)){ return joinPoint.proceed(); } Method method = ((MethodSignature)joinPoint.getSignature()).getMethod(); if (!AuthUtils.checkNeedDataAuthHandleBean(method)){ return joinPoint.proceed(); } log.info("start checkDataAuth:{}",method.getName()); Object[] args= joinPoint.getArgs(); for (Object arg: args) { int index= ArrayUtils.indexOf(args, arg); DataAuthHandlerAdapter.DataAuthHandleBean dataAuthHandleBean = AuthUtils.findDataAuthHandleBean(method,index); if(dataAuthHandleBean!=null){ Boolean result= checkByParam(arg,dataAuthHandleBean); if(!result){ log.error("checkDataAuth error:{},-{}",arg,dataAuthHandleBean); throw new CustomBusinessException(ErrorCodeUtils.PermissionErrorCode.DATA_AUTH_DENIED); } } } log.info("end checkDataAuth:{}",method.getName()); return joinPoint.proceed(); }
private Boolean checkByParam(Object value, DataAuthHandlerAdapter.DataAuthHandleBean dataAuthHandleBean){ if(value==null){ return true; } DataAuthAnnotation annotation = dataAuthHandleBean.getDataAuthAnnotation(); String[] ids= annotation.idKeys(); int[] roleCodes= annotation.roleCodes(); if(ids.length==0 || roleCodes.length ==0 ){ return true; } Map<String,Set<String>> allIds = new HashMap<>(); for(String idFiled:ids){ try { Set<String> oneIds=AuthUtils.getStringByField(value,idFiled); allIds.put(idFiled,oneIds); } catch (IllegalAccessException e) { log.error("getStringByField error:{}-{}",value,idFiled); } } HttpCommomHeader httpCommomHeader= HttpCommomHeader.getHttpCommomHeaderForIiap(); String userId=httpCommomHeader.getUserid(); for(Class<? extends DataAuthAbstractHandle> handleCls:dataAuthHandleBean.getClasses()){
if(handleCls== DataAuthAbstractHandle.class){ continue; } DataAuthAbstractHandle dataAuthAbstractHandle= applicationContext.getBean(handleCls); log.info("annotation value:{}-{}-{}-{}",ids,roleCodes,handleCls.getName(),value); log.info("begin dataAuthAbstractHandle:{}-{} ",userId,allIds); Boolean aBoolean =dataAuthAbstractHandle.checkDataAuth(annotation, userId,allIds); log.info("end dataAuthAbstractHandle:{}-{}-{} ",userId,allIds,aBoolean); if(!aBoolean){ return aBoolean; }
} return true; } }
|