查找具有特定弹簧安全角色的用户

Tho*_*ley 0 grails groovy hibernate hql spring-security

我有以下内容:

def userInstance=User.findAll("from User u where u.merchant is not null and u.merchant.id = ? and u.authorities.authority.contains('ROLE_MERCHANT')", merchantId)
Run Code Online (Sandbox Code Playgroud)

并得到以下错误:

org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: ( near line 1, column 121 [from com.testing.tests.User u where u.merchant is not null and u.merchant.id = ? and u.authorities.authority.contains('ROLE_MERCHANT')]
    at $Proxy20.createQuery(Unknown Source)
    at testing.admin.UserService.findByMerchantId(UserService.groovy:14)
Run Code Online (Sandbox Code Playgroud)

如何仅返回具有特定角色的用户?

谢谢

And*_*ess 5

如果您查看默认实现,User#getAuthorities您将注意到该属性本身是使用动态查找程序调用实现的:

UserRole.findAllByUser(this).collect { it.role } as Set
Run Code Online (Sandbox Code Playgroud)

因此,您无法authorities在HSQL查询中引用,但您可以通过UserRole多种方式查询具有关联域类的特定角色的用户:

def users = UserRole.withCriteria {
   role{
       eq 'authority', 'ROLE_SOMETHING'
   }
   projections{
      property 'user'
   }
}
Run Code Online (Sandbox Code Playgroud)

要么

def users = UserRole.findAllByRole(domainRole)?.user
Run Code Online (Sandbox Code Playgroud)