【问题标题】:Grails Acegi: update usernameGrails Acegi:更新用户名
【发布时间】:2011-01-26 02:07:58
【问题描述】:

我在我的 Grails 应用程序中使用 Acegi (AKA Spring Security) 插件。在SecurityConfig.groovy我已经添加了这一行

userName = 'email'

以便将电子邮件字段用作用户名。我发现如果我更改电子邮件字段并保存对象,例如

user.email = 'my_new_email@foo.com'
user.save(failOnError: true)  

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

谢谢, 唐

【问题讨论】:

    标签: grails spring-security grails-plugin


    【解决方案1】:

    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 的来源,了解所有这些真实值的含义!)

    【讨论】:

      【解决方案2】:

      你可以简单地做:

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

      【讨论】:

        猜你喜欢
        • 2011-01-21
        • 2011-02-19
        • 2011-04-24
        • 1970-01-01
        • 2010-10-29
        • 1970-01-01
        • 2011-01-17
        • 1970-01-01
        • 2011-11-24
        相关资源
        最近更新 更多