ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

如何使用@Aspect 实现AOP动态代理 --- AOP实战(二)

2021-08-02 12:05:36  阅读:267  来源: 互联网

标签:req educationIdList --- getEntity user Aspect AOP new arg


文章目录

  • @Aspect则开启动态代理,结合 @Before(“xxx切入点方法”),@After(“xxx切入点方法”)等注解使用
  • @Order(xx) 是配置类的执行顺序,xx越小越先执行
  • @Pointcut("xx") 是切入点的路径位置

简洁业务代码(推荐)

@Slf4j
@Aspect
@Order(669)
@Component
public class DataIsolationAop {

    @Autowired
    private XkHttpSecurity xkHttpSecurity;


    @Pointcut("execution(* com.tzh.practice..*Controller.*(..))")
    public void pointToCut() {
    }

    @Before("pointToCut()")
    public void before(JoinPoint joinPoint) {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        获取接口名 判断是否是需要数据隔离的接口
        String servletPath = request.getServletPath();
        boolean dataIsolation = xkHttpSecurity.isDataIsolation(servletPath);

//        若是需要业务操作的接口 则将数据 放进传参内
        if (dataIsolation) {
//      校验情况
            Object[] args = joinPoint.getArgs();//获取接口参数
            if (args == null || args.length == 0) {
                return;
            }
       
//                根据不同身份(学生,学校,教育局,家长)  获取所需数据
                List<String> schoolIdList = new ArrayList<>();
                List<String> educationIdList = new ArrayList<>()
                
//-------------------------------------------    
执行相关业务代码 给予属性赋值(此处不演示)
//-------------------------------------------

            /**
             * 把所需数据 放进请求参数
             */
            for (Object arg : args) {
                if (arg instanceof PortalBasePageRequest) {//判断传参的类型,给予属性赋值
                    PortalBasePageRequest req = (PortalBasePageRequest) arg;
                    req.setEducationIdList(educationIdList);
                    req.setSchoolIdList(schoolIdList);
                } else if (arg instanceof PortalBaseRequest) {
                    PortalBaseRequest req = (PortalBaseRequest) arg;
                    req.setEducationIdList(educationIdList);
                    req.setSchoolIdList(schoolIdList);
                }
            }


        }

    }
}

详细业务代码

  • 该演示是做数据隔离的业务,在需要做数据隔离的接口中(将这些接口名放在yml下配置即可,具体操作请看获取配置文件中所有接口名),获取当前用户判断不同身份,给予不同的属性值进参数内即可, 之后即可在这些接口后续的业务逻辑中拿这些属性值做业务隔离
@Slf4j
@Aspect
@Order(669)
@Component
public class DataIsolationAop {

    @Autowired
    private XkHttpSecurity xkHttpSecurity;
    @Autowired
    private AuthUserClient authUserClient;
    @Autowired
    private BaseSchoolClient baseSchoolClient;
    @Autowired
    private BaseStudentClient baseStudentClient;
    @Autowired
    private AuthUserFamilyClient familyClient;


    @Pointcut("execution(* com.tzh.practice..*Controller.*(..))")
    public void pointToCut() {
    }

    @Before("pointToCut()")
    public void before(JoinPoint joinPoint) {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        获取接口名 判断是否是需要数据隔离的接口
        String servletPath = request.getServletPath();
        boolean dataIsolation = xkHttpSecurity.isDataIsolation(servletPath);

//        若是需要数据隔离的接口 则将数据 放进传参内
        if (dataIsolation) {
//      校验情况
            R<UserLoginResp> r = authUserClient.getBaseUser(new TokenReq(getToken(request)));
            Object[] args = joinPoint.getArgs();
            if (args == null || args.length == 0) {
                return;
            }
            UserLoginResp user = r.getEntity();
            if (Objects.isNull(user)){
                return;
            }

//                根据不同身份(学生,学校,教育局,家长)  获取所需数据
                List<String> schoolIdList = new ArrayList<>();
                List<String> educationIdList = new ArrayList<>();

                if (user.getIdentity().equals(9)){//学校身份
                    schoolIdList.add(user.getBaseId());
                    R<BaseSchoolDetailResp> schR = baseSchoolClient.getBaseSchoolById(new IdReq(user.getBaseId()));
                    BaseSchoolDetailResp sch = schR.getEntity();
                    educationIdList.add(sch.getBaseEducationId());
                }else if (user.getIdentity().equals(6)){//教育局身份
                    R<List<String>> schIdListR = baseSchoolClient.getSchoolIdListByEduId(new IdReq(user.getBaseId()));
                    if (Objects.nonNull(schIdListR.getEntity()) && schIdListR.getEntity().size() >0){
                        schoolIdList.addAll(schIdListR.getEntity());
                    }
                    educationIdList.add(user.getBaseId());
                }else if (user.getIdentity().equals(0)){//学生身份
                    R<String> schIdR =  baseStudentClient.getSchId(new IdReq(user.getBaseId()));
                    if (Objects.nonNull(schIdR.getEntity())){
                        schoolIdList.add(schIdR.getEntity());
                    }
                    R<String> eduIdR =  baseStudentClient.getEduId(new IdReq(user.getBaseId())); //通过学生baseId 找到对应的教育局id
                    if (Objects.nonNull(eduIdR.getEntity())){
                        educationIdList.add(eduIdR.getEntity());
                    }
                }else {
                    R<List<StuBindFamilyInfoResp>> familyBindStuInfo = familyClient.getNewFamilyBindStuInfo(new IdReq(user.getId()));
                    if (Objects.nonNull(familyBindStuInfo.getEntity()) && familyBindStuInfo.getEntity().size()>0){//有绑定学生信息 则是家长身份
                        List<StuBindFamilyInfoResp> familyInfoResps = familyBindStuInfo.getEntity();
                        for (StuBindFamilyInfoResp  resp: familyInfoResps){
                            R<String> eduIdR =  baseStudentClient.getEduId(new IdReq(resp.getStudentBaseId()));
                            if (Objects.nonNull(eduIdR.getEntity())){
                                educationIdList.add(eduIdR.getEntity());
                            }
                        }
                    }
                }

            /**
             * 把所需数据隔离数据 放进请求参数
             */
            for (Object arg : args) {
                if (arg instanceof PortalBasePageRequest) {
                    PortalBasePageRequest req = (PortalBasePageRequest) arg;
                    req.setEducationIdList(educationIdList);
                    req.setSchoolIdList(schoolIdList);
                } else if (arg instanceof PortalBaseRequest) {
                    PortalBaseRequest req = (PortalBaseRequest) arg;
                    req.setEducationIdList(educationIdList);
                    req.setSchoolIdList(schoolIdList);
                }
            }

        }

    }

    private String getToken(HttpServletRequest request) {
        String token = request.getHeader(RedisConstant.TOKEN);
        if (StringUtils.isBlank(token)) {
            token = request.getParameter("token");
            if (StringUtils.isBlank(token)) {
                throw new BizException(AccountErrorEnum.NO_LOGIN);
            }
        }
        return token;
    }

}

标签:req,educationIdList,---,getEntity,user,Aspect,AOP,new,arg
来源: https://blog.csdn.net/EDT777/article/details/119321948

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有