【问题标题】:Grails Spring Security Core 2.0-RC4 issue "Sorry not able to find user with that ....."Grails Spring Security Core 2.0-RC4 问题“抱歉找不到用户......”
【发布时间】:2014-11-24 12:48:12
【问题描述】:

我正在尝试保护一个简单的 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 进行身份验证尝试 用户管理员的错误身份验证失败:指定的用户域类“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] 调试 rememberme.TokenBasedRememberMeServices – 取消 cookie 2014-09-30 18:48:43,099 [http-bio-9191-exec-6] 调试 web.DefaultRedirectStrategy - 重定向到'/shareRef/login/authfail?login_error=1'

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

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

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    我写了几篇博文,描述了一些可用于诊断此类问题的技术 - 请查看 http://burtbeckwith.com/blog/?p=2003http://burtbeckwith.com/blog/?p=2029

    【讨论】:

    • 谢谢。我现在去看看。
    • 从您在博客 cmets 中发布的错误来看,您似乎有一个名为 SecUser.SecRole 的域类 - 对吗?类名中间不能有句点。
    • 再次感谢您提出我的问题。 SecUser.SecRole 在我的域包中不存在,所以我不确定它为什么要寻找那个类。
    • 根据您对我的 SecUser.SecRole 的询问以及您之前关于如何调试 Spring Security 的帖子,找到了我的问题的解决方案。调试表明 SecUser.SecRole 是域类的名称,即使它不存在。问题出在我的 Config.groovy grails.plugin.springsecurity 设置错误。我将 userDomainClassName、authorityJointClassName 和 authority.className 的 Config.groovy 设置分别更新为 SecUser、SecUserSecRole 和 SecRole,现在一切正常。
    • 出于文档目的:grails.plugin.springsecurity.useSecurityEventListener = true grails.plugin.springsecurity.onAbstractAuthenticationFailureEvent = { e, appCtx -> println "\nERROR auth failed for user $e.authentication.name: $e.exception.message\n" } 我用它来获取正确的调试信息来帮助解决问题。
    猜你喜欢
    • 2015-08-01
    • 2014-10-04
    • 1970-01-01
    • 2014-10-12
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 2012-11-19
    相关资源
    最近更新 更多