2012-07-29 35 views
2

我在SpringSecurity插件中使用Grails。我運行了s2-quickstart,因此用戶域類 被命名爲「User.groovy」,角色域類被命名爲「Role.groovy」。因此,映射類被命名爲「UserRole.groovy」。 然後我修改BootStrap.groovy來創建一個示例用戶,這導致了一個令人討厭的語法錯誤「Groovy:意外的令牌:UserRole @ line 19,column 2」。當調用「UserRole.create」時。BootStrap.groovy中的語法錯誤

這是我BootStrap.groovy中的文件:

import com.foo.Role 
import com.foo.User 
import com.foo.UserRole 


class BootStrap { 

    def springSecurityService 

    def userSecRole = Role.findByAuthority("ROLE_USER") ?: new Role()(authority: "ROLE_USER").save() 

    def user = new User(
     username: "user", 
     password: springSecurityService.encodePassword("user"), 
     enabled: true 
     ) 


    UserRole.create user, userSecRole  // <--- This is where the error happens 


    def init = { servletContext -> 
    } 
    def destroy = { 
    } 
} 

回答

2

你把你的代碼在主類定義。

該代碼應該是init瓶蓋內,即:

import com.foo.Role 
import com.foo.User 
import com.foo.UserRole 

class BootStrap { 

    def springSecurityService 

    def init = { servletContext -> 
        def userSecRole = Role.findByAuthority("ROLE_USER") ?: new Role()(authority: "ROLE_USER").save() 

        def user = new User(
        username: "user", 
        password: springSecurityService.encodePassword("user"), 
        enabled: true 
       ) 

        UserRole.create user, userSecRole     // <--- This is where the error happens 
    } 
    def destroy = { 
    } 
} 
+0

,似乎工作,謝謝 – kohlehydrat 2012-07-29 14:17:18