Spring Data REST filtering data based on the user

前端 未结 4 1951
無奈伤痛
無奈伤痛 2020-12-15 11:25

If I have a repository setup like the following, making use of Spring Data REST, I can access the data at /receipts and see all data. However, I want to only return data fo

4条回答
  •  被撕碎了的回忆
    2020-12-15 12:08

    This issue is a tipical cross-cutting concern so I tried apply AOP. Define Advice and update the args (String storer), as explain at: https://stackoverflow.com/a/46353783/1203628

    @Aspect
    @Transactional
    @Component
    public class FilterProjectsAspect {
    
    @Pointcut("execution(*  com.xxx.ReceiptRepository.findByStorer(..))")
        public void projectFindAll() {
        }
    
        @Around("projectFindAll()")
        public Object  filterProjectsByUser(final ProceedingJoinPoint pjp) throws Throwable {
    
            Object[] args = pjp.getArgs();
            for (int i = 0; i < args.length; i++) {
                if (args[i] instanceof String) {
                    String storer=(String) args[i];
                    // Find storer by user 
                    args[i]=storer;  //Update args
                }
            return pjp.proceed(args);
        }
    
    }
    

提交回复
热议问题