简体   繁体   English

Grails Spring-security-core插件需要角色表中的用户名列

[英]Grails spring-security-core plugin expects a username column in the role table

I have a really strange problem. 我有一个很奇怪的问题。 I've installed the spring-security-core 1.0.1 in my Grails 1.3.6 application and configured it according to the tutorial - tables are created orderly and populated by BootStrap.groovy. 我已经在Grails 1.3.6应用程序中安装了spring-security-core 1.0.1并根据教程进行了配置-表是按顺序创建的,并由BootStrap.groovy填充。 I'm using a PostgreSQL 8.4 database --the whole thing is running on my localhost. 我正在使用PostgreSQL 8.4数据库-整个事情都在我的本地主机上运行。 Now, the bootstrap works perfectly, but when I try to login I get an exception which says 现在,引导程序可以正常运行,但是当我尝试登录时,出现一个异常,上面写着

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

and further down: 再往下走:

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

The query that fails is the following: 失败的查询如下:

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

and this is OK, because the auth_role table is not supposed to have username column. 可以,因为auth_role表不应具有username列。 But why does Hibernate expect a username column, where it shouldn't be one? 但是,为什么Hibernate期望一个用户名列而不应该是一个呢?

Any clues as how to resolve this? 有什么线索可以解决这个问题吗?

I've tried two different hibernate diaclects with no effect. 我已经尝试了两种不同的冬眠方法,但都没有效果。 I've noticed that the mapping of the table is somehow curious - with the lookup table mapped as well. 我注意到表的映射有点奇怪-查找表也被映射了。 Unfortunately it says in the documentation of the plugin that I'm not supposed to change it, because the class is needed by the plugin. 不幸的是,它在插件的文档中说我不应该更改它,因为插件需要该类。

My classes look like that: 我的课看起来像这样:

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
    }
}

They are pretty much the way the plugin created them. 它们几乎是插件创建它们的方式。

Thanks, al 谢谢,al

OK, found it - "enabled" is a default value. 好的,找到它-“启用”是默认值。 In my class I defined "active" instead of "enabled". 在我的课堂上,我定义了“活动”而不是“启用”。

Seems that the plugin is really touchy, when it comes to customizing ;-) 似乎在定制时,该插件确实很敏感;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM