grails - Spring Security 插件认证失败问题

标签 grails spring-security

编辑标题:与实际问题更相关

我正在尝试为我的测试应用程序设置 spring 安全性

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

把它放到 UrlMappings.groovy 中;

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

然后我把一个用户放在 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 = {
    }
}

这个打印 User.count() 返回 1,所以我知道用户已创建,打印“TEST”也有效,所以我知道它进入 if 块但是当我运行服务器时它失败了
Sorry, we were not able to find a user with that username and password.

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

最佳答案

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

如果没有,打开调试,你应该会看到一条关于它为什么失败的消息。将此添加到 Config.groovy 的 log4j 块中:

debug 'org.springframework.security'

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

关于grails - Spring Security 插件认证失败问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6946418/

相关文章:

grails - 我还如何获取Grails中2个日期之间的记录计数

mysql - 如何处理 Grails/GORM 中破裂的关系?

Grails 和 quartz : Bad value for type long

grails - 使用Grails Controller 进行模板导航

spring-boot - JUNIT5 @InjectMocks 抛出 NullPointerException

gradle - Bootrun因grails应用程序而失败,但构建成功

java - Spring security中registerGlobal()、configure()、configureGlobal()、configureGlobalSecurity的区别

java - 如何从spring mvc安全配置生成权限矩阵?

rest - Grails Spring Security REST被禁止使用403

java - Bean 'scopedTarget.oauth2ClientContext' 无法注册相同的 bean 名称已在类路径中定义