1

我希望从在我的 Grails 应用程序中使用用户名/密码组合进行身份验证转变为使用电子邮件/密码。我试图对 AuthControler.groovy 和 ShiroDbReal.groovy 进行更改以允许这样做,但它不允许我登录。

这是我的模型课

class User {
String email
String passwordHash
byte[] passwordSalt


static belongsTo = Account
static hasMany = [ roles: Role, permissions: String ]

static constraints = {
    email (nullable: false, blank: false, unique: true)
}
}

这是我的 AuthController 登录功能

def signIn = {
    def authToken = new UsernamePasswordToken(params.email, params.password as String)

    if (params.rememberMe) {
        authToken.rememberMe = true
    }

    def targetUri = params.targetUri ?: "/"

    def savedRequest = WebUtils.getSavedRequest(request)
    if (savedRequest) {
        targetUri = savedRequest.requestURI - request.contextPath
        if (savedRequest.queryString) targetUri = targetUri + '?' + savedRequest.queryString
    }

    try{
        SecurityUtils.subject.login(authToken)
        redirect(uri: targetUri)
    }
    catch (AuthenticationException ex){
        log.info "Authentication failure for user '${params.username}'."
        flash.message = message(code: "login.failed")
        redirect(action: "login", params: m)
    }
}

最后是我的 ShiroDbReal.groovy

    def authenticate(authToken) {
    log.info "Attempting to authenticate ${authToken.username} in DB realm..."
    def email = authToken.username

    if (email == null) {
        throw new AccountException("Null usernames are not allowed by this realm.")
    }

    def user = User.findByEmail(email)
    if (!user) {
        throw new UnknownAccountException("No account found for user [${username}]")
    }

    log.info "Found user '${user.username}' in DB"
    def account = new SimpleAuthenticationInfo(email, user.passwordHash, new SimpleByteSource(user.passwordSalt), "ShiroDbRealm")

    if (!credentialMatcher.doCredentialsMatch(authToken, account)) {
        log.info "Invalid password (DB realm)"
        throw new IncorrectCredentialsException("Invalid password for user '${username}'")
    }

    return account
}

我在“SecurityUtils.subject.login(authToken)”方法调用的调试器中收到“groovy.lang.MissingPropertyException: No such property: username for class: icago.User”异常。

我不太确定还能去哪里寻找,因为我找不到任何有关更改唯一标识符的文档。任何帮助将不胜感激。

4

2 回答 2

2

您忘记更改以下行:

log.info "Found user '${user.username}' in DB"

删除它或将其更改为:

log.info "Found user '${user.email}' in DB"

应该修复它。

于 2013-08-20T13:00:51.417 回答
0

顺便说一句,您的所有例外情况也是如此……您应该将所有出现的用户名更改为电子邮件。

于 2014-08-26T14:14:30.247 回答