Spring Security Plugin身份验证失败问题

时间:2011-08-04 18:10:38

标签: grails spring-security

EDITED HEADER:与实际问题更相关

我正在尝试为我的测试应用程序设置spring security

我安装了插件,创建了用户和角色类;

将此内容添加到UrlMappings.groovy;

        "/login/$action?"(controller: "login")
    "/logout/$action?"(controller: "logout")

然后我将用户置于引导程序中,如下所示,

import org.project.auth.Role
import org.project.auth.User
import org.project.auth.UserRole;

class BootStrap {
    def springSecurityService
    def init = { servletContext ->
        def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true,flush:true)
        def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true,flush:true)

        def adminUser = User.findByUsername('admin') ?: new User(

                username: 'admin',

                password: springSecurityService.encodePassword('admin'),

                enabled: true).save(failOnError: true,flush:true)

        print User.count()          

        if (!adminUser.authorities.contains(adminRole)) {
                    print "TEST"
            UserRole.create adminUser, adminRole,true
        }
    }
    def destroy = {
    }
}

此打印User.count()返回1因此我知道用户已创建,打印“TEST”也可以正常工作,因此我知道它进入了if块,但是当我运行服务器时它失败了

Sorry, we were not able to find a user with that username and password.

我使用Grails 2.0.0.M1,您认为这可能是问题吗?

2 个答案:

答案 0 :(得分:9)

1.2版本的插件中的用户域类会为您加密密码。所以像这样的旧代码使用springSecurityService双重编码。将password: springSecurityService.encodePassword('admin')更改为password: 'admin',它应该有效。

如果没有,请打开调试,你会看到一条消息,说明它失败的原因。将其添加到log4j块中的Config.groovy:

debug 'org.springframework.security'

为了安全起见我也要改变 if (!adminUser.authorities.contains(adminRole)) {if (!UserRole.findByUserAndRole(adminUser, adminRole)) { `

答案 1 :(得分:0)

我面临同样的问题,但经过一番搜索后发现了这个问题。 http://jira.grails.org/browse/GPSPRINGSECURITYUI-33http://jira.grails.org/browse/GPSPRINGSECURITYUI-27 希望这可以帮助。但我无法找到解决此问题的下一个稳定版本。

相关问题