Spring Security Plugin身份验证失败问题

add*_*dd9 4 grails spring-security

EDITED HEADER:与实际问题更相关

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

我安装了插件,创建了User和Role类;

把这个放到UrlMappings.groovy;

        "/login/$action?"(controller: "login")
    "/logout/$action?"(controller: "logout")
Run Code Online (Sandbox Code Playgroud)

然后我把用户放在bootstrap中,如下所示,

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 = {
    }
}
Run Code Online (Sandbox Code Playgroud)

这个打印User.count()返回1所以我知道用户已创建,打印"TEST"也可以,所以我知道它进入if块但是当我运行服务器它失败了

Sorry, we were not able to find a user with that username and password.
Run Code Online (Sandbox Code Playgroud)

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

Bur*_*ith 9

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

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

debug 'org.springframework.security'
Run Code Online (Sandbox Code Playgroud)

同时为了保险起见我会改变 if (!adminUser.authorities.contains(adminRole)) {if (!UserRole.findByUserAndRole(adminUser, adminRole)) { `