【问题标题】:Using an email address as the unique identifier instead of username with Shiro for Grails在 Shiro for Grails 中使用电子邮件地址而不是用户名作为唯一标识符
【发布时间】:2013-08-22 09:20:08
【问题描述】:

我希望从在我的 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: icango.User”异常。

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

【问题讨论】:

    标签: authentication grails shiro


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您忘记更改以下行:

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

      删除或更改为:

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

      应该修复它。

      【讨论】:

      • 因为关闭了日志记录,我认为这些语句不会被评估,我错了。感谢您的提醒。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 2012-11-15
      • 2020-05-22
      • 1970-01-01
      相关资源
      最近更新 更多