【问题标题】:Sorry, we were not able to find a user with that username and password. (Grails-Spring Security Core)抱歉,我们无法找到具有该用户名和密码的用户。 (Grails-Spring 安全核心)
【发布时间】:2013-12-03 06:06:19
【问题描述】:

我是 Grails 的新手,正在尝试使用 Spring Security Core 插件为用户分配角色。我将我的项目称为“MyProject”。

在为我的模型生成控制器和视图后,在 Config.groovy 中,我设置了所需的登录名以便访问 /userMyProject/ :

grails.plugin.springsecurity.controllerAnnotations.staticRules = [
    '/userMyProject/**':               ['ROLE_ADMIN']
]

然后在BootStrap.groovy 我自动创建一些用户和角色:

def init = { servletContext ->
    def role1 = new Role();
    role1.setAuthority("ROLE_ADMIN")
    role1.save();

    def user1 = new UserMyProject();
    // ... assigning name, username, password...
    user1.save();

    new UserMyProjectRole(userCwitter:user1, role:role1).save();
}

但是当我转到我的UserMyProjectController 页面时,它正确打开了登录页面,但总是大喊抱歉,我们无法找到具有该用户名和密码的用户。 当我'我可以在我的数据库中看到它。

感谢您的帮助。

【问题讨论】:

  • 根据您使用的插件版本,可能是您分配密码的方式。您可以使用插件版本和用于填充用户密码的代码来更新您的问题吗?
  • @Gregg 我正在使用插件 2.0,而我只使用了user.setPassword('password'),也许这是问题所在?
  • 取决于,查看您的用户域。使用 setPassword() 时密码是否被散列?
  • 如果密码没有在用户域中进行散列,那么 Benoit 的答案是正确的方法。
  • 我在用户中有这些方法:def beforeInsert() { encodePassword() }def beforeUpdate() { if(isDirty('password')) { encodePassword() } }protected void encodePassword() { password = springSecurityService.encodePassword(password) }

标签: grails spring-security


【解决方案1】:

在部分:

// ... assigning name, username, password...

您是否正在对密码进行编码(1.2 版之前)?

def springSecurityService
...
springSecurityService.encodePassword('password')

关于如何setup Spring security with Grails的很好的参考。

编辑:

您确定用户已保存在数据库中吗?如果您的用户在这里,请添加save(flush: true, failOnError: true) 并检查数据库。

【讨论】:

  • 在较新版本的插件中,如果使用脚手架用户域,则不需要这样做。
  • 感谢您的回答。我试过了,但仍然得到这个错误。
猜你喜欢
  • 2012-10-27
  • 2018-09-11
  • 2013-12-26
  • 2016-04-16
  • 2014-11-24
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多