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

你能在春季将动态值设置为@PreAuthorize吗?

  •  0
  • erotsppa  · 技术社区  · 5 年前

    @PreAuthorize("hasAuthority('CREATE_USER_PRIVILEGE')")
    

    但我希望CREATE_USER_权限来自一个函数()。这可能吗?

    0 回复  |  直到 5 年前
        1
  •  2
  •   Dani Mesejo    5 年前

    你可以这样做:

    @RestController
    class FooController {
    
        @PreAuthorize("hasAuthority(@securityService.privilege)")
        @GetMapping("/")
        public ResponseEntity<String> helloSecurity(@RequestParam("id") Integer id){
            return ResponseEntity.ok("Hello World");
        }
    
    
    }
    
    @Service("securityService")
    class SecurityService {
    
        public String getPrivilege(){
            return "CREATE_USER_PRIVILEGE";
        }
    
    }
    
        2
  •  0
  •   Mohammed Housseyn Taleb    5 年前

    this great article

    首先必须使用构造函数或注释自动连接服务,然后可以使用Spel语言来使用它,如下面的示例所述

    @RequestMapping(value="/id/{domainObjectId}/dostuff", method=RequestMethod.POST, produces="application/json")
    @PreAuthorize(value="hasRole('ROLE_DomainObjectAdmin') or @domainObjectServiceImpl.findDomainObject(#domainObjectId).getOwners().contains(#userAccount.getEmployee())")
    public String setObjectiveComplete(@PathVariable String domainObjectId, UserAccount userAccount) {
    // Do stuff
    }