【问题标题】:Grails Spring-Security-Core plugin - Cannot authenticate userGrails Spring-Security-Core 插件 - 无法验证用户
【发布时间】:2012-02-17 15:13:51
【问题描述】:

所以我正在努力让 spring-security-core-1.2.7.1 与 Grails 2.0 一起使用...

我查看了教程并运行了 s2。读到新插件会为你加密密码,所以我的引导程序看起来像:

def userRole  = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true)
    def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true)

    def adminUser = User.findByUsername('admin') ?: new User(
        username: 'admin',
        password: "admin",
        enabled: true).save(failOnError: true)

        def testUser = User.findByUsername('test') ?: new User(
            username: 'test',
            password: "test",
            enabled: true).save(failOnError: true)

    if (!adminUser.authorities.contains(adminRole)) {
        UserRole.create adminUser, adminRole
    }

    if (!testUser.authorities.contains(userRole)) {
        UserRole.create testUser, userRole
    }

我可以查看 H2 数据库,我可以看到用户、他们的编码密码、角色已创建,并且可以看到用户角色映射也已正确创建。

但是,我仍然收到“抱歉,我们无法找到具有该用户名和密码的用户”。在两个用户的登录提示处。

我已打开 log4j debug 'org.springframework.security' 但我真正从日志中得到的只是:

2012-01-23 23:08:44,875 ["http-bio-8080"-exec-5] DEBUG dao.DaoAuthenticationProvider  - Authentication failed: password does not match stored value

【问题讨论】:

  • 冲洗有时会有所帮助,你应该试试!

标签: grails spring-security grails-plugin


【解决方案1】:

我看不出您的代码有任何明显错误。我正在使用相同版本的 Grails 和 spring 安全插件,Bootstrap.groovy 中的以下代码适用于我:

def init = { servletContext ->

    // create some roles
    def userRole = createRole('ROLE_USER')
    def adminRole = createRole('ROLE_ADMIN')

    // create some users
    User admin = createUser('Admin', 'admin@mailinator.com', adminRole)
    User user = createUser('User', 'user@yahoo.co.uk', userRole)
}

private User createUser(name, username, role) {

    def defaultPassword = 'password'

    User user = User.findByUsername(username) ?: new User(
            name: name,
            username: username,
            password: defaultPassword,
            passwordConfirm: defaultPassword,
            enabled: true).save()

    if (!user.authorities.contains(role)) {
        UserRole.create user, role
    }
    user
}

private createRole(String roleName) {
    Role.findByAuthority(roleName) ?: new Role(authority: roleName).save()
}

【讨论】:

  • Ug...这简直要了我的命...我什至将其追溯到创建 GrailsUser 的插件...它正在数据库中查找用户并传递适当的信息...
  • 好像是多个数据源的问题。一旦我注释掉其他数据源(它不应该使用它)它就起作用了....不好。
【解决方案2】:

在OP的原始代码中,为什么用户名是单引号,密码是双引号。这可能是问题所在。

【讨论】:

    【解决方案3】:

    您可以通过更新 User 类来解决多数据源问题。 见https://stackoverflow.com/q/13296594

    【讨论】:

      【解决方案4】:

      我确实有类似的问题。是因为我忘记添加了

      grails.plugin.springsecurity.userLookup.userDomainClassName ='yourpackage.User'
      grails.plugin.springsecurity.userLookup.authorityJoinClassName =yourpackage.UserRole'
      grails.plugin.springsecurity.authority.className ='yourpackage.Role'
      

      在该身份验证工作之后。

      【讨论】:

        猜你喜欢
        • 2015-07-20
        • 2016-03-23
        • 2012-09-29
        • 2012-10-17
        • 2012-11-04
        • 2015-06-18
        • 1970-01-01
        • 2013-11-13
        • 2012-08-28
        相关资源
        最近更新 更多