Can you set a dynamic value to @PreAuthorize in Spring?

Right now I use


But I want the CREATE_USER_PRIVILEGE to come from a function(). Is this possible?


You could do something like this:

class FooController {

    public ResponseEntity<String> helloSecurity(@RequestParam("id") Integer id){
        return ResponseEntity.ok("Hello World");


class SecurityService {

    public String getPrivilege(){
        return "CREATE_USER_PRIVILEGE";


Source: stackoverflow