繁体   English   中英

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

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

我有一个很奇怪的问题。 我已经在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表不应具有username列。 但是,为什么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
    }
}

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

谢谢,al

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

似乎在定制时,该插件确实很敏感;-)

暂无
暂无

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

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