0

我正在使用 Sec 插件 3.1.2 测试 Grails 3.2.9。

在引导程序中创建了一个角色为 ROLE_ADMIN 的用户,并为测试控制器“note”添加了对 interceptUrlMap 的权限。使用该用户成功登录后,我在日志中看到我的管理员拥有 ROLE_NO_ROLES 并且被拒绝访问笔记控制器。

用户、角色和用户角色关联在数据库中。

  def adminRole = Role.findOrSaveByAuthority('ROLE_ADMIN')
  def admin = new User(username: 'admin', password: 'admin').save()
  UserRole.create admin, adminRole

应用程序.groovy

grails.plugin.springsecurity.userLookup.userDomainClassName = 'com.cabolabs.security.User'

grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'com.cabolabs.security.UserRole'
grails.plugin.springsecurity.authority.className = 'com.cabolabs.security.Role'
grails.plugin.springsecurity.authority.groupAuthorityNameField = 'authorities'
grails.plugin.springsecurity.useRoleGroups = true

...

grails.plugin.springsecurity.interceptUrlMap = [
    [pattern: '/note/**',        access: ['ROLE_ADMIN']],
    [pattern: '/patient/**',     access: ['ROLE_ADMIN']],
    [pattern: '/login/**',       access: ['permitAll']],
    [pattern: '/logout',         access: ['permitAll']],
    [pattern: '/logout/**',      access: ['permitAll']],
    [pattern: '/dbconsole/**',   access: ['permitAll']],
    [pattern: '/**',             access: ["IS_AUTHENTICATED_FULLY"]]
]

...

登录后的日志,当我尝试转到 /note/index 时

2017-05-11 23:32:15.793 DEBUG --- [nio-8091-exec-4] o.s.s.w.a.i.FilterSecurityInterceptor    : Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@8d34560b: Principal: grails.plugin.springsecurity.userdetails.GrailsUser@586034f: Username: admin; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_NO_ROLES; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: F0902A19E19C23D30B452C332C6C5728; Granted Authorities: ROLE_NO_ROLES
2017-05-11 23:32:15.793 DEBUG --- [nio-8091-exec-4] o.s.s.a.h.RoleHierarchyImpl              : getReachableGrantedAuthorities() - From the roles [ROLE_NO_ROLES] one can reach [ROLE_NO_ROLES] in zero or more steps.
2017-05-11 23:32:15.805 DEBUG --- [nio-8091-exec-4] tContextHolderExceptionTranslationFilter : Access is denied (user is not anonymous); delegating to AccessDeniedHandler

关于发生了什么的任何想法?

试图在 Sec 插件文档上找到指针,但它只提到 ROLE_NO_ROLES 一次,并且在用户没有角色时分配,不是这种情况。

4

1 回答 1

0

如果你有 useRoleGroups = true 你必须填充你的数据库:

Role adminRole = new Role("ROLE_ADMIN").save()
RoleGroup adminGroup = new RoleGroup("GROUP_ADMIN").save()
RoleGroupRole.create(adminGroup, adminRole, true)
User user = new User("<username>", "<password>").save()
UserRoleGroup.create(user, adminGroup, true)

代替

def adminRole = Role.findOrSaveByAuthority('ROLE_ADMIN')
def admin = new User(username: 'admin', password: 'admin').save()
UserRole.create admin, adminRole
于 2017-07-22T16:54:31.783 回答