Grails Acegi:更新用户名

Grails Acegi:更新用户名,grails,spring-security,grails-plugin,Grails,Spring Security,Grails Plugin,我正在Grails应用程序中使用Acegi(又名Spring Security)插件。在SecurityConfig.groovy中,我添加了一行 userName = 'email' 以使电子邮件字段用作用户名。我发现如果我更改电子邮件字段并保存对象,例如 user.email = 'my_new_email@foo.com' user.save(failOnError: true) 保存完成,没有错误,但电子邮件字段实际上没有更新。我的猜测是Acegi插件禁止更改用户名字段,但如果有

我正在Grails应用程序中使用Acegi(又名Spring Security)插件。在
SecurityConfig.groovy
中,我添加了一行

userName = 'email'
以使电子邮件字段用作用户名。我发现如果我更改电子邮件字段并保存对象,例如

user.email = 'my_new_email@foo.com'
user.save(failOnError: true)  
保存完成,没有错误,但电子邮件字段实际上没有更新。我的猜测是Acegi插件禁止更改用户名字段,但如果有人能够确认,我将不胜感激

谢谢,
Don

缓存acegi使用的域对象。非常巧合的是,我这周和昨天都遇到了同样的问题

总之,您有两个选择:

通过向SecurityConfig.groovy中添加cacheUsers=false来关闭域对象的缓存

通过在SecurityContextHolder中替换域对象来刷新该域对象

private def refreshUserPrincipal(user) {
    GrantedAuthority[] auths = user.authorities.collect {
        new GrantedAuthorityImpl(it.authority)
    }
    def grailsUser = new GrailsUserImpl(
        user.username
            "",
            true,
            true,
            true,
            true,
            auths,
            user);
    def authToken = new UsernamePasswordAuthenticationToken(grailsUser, "", auths)
    SecurityContextHolder.context.authentication = authToken
}

(检查的源以查看所有这些真值的含义!)

缓存acegi使用的域对象。非常巧合的是,我这周和昨天都遇到了同样的问题

总之,您有两个选择:

通过向SecurityConfig.groovy中添加cacheUsers=false来关闭域对象的缓存

通过在SecurityContextHolder中替换域对象来刷新该域对象

private def refreshUserPrincipal(user) {
    GrantedAuthority[] auths = user.authorities.collect {
        new GrantedAuthorityImpl(it.authority)
    }
    def grailsUser = new GrailsUserImpl(
        user.username
            "",
            true,
            true,
            true,
            true,
            auths,
            user);
    def authToken = new UsernamePasswordAuthenticationToken(grailsUser, "", auths)
    SecurityContextHolder.context.authentication = authToken
}
(检查源代码以查看所有这些真值的含义!)

您只需执行以下操作:

String oldUsername = user.username
user.username='my@newusername.com'
user.save()
if(oldUsername != user.username) {
  SpringSecurityUtils.reauthenticate(user.username, null)
}
您可以简单地执行以下操作:

String oldUsername = user.username
user.username='my@newusername.com'
user.save()
if(oldUsername != user.username) {
  SpringSecurityUtils.reauthenticate(user.username, null)
}