Grails Spring Security Core 2.0-RC4问题"很抱歉找不到那个用户....."

时间:2014-09-30 01:49:00

标签: grails spring-security

我正在尝试保护简单的Grails应用。拔出我的头发试图与在BootStrap.groovy中创建的管理员用户进行身份验证。

BootStrap.groovy中:

class BootStrap {
    def springSecurityService

    def init = { servletContext ->

    def userRole = SecRole.findByAuthority('ROLE_USER') ?: new SecRole(authority: 'ROLE_USER').save(failOnError: true)
    def adminRole = SecRole.findByAuthority('ROLE_ADMIN') ?: new SecRole(authority: 'ROLE_ADMIN').save(failOnError: true)
    def adminUser = SecUser.findByUsername('admin') ?: new SecUser( username: 'admin', password: 'admin', enabled: true  ).save(failOnError: true)

    println(userRole.all)
    println(adminRole.getAuthority())
    println(adminUser.getUsername())

    if (!adminUser.authorities.contains(adminRole)) { SecUserSecRole.create( adminUser, adminRole ) }

    }

    def destroy = {
    }
}

控制器:

import grails.plugin.springsecurity.annotation.Secured;

class EmployeeController {

    @Secured(['ROLE_ADMIN'])
    def index() { 
        render "Some things are just private"
    }
//  def scaffold = true
}

Config.groovy中:

// Added by the Spring Security Core plugin:
grails.plugin.springsecurity.userLookup.userDomainClassName = 'SecUser.SecRole'
grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'SecUser.SecRoleReqeustmap'
grails.plugin.springsecurity.authority.className = 'SecUser.Reqeustmap'
grails.plugin.springsecurity.controllerAnnotations.staticRules = [
    '/':                              ['permitAll'],
    '/index':                         ['permitAll'],
    '/index.gsp':                     ['permitAll'],
    '/assets/**':                     ['permitAll'],
    '/**/js/**':                      ['permitAll'],
    '/**/css/**':                     ['permitAll'],
    '/**/images/**':                  ['permitAll'],
    '/employee/**':                   ['permitAll'],
    '/**/favicon.ico':                ['permitAll']
]

URLMapping.groovy:

class UrlMappings {

    static mappings = {
        "/$controller/$action?/$id?(.$format)?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(view:"/index")
        "500"(view:'/error')
        "/login/$action?"(controller:"login")
        "/logout/$action?"(controller:"logout")
    }
}

Database.groovy:

environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
            //url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
            url = "jdbc:mysql://localhost:3306/test?autoreconnect=true"
        }
    }

...

添加了调试但没有显示任何值。

我错过了什么?当我获得默认用户登录/身份验证页面并输入管理员/管理员凭据时,应用程序将继续返回"抱歉无法找到具有该用户的用户...."

提前谢谢

使用帖子中标识的技术。我得到了以下结果: 我将事件处理程序直接添加到配置中,它为错误提供了更多信息: uthentication.ProviderManager - 使用org.springframework.security.authentication.dao.DaoAuthenticationProvider进行身份验证尝试 用户admin的ERROR auth失败:指定的用户域类'SecUser.SecRole'不是域类 2014-09-30 18:48:43,076 [http-bio-9191-exec-6] DEBUG rememberme.TokenBasedRememberMeServices - 交互式登录尝试失败。 2014-09-30 18:48:43,076 [http-bio-9191-exec-6] DEBUG rememberme.TokenBasedRememberMeServices - 取消cookie 2014-09-30 18:48:43,099 [http-bio-9191-exec-6] DEBUG web.DefaultRedirectStrategy - 重定向到'/ shareRef / login / authfail?login_error = 1'

我不确定为什么springsecurity表明SecUser.SecRole不是域类。

我有一个在运行grails s2脚本后自动创建的SecUserSecRole域类。

1 个答案:

答案 0 :(得分:0)

我做了一些博客文章,介绍了可用于诊断此类问题的一些技巧 - 请查看http://burtbeckwith.com/blog/?p=2003http://burtbeckwith.com/blog/?p=2029