0

I have a spring application, I want to add high-level validation against all calls to every controller, that will validate path variable value for permission manners

example:

@RestController
@RequestMapping(value = "/user/{userId}", produces = "application/json")
public class ValidationController {


    @PostMapping(value = "/update-status")
    @ResponseBody
    public void updateStatus(@PathVariable Integer userId) {
        ////////some code;
    }
}
Hard Worker
  • 995
  • 11
  • 33
  • First of all your method doesn't have `@PathVariable`. Can you elaborate a little more on what you want to do? Did the code you shared help you? – İsmail Y. Mar 10 '22 at 13:41

1 Answers1

0

Should be something similar to

@Component
@Aspect
@Order(1)
public class ValidationTokenInterceptor {
    
    @Around("@within(controller)")
    public Object checkController(ProceedingJoinPoint thisJoinPoint, Controller controller) throws Throwable { 
                Object[] args = thisJoinPoint.getArgs();
        MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getStaticPart().getSignature();
        Method method = methodSignature.getMethod();
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        assert args.length == parameterAnnotations.length;
        for (int argIndex = 0; argIndex < args.length; argIndex++) {
            for (Annotation annotation : parameterAnnotations[argIndex]) {
                if (!(annotation instanceof RequestParam))
                    continue;
                RequestParam requestParam = (RequestParam) annotation;
                if (! "accessToken".equals(requestParam.value()))
                    continue;
                System.out.println("  " + requestParam.value() + " = " + args[argIndex]);
            }
        }
        joinPoint.proceed(joinPoint.getArgs());
    }   
}

Credit also to this answer: Retrieve parameter value from ProceedingJoinPoint

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
Hard Worker
  • 995
  • 11
  • 33