简体   繁体   English

Grails 2 - 无法创建spring安全域对象

[英]Grails 2 - Cannot create spring security domain object

I have installed the spring security app from th grails plugin page after running grails s2-quickstart com.testApplication.secureApplication User Role to autogenerate the domain object I did grails run-app and got this exception: 我在运行grails s2-quickstart com.testApplication.secureApplication User Role之后从grails插件页面安装了spring security app来自动生成域对象我做了grails run-app并得到了这个异常:

|Loading Grails 2.3.4
|Configuring classpath
.
|Environment set to development
.................................
|Packaging Grails application
Precompiling AST Transformations ...
src C:\Users\GrailsWorkspace\testApplication\target\work\plugins\postgresql-extensions-0.6.1 C:\Users\GrailsWorkspace\testApplication\target\classes
Done precompiling AST Transformations!
..
|Compiling 3 source files
...................................................
|Running Grails application
Configuring Spring Security Core ...
... finished configuring Spring Security Core
Error |
2013-12-15 15:42:45,635 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport  - Unsuccessful: create table user (id int8 not null, version int8 not null, account_expired bool not null, account_locked bool not null, enabled bool not null, "password" varchar(255) not null, password_expired bool not null, username varchar(255) not null unique, primary key (id))
Error |
2013-12-15 15:42:45,638 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport  - ERROR: syntax error at "user"
  Position: 14
Error |
2013-12-15 15:42:45,688 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport  - Unsuccessful: alter table user_role add constraint FK143BF46A1E03E05D foreign key (user_id) references user
Error |
2013-12-15 15:42:45,688 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport  - ERROR: syntax error at "user"
  Position: 90
|Server running. 

The thing is my database is correctly configured, because I get the table role and user_role . 问题是我的数据库配置正确,因为我获得了表roleuser_role However user does not get generate in my postgresql db . 但是用户不能在我的postgresql db生成。 My implementation of my autogenerated user domain object looks like that: 我的自动生成的user domain object看起来像这样:

class User {

    transient springSecurityService

    String username
    String password
    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    static transients = ['springSecurityService']

    static constraints = {
        username blank: false, unique: true
        password blank: false
    }

    static mapping = {
        password column: '`password`'
    }

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

    def beforeInsert() {
        encodePassword()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
    }

    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }
}

I appreciate your answer! 我感谢你的回答!

'user' is a reserved name in Postgres. 'user'是Postgres中的保留名称。 You can avoid this by setting up a mapping in your domain class and use an alternate name. 您可以通过在域类中设置映射并使用备用名称来避免这种情况。

 static mapping = { table 'myusers' }

This way your domain class remains the same. 这样您的域类保持不变。 You could also escape the name similar to the way you have done with 'password'. 您也可以使用类似于“密码”的方式来逃避名称。

In the user domain, add the following mapping configuration to keep the domain/table name user similar to the way password column is skipped. 在用户域中,添加以下映射配置以使域/表名称user与跳过password列的方式类似。

static mapping = {
    table '`user`'
    password column: '`password`'
}

A blog post that explains the problem and the solution can be found here 可以在此处找到解释问题和解决方案的博客文章

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

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