2010-03-10 30 views
1

我在我的Grails应用程序中使用Acegi(AKA Spring Security)插件。在SecurityConfig.groovy我添加了这行Grails Acegi:更新用户名

userName = 'email' 

使得电子邮件字段被用来作为用户名。我发现如果我改变电子邮件字段并保存该对象,例如

user.email = '[email protected]' 
user.save(failOnError: true) 

保存完成时没有错误,但电子邮件字段未实际更新。我的猜测是Acegi插件禁止更改用户名字段,但如果有人能够确认,我将不胜感激。

谢谢, 唐

回答

3

通过的acegi使用的域对象被缓存。作为一个巨大的巧合,我本周也遇到了同样的问题,昨天还有wrote up the solution

总之你有两个选择:

加入cacheUsers = FALSE您SecurityConfig.groovy

关闭域对象的缓存通过在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 
} 
替换它刷新域对象

(查看GrailsUserImpl的来源,查看所有这些真值是什么意思!)

0

您可以简单地做:

String oldUsername = user.username 
user.username='[email protected]' 
user.save() 
if(oldUsername != user.username) { 
    SpringSecurityUtils.reauthenticate(user.username, null) 
} 
相关问题