繁体   English   中英

Grails + Spring Security:无法登录

[英]Grails + Spring Security: unable to login

我刚刚开始学习Grails和Spring,我已经按照官方教程创建了一个登录系统。 但我无法登录,“用户名或密码不匹配”。 我知道在90%的情况下这是由于双重编码或多个数据源(这也导致双重编码),但我也没有做到。

class BootStrap {

    def init = { servletContext ->

        def adminRole = new SecurityRole(authority: 'ROLE_ADMIN').save(failOnError: true, flush: true)
        def userRole = new SecurityRole(authority: 'ROLE_USER').save(failOnError: true, flush: true)

        def testUser = new User(username: 'admin', password: 'admin')
        testUser.save(failOnError: true, flush: true)

        SecurityUserSecurityRole.create testUser, adminRole, true

        assert User.count() == 1
        assert SecurityRole.count() == 2
        assert SecurityUserSecurityRole.count() == 1

        println testUser.username
        println testUser.password
    } 

弹簧安全核:2.0 RC2
grails 2.3.3

我在一些项目中遇到了类似的问题,对我来说一直是一个双重编码问题。 我使用的是Spring Security Plugin的早期版本,但这种技术可以确保它不会进行双重编码。 同样,我在不同的版本,但可能值得一试。

class User {
    // regular generated code should still be included
    boolean beforeInsertRunOnce = false
    boolean beforeUpdateRunOnce = false

    def beforeInsert() {
        if (! beforeInsertRunOnce) {
            beforeInsertRunOnce = true
            encodePassword()
        }
    }

    def afterInsert() {
        beforeInsertRunOnce = false
    }

    def beforeUpdate() {
        if (isDirty('password') && ! beforeUpdateRunOnce ) {
            beforeUpdateRunOnce = true
            encodePassword()
        }
    } 

    def afterUpdate() {
        beforeUpdateRunOnce = false
    }

    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM