代码之家  ›  专栏  ›  技术社区  ›  nintschger

如何将RequestBody中的对象传递给注释

  •  0
  • nintschger  · 技术社区  · 8 年前

    userDto . 问题是,我不能通过考试 userDto.getId() 自从 用户DTO 尚未解决。见以下代码:

      @RequiresPrivileges(value = {PrivilegeType.USRMGA, PrivilegeType.USRMGO}, usrMgmntObj = User.class, usrMgmntObjId = userDto.getId()) // at this stage, userDto is not known
      @PutMapping
      public ResponseEntity<List<DetailedUserDto>> update(@RequestBody UserDto userDto) {
        return new ResponseEntity<>(userService.update(userDto), HttpStatus.OK);
      }
    

    @接口要求特权:

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface RequiresPrivileges {
    
      /**
       * Required privileges to access the method
       */
      PrivilegeType[] value();
    
      /**
       * If user wants to make changes to another user or usergroup, the rank needs to be checked. To do so,
       * the object class (user or usergroup) and the suiting id needs to be provided
       */
      Class usrMgmntObj() default Object.class;
      long usrMgmntObjId() default -1;
    }
    

    @注释的方面

    @Around("@annotation(RequiresPrivileges)")
    public Object requiresPrivileges(ProceedingJoinPoint joinPoint) throws Throwable {
    
      MethodSignature signature = (MethodSignature) joinPoint.getSignature();
      Method method = signature.getMethod();
    
      PrivilegeType[] privileges = method.getAnnotation(RequiresPrivileges.class).value();
      Class usrMgmntObjClass = method.getAnnotation(RequiresPrivileges.class).usrMgmntObj();
      long usrMgmntObjId = method.getAnnotation(RequiresPrivileges.class).usrMgmntObjId();
    
      boolean authorized = false;
    
      // if the user wants to change a user management object (user or usergroup), the rank has to be higher than the
      // one the user wants to change. BUT user with the privilege PrivilegeType.USRMGA may edit without checking the
      // rank
      if ((usrMgmntObjClass == User.class || usrMgmntObjClass == Usergroup.class) && usrMgmntObjId > -1) {
    
        if (usrMgmntObjClass == User.class) {
          if (!authorizationService.hasHigherRank(currentUserService.getCurrentUserId(), usrMgmntObjId)) {
            if (authorizationService.hasPrivileges(privileges, currentUserService.getCurrentUserId())) {
              // user is authorized
              authorized = true;
            }
          }
        }
      }
    
      // check for privileges if user is not authorized already
      if (!authorized) {
        if (authorizationService.hasPrivileges(privileges, currentUserService.getCurrentUserId())) {
          // user is authorized
          return joinPoint.proceed();
        } else {
          // user is not authorized
          throw new AccessNotAllowedException(currentUserService.getCurrentUserId());
        }
      } else {
        return joinPoint.proceed();
      }
    }
    

    UserDto (或整个对象)到注释?是否可以为参数创建另一个注释 用户DTO 具有 ElementType.PARAMETER 然后在那里执行逻辑?

    1 回复  |  直到 8 年前
        1
  •  1
  •   msmani    8 年前

    如果您的应用程序使用Spring安全性,那么您可以简单地使用Spring的 @PreAuthorize . 否则,如果你不想现在这样做,那么你可以得到 UserDto 从方法的参数使用 JoinPoint#getArgs() .