【问题标题】:Is UserDetails or principal really stored in Session? [duplicate]UserDetails 或 principal 是否真的存储在 Session 中? [复制]
【发布时间】:2015-01-09 16:28:16
【问题描述】:

请看下面的链接..

Spring security access UserDetailsService from deeper layers

我读到的每一个地方都说 UserDetails 或主体在成功的身份验证时由 Spring 自动存储在会话中。但真的完成了吗?用哪个名字呢? 是否可以通过 session.getAttribute(...) 从会话中获取 UserDetails ?

所有其他方法都可以,但我对此感到困惑

【问题讨论】:

    标签: spring session authentication


    【解决方案1】:

    是的,每个执行线程都有一个关联的 SecurityContext 上下文。您可以像这样使用 SecurityContextHolder 检索身份验证/用户详细信息:

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    

    这是一个很好的教程:http://www.baeldung.com/get-user-in-spring-security

    【讨论】:

    • 这很好。但我的问题是,它是否保存在会话中?如果是,那么如何从 session.getAttribute 中获取它。我认为 seson.getAttribute 是在用户会话中获取值的唯一方法。
    • 你在写 Spring MVC 代码吗?如果是,示例代码显示了如何在 Controller 中获取有关用户的详细信息。如果您发布一些示例代码来显示您正在尝试做什么,这会有所帮助。
    • 我正在使用 SecurityContextHolder.getContext().getAuthentication() 但我只想知道是否可以使用 session.getAttribute() 函数找到用户详细信息。因为在我的应用程序中,所有其他会话属性都是使用 session.getAttribute() 访问的。但不是用户详细信息......所以唯一的问题是它真的由会话对象持有吗? ?
    • 我没有明确的答案,但看起来您无法从 HttpSession 对象中检索它。当他们说“Spring 在 session 中自动存储 UserDetails 或 principal”时,他们的意思是每个执行线程都有自己的 UserDetails 或 principal 对象。
    猜你喜欢
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2014-05-23
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多