Spring Security Plug-in不适用于多个属性。为什么?

时间:2011-12-31 06:59:06

标签: grails groovy spring-security grails-plugin

我有一个这样的课程:

class User {
    String name;
    String password;    
    static hasMany = [ authorities : testing.Role ]
    static belongsTo = Role
    static constraints = {
    name(unique:true,size:4..10)
    }
}

我已经配置了Spring Security Plugin(acegi),并且它按预期工作。但问题是,当我将belongsTo类中的User更改为类似内容时:

static belongsTo = [ Role , company : Company]

我尝试运行grails run-app我得到这样的错误:

/home/anto/anto/test/testing/grails-app/domain/testing/User.groovy: 7: Unexpected node type: EXPR found when expecting type: LABELED_ARG at line: 7 column: 25. File: /home/anto/anto/test/testing/grails-app/domain/testing/User.groovy @ line 7, column 25.
       static belongsTo = [Role , company : Company ] 
                           ^

1 error

为什么这个语法会抛出错误?我犯了哪个错误?

编辑:

现在,在收到少数人的答案后,我将代码更改为:

static belongsTo = [ role: Role , company : Company]

但这次我的错误甚至更大了!

2011-12-31 14:23:57,304 [Thread-9] ERROR context.ContextLoader  - Context initialization failed 
Message: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: No owner defined between domain classes [class testing.Role] and [class testing.User] in a many-to-many relationship. Example: static belongsTo = testing.User 
   Line | Method 
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask 
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker 
|   908 | run      in     '' 
^   662 | run . .  in java.lang.Thread 

Caused by GrailsDomainException: No owner defined between domain classes [class testing.Role] and [class testing.User] in a many-to-many relationship. Example: static belongsTo = testing.User 
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask 
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker 
|   908 | run      in     '' 
^   662 | run . .  in java.lang.Thread 
| Error 2011-12-31 14:23:57,401 [Thread-9] ERROR context.GrailsContextLoader  - Error executing bootstraps: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: No owner defined between domain classes [class testing.Role] and [class testing.User] in a many-to-many relationship. Example: static belongsTo = testing.User 
Message: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: No owner defined between domain classes [class testing.Role] and [class testing.User] in a many-to-many relationship. Example: static belongsTo = testing.User 
   Line | Method 
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask 
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker 
|   908 | run      in     '' 
^   662 | run . .  in java.lang.Thread 

Caused by GrailsDomainException: No owner defined between domain classes [class testing.Role] and [class testing.User] in a many-to-many relationship. Example: static belongsTo = testing.User 
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask 
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker 
|   908 | run      in     '' 
^   662 | run . .  in java.lang.Thread

现在,发生什么事了?

但如果我这样做:

static belongsTo = Role

一切都很好。我不知道为什么以前的语法会抛出这样的错误!

我是Grails的初学者,我需要帮助。

注意: 我正在使用Grails 2.0

提前致谢。

3 个答案:

答案 0 :(得分:3)

这根本不是安全问题,而是GORM问题。 [ Role , company : Company]毫无意义,因为您将Groovy的List语法([Foo, Bar, Baz])与其Map语法([foo: Foo, bar: Bar, baz: Baz])混合在一起。你必须选择一个。

答案 1 :(得分:2)

应该是static belongsTo = [role: Role , company : Company ]吗?

答案 2 :(得分:-1)

堆栈跟踪似乎表明问题出在关系的另一端。角色和公司课程如何?它们都应该具有“用户用户”

之类的属性