【发布时间】:2014-06-10 09:18:03
【问题描述】:
有没有案例:
def user = User.get(springSecurityService.principal.id)
结束
def user = springSecurityService.currentUser
我能想到的只是防止延迟初始化或确保您当前正在操作的数据不会过时?
【问题讨论】:
有没有案例:
def user = User.get(springSecurityService.principal.id)
结束
def user = springSecurityService.currentUser
我能想到的只是防止延迟初始化或确保您当前正在操作的数据不会过时?
【问题讨论】:
实际上,我认为这两者之间没有太大区别。我会倾向于使用
def user = springSecurityService.currentUser
因为它比另一种形式稍短,这是插件文档推荐的,并且可能在插件中对用户进行了一些额外的缓存(超出了 Hibernate 已经提供的缓存)。 p>
【讨论】:
getCurrentuser() 方法已被弃用。请参阅下面的 Joshua 的答案以获取上面链接的提交作为替代方案的getPrincipal()。
SpringSecurityService.getCurrentUser() 可用,将在 3.x 版本中弃用。
getCurrentUser 不再标记为弃用。 @deprecated 注释已从 source code 中删除。 getCurrentUser 在current documentation 中有描述。
嗯,两者之间存在细微差别。 documentation 指出了这一点。
currentUser 将始终返回当前登录用户的域实例。
另一方面,principal 检索当前登录用户的Principal。如果经过身份验证,主体将是 grails.plugin.springsecurity.userdetails.GrailsUser,除非您创建了自定义 UserDetailsService,在这种情况下,它将是您在那里使用的 UserDetails 的任何实现。
如果未通过身份验证且AnonymousAuthenticationFilter 处于活动状态(默认为true),则使用标准org.springframework.security.core.userdetails.User。
希望这有助于解决问题。
【讨论】:
User 域类可能不会存在,即如果您有 UserDetailsService 的自定义实现
我们刚刚遇到了代码使用 currentUser 并失败的情况,因为没有 User 域的用户记录。在我们的例子中,principal.username 有效,因为我们有一个自定义的 UserDetailsService,如果 User 表中不存在 GrailsUser,它会即时创建一个 GrailsUser。
所以区别很重要。
【讨论】: