Changing last login date in Grails with Spring Security

在使用grails spring security core plugin时,实现用户登录的时候更新上次登录时间

在Config.groovy添加如下代码:

import com.myapp.User
grails.plugins.springsecurity.useSecurityEventListener = true

grails.plugins.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx ->
    User.withTransaction {
        def user = User.findById(appCtx.springSecurityService.principal.id)
        if(!user.isAttached())
            user.attach()
        user.lastLoginDate = new Date()
        user.save(flush: true, failOnError: true)
    }
}


Reference: http://stackoverflow.com/questions/5868311/changing-last-login-date-in-grails-with-spring-security

你可能感兴趣的:(grails)