2016-07-28 115 views
-1

我有域:春季安全检查用户有RoleGroup

用户hasOne RoleGroup 的hasMany角色

  • Exemples:

RoleGroup:管理,专业,客户,...

角色: ROLE_ACTION_1,ROLE_ACTION_2,...

我如何检查,如果用户有注释@Secured一个RoleGroup?

我需要检查用户是否包含RoleGroup的所有角色?

用户等级:

class User implements Serializable { 

    private static final long serialVersionUID = 1 

    static constraints = { 
     password blank: false, password: true 
     username blank: false, unique: true 
    } 

    static mapping = { 
     password column: '`password`' 
     version false 
     table schema: "CA" 
    } 

    static transients = ['springSecurityService'] 

    transient springSecurityService 

    transient boolean enabled = true 
    transient boolean accountExpired 
    transient boolean accountLocked 
    transient boolean passwordExpired 

    String username 
    String password 
    RoleGroup profile 

    Set<RoleGroup> getAuthorities() { 
     [profile] 
    } 
} 

RoleGroup类:

class RoleGroup implements Serializable { 

    private static final long serialVersionUID = 1 

    String name  

    Set<Role> getAuthorities() { 
     RoleGroupRole.findAllByRoleGroup (this)*.role 
    }  
} 
+0

你是说,如果弹簧支持角色组这是角色的列表不知道生根粉呢?。我会为扩展角色或角色组的角色进行数据库设计,然后在春季将它们视为角色。 Spring支持角色列表 - > @Secured({“Admin”,“Professional”}) – surya

+0

粘贴您的用户域类源和示例用户类实例。 –

+0

我粘贴我的课程 –

回答

1

我想你还没有完全掌握春季安全。

使用注释时 - 必须在配置中启用第一个注释 - 默认情况下是这种情况。

然后,您可以确保选择对整个控制器或使用这样的事情

@Secured(['ROLE_ADMIN', 'ROLE_USER']) 

它没有工作的所有的东西用户具有作为权力集团出方式的控制器操作。

虽然在你的代码中RoleGroup类粘贴您有:

getAuthorities() 

我已经调整了我的用户领域类,并添加以下内容:

Set<RoleGroup> getAuthorities() { 
     UserRoleGroup.findAllByUser(this)*.roleGroup 
    } 
    Set<RoleGroup> getAuthoritiesNames() { 
     UserRoleGroup.findAllByUser(this)*.roleGroup?.name 
    } 

所以当我有一个用户

ie User user = User.get(1L)

def authorities = user.getAuthorities() 
println "user ${user} has ${authorities}" 

这是一个包含所有当局

if (authorities.contains('ROLE_USER')) { 
println "WOOHOO" 
} 

随着春天的安全,你还可以用它GSPS内的列表:

<sec:ifAllGranted roles="ROLE_ADMIN"> 
show something 
</sec:ifAllGranted> 

所以回到你的问题:

您有:

Set<RoleGroup> getAuthorities() { 
     [profile] 
    } 

这是你在pu到位了吗?

从那里:

class RoleGroup implements Serializable { 

    private static final long serialVersionUID = 1 

    String name  

    Set<Role> getAuthorities() { 
     RoleGroupRole.findAllByRoleGroup (this)*.role 
    }  
} 

这应该列出你所有的部门

User user = User.get(1L) 
def authorities = user?.profile?.getAuthorities()