Search code examples
javaspringspring-mvcannotations

Path variable usage as annotation param


I've got simple spring mvc controller

@GetMapping("/{s}")
@PreAuthorize("@myMethod(s, Constant.WRITE")
public ResponseEntity<MyDto> getDto(
    @PathVariable("s") String s
) {}

So the problem is to pass the path variable to PreAuthorize annotation. Can SPeL help me in this situation?


Solution

  • You can use Expression-Based Access Control

    @GetMapping("/{s}")
    @PreAuthorize("@myMethod(#s, Constant.WRITE")
    public ResponseEntity<MyDto> getDto(
        @PathVariable("s") String s
    ) {}