我在我的项目中使用弹簧和弹簧安全4.我必须使用ROLE_USER或ROLE_TIMER_TASK调用我的dao方法.
目前我正在使用此注释 -
@Secured({"ROLE_USER", "ROLE_TIMER_TASK"})
Run Code Online (Sandbox Code Playgroud)
这个@Secured注释只允许那些同时拥有这两个角色的用户,但是我想要由具有任何一个角色的用户调用此方法.
如果用户具有此角色中的任何一个角色并调用此方法,是否可以?
我已将弹簧安全配置为与群组配合使用.
我用这个scipt来创建域类:
grails s2-quickstart com.yourapp User Role --groupClassName=RoleGroup
Run Code Online (Sandbox Code Playgroud)
我假设一个用户可以有很多组,其中一个组可以有很多角色
这是User类中生成的方法的样子:
Set<RoleGroup> getAuthorities() {
UserRoleGroup.findAllByUser(this).collect { it.roleGroup }
}
Run Code Online (Sandbox Code Playgroud)
但是现在我看到脚本还创建了一个名为UserRole的类,它是User和Role之间的关联.那么用户也可以直接拥有多个角色?
我试过了,它保存在数据库中.我修改了这样的方法:
def getAuthorities() {
def authorities = UserRoleGroup.findAllByUser(this).collect { it.roleGroup }
authorities.addAll(UserRole.findAllByUser(this).collect { it.role })
return authorities
}
Run Code Online (Sandbox Code Playgroud)
现在,当我在User < - > Role Association中创建数据库中的条目时.我不能再登录了.我得到spring security的默认消息,基本上说没有找到凭据.
当我手动删除条目时,我可以再次登录.我认为这是因为该方法只返回RoleGroup对象.
我的问题是:
a)我可以在配置组时直接分配角色
b)如果不是,为什么脚本会创建这个类
c)如果是,我该怎么做?