2012-11-26 57 views
2

刚刚在Groovy中开始编程并已停滞不前。我正在尝试创建可在我的引导程序中登录的用户,我已经看到了大量的教程,尽管复制和粘贴代码让我受到无数次错误的迎接。我现在已经开始关注似乎运行的代码,但用户根本就不在那里。使用SpringSecurityCore引导程序

我在做什么错?

import grails.timesecurity.* 
import timetracker2.* 

class BootStrap { 
    def springSecurityService 

    def init = { servletContext -> 

     def examples = [ 
      'rob' : [username: 'rob', password: 'password'] 
     ] 

     def userRole = Authority.findByAuthority("ROLE_USER") ?: new Authority(authority: "ROLE_USER").save() 
     def adminRole = Authority.findByAuthority("ROLE_ADMIN") ?: new Authority(authority: "ROLE_ADMIN").save() 

     if(!Person.count()) 
     { 
      userRole = new Authority(authority: 'ROLE_USER').save() 

      //String password = springSecurityService.encodePassword('password') 

      def user = new Person(
       username: "Rob", 
       password: springSecurityService.encodePassword("Password"), 
       enabled: true 
       ) 
      PersonAuthority.create user, userRole 
      //def user = new Person['Rob', password, enabled: true].save() 
     }  
    } 

    def destroy = {} 
} 

任何人都可以帮忙的是一个传奇!

回答

0

首先,我打赌你需要拨打save()在你的新Person。之后,确保您的Person对象正在验证中。

4

您不要在Person实例上调用save()。一旦这个问题解决了,你将无法登录,因为你正在关注一个旧的教程或博客文章,并且你明确地编码了密码。但生成的Person类已经这样做了,所以它将被双重编码。而为了进一步混淆事情,你正在用ROLE_USER创建第二个权威。

试试这个:

def userRole = Authority.findByAuthority("ROLE_USER") ?: new Authority(authority: "ROLE_USER").save() 
def adminRole = Authority.findByAuthority("ROLE_ADMIN") ?: new Authority(authority: "ROLE_ADMIN").save() 

if (!Person.count()) { 

    def user = new Person(
     username: "Rob", 
     password: "Password", 
     enabled: true).save() 

    PersonAuthority.create user, userRole 
} 
+0

你真棒,谢谢 – user1854751