【问题标题】:Grails spring-security-core plugin expects a username column in the role tableGrails spring-security-core 插件需要角色表中的用户名列
【发布时间】:2011-06-15 20:47:01
【问题描述】:

我有一个非常奇怪的问题。我已经在我的 Grails 1.3.6 应用程序中安装了 spring-security-core 1.0.1 并根据教程对其进行了配置 - 表是按顺序创建并由 BootStrap.groovy 填充的。我使用的是 PostgreSQL 8.4 数据库——整个过程都在我的本地主机上运行。现在,引导程序完美运行,但是当我尝试登录时,我得到一个异常,上面写着

org.hibernate.exception.SQLGrammarException: could not execute query

再往下:

Caused by: org.postgresql.util.PSQLException: ERROR: Column »username« does not exist

失败的查询如下:

select
    authrole0_.id as id1_,
    authrole0_.version as version1_,
    authrole0_.authority as authority1_ 
from
    auth_role authrole0_ 
where
    username=?

这没关系,因为 auth_role 表不应该有用户名列。但是为什么 Hibernate 需要一个用户名列,而它不应该是一个呢?

关于如何解决这个问题的任何线索?

我尝试了两种不同的休眠方言,但没有任何效果。我注意到表的映射有点奇怪 - 查找表也映射了。不幸的是,它在插件的文档中说我不应该更改它,因为插件需要该类。

我的课是这样的:

class AuthUser {

    String username
    String password
    boolean active
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    static mapping = {
        cache true
        username column: '`username`'
        password column: '`password`'
    }

    Set<AuthRole> getAuthorities() {
        UserRole.findAllByUser(this).collect { it.role } as Set
    }
}

import java.util.Set;

class AuthRole {

String authority

static constraints = {
    authority blank: false, unique: true
}
  }

  import org.apache.commons.lang.builder.HashCodeBuilder

class UserRole implements Serializable {

    AuthRole role
    AuthUser user

    boolean equals(other) {
        if (!(other instanceof UserRole)) {
            return false
        }

        other.user?.id == user?.id &&
                other.role?.id == role?.id
    }

    int hashCode() {
        def builder = new HashCodeBuilder()
        if (role) builder.append(role.id)
        if (user) builder.append(user.id)
        builder.toHashCode()
    }

    static UserRole get(long roleId, long userId) {
        find 'from UserRole where role.id=:roleId and user.id=:userId',
                [roleId: roleId, userId: userId]
    }

    static UserRole create(AuthRole role, AuthUser user, boolean flush = false) {
        new UserRole(role: role, user: user).save(flush: flush, insert: true)
    }

    static boolean remove(AuthRole role, AuthUser user, boolean flush = false) {
        UserRole instance = UserRole.findByRoleAndUser(role, user)
        instance ? instance.delete(flush: flush) : false
    }

    static void removeAll(AuthRole role) {
        executeUpdate 'DELETE FROM UserRole WHERE role=:role', [role: role]
    }

    static void removeAll(AuthUser user) {
        executeUpdate 'DELETE FROM UserRole WHERE user=:user', [user: user]
    }

    static mapping = {
        id composite: ['user', 'role']
        version false
    }
}

它们几乎就是插件创建它们的方式。

谢谢, 人

【问题讨论】:

  • 出于某种原因(也许我已经编辑了它)我在我的 Config.groovy 中有错误的类。而不是: grails.plugins.springsecurity.authority.className = 'auth.AuthRole' 我有: grails.plugins.springsecurity.authority.className = 'auth.AuthUser' 修复后我得到以下异常:2011-01-26 15:35:47,077 [http-9000-1] 错误 [/aquaWell].[default] - Servlet.service() 用于 servlet 默认抛出异常 groovy.lang.MissingPropertyException:没有这样的属性:为类启用:auth.AuthUser

标签: hibernate postgresql grails spring-security


【解决方案1】:

好的,找到了——“启用”是默认值。在我的课堂上,我定义了“活动”而不是“启用”。

在自定义方面,插件似乎真的很敏感;-)

【讨论】:

  • 在插件的未来版本中,它有望能够读懂你的想法和/或使用同义词库来检测enabled 属性缺失但有active 属性,所以它应该使用它。在那之前,它将是“敏感的”,并要求您配置 grails.plugins.springsecurity.userLookup.enabledPropertyName 属性。
猜你喜欢
  • 2016-03-23
  • 2012-09-29
  • 2012-02-17
  • 2011-11-27
  • 2012-11-04
  • 2015-06-18
  • 1970-01-01
  • 2015-11-07
  • 2012-11-28
相关资源
最近更新 更多