【问题标题】:Fetch persons with specific authorities - spring security with grails获取具有特定权限的人员 - 带有 grails 的弹簧安全性
【发布时间】:2012-09-29 06:00:16
【问题描述】:

我有典型的 Spring Security 域类布局(Person、Authority 和连接表 PersonAuthority)。我进行了以下查询,但我不知道如何建立正确的标准来获取只有特定权限的人(最后一个“if”块)。

public static def query(Map params=[:]){
    def criteria = Person.createCriteria();
    def rows = criteria.list(max:params.max,offset:params.offset){
    order(params.column?:"id",params.order?:"asc")
        if(params.id){
            idEq (params.id as Long);
        }        
        if(params.username){
            ilike "username","%"+params.username+"%"    
        }
        if(params.email){
            ilike "email","%"+params.email+"%"
        }
        if(params.accountLocked){
            eq "accountLocked",Boolean.valueOf(params.accountLocked)
        }
        if(params.enabled){
            eq "enabled",Boolean.valueOf(params.enabled)
        }
        if(params.passwordExpired){
            eq "passwordExpired",Boolean.valueOf(params.passwordExpired)
        }
        if(params.authorities){
            inList "authority",params.authorities;
        }
    }
    return rows;
}

对于这个查询,我得到org.hibernate.QueryException: could not resolve property: authority

【问题讨论】:

  • 您在 Person 中的字段不是“authorities”?

标签: hibernate grails join criteria-api


【解决方案1】:

如果您没有修改默认 Spring Security 选项和类中的任何内容,那么我想您没有 authorityauthorities 字段。

但是,您的 Person 类中应该有一个名为 getAuthorities() 的方法,该方法返回一组权限

【讨论】:

  • 我想按权威过滤人。如果我想使用 'getAuthorities()' 我需要在结果集中的每个 'Person' 上调用它,例如: def list = ['ROLE_SOME','ROLE_OTHER']; def res = 查询(); def filterRes = res.find{list.contains(it.authorities)};这是唯一的方法吗?
  • 您可以直接使用person.authorities,但您不能单独使用authorities,它不适用于条件或任何其他搜索,因为该字段是由SpringSecurity动态添加的(通过getAuthorities()方法)
猜你喜欢
  • 1970-01-01
  • 2014-07-21
  • 2013-04-12
  • 2017-01-01
  • 2012-01-07
  • 2014-02-09
  • 2014-12-01
  • 2015-01-09
  • 2014-05-20
相关资源
最近更新 更多