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
然后在那里执行逻辑?