【问题标题】:How to use cookie which is created using the rememberMe() feature of spring security in springboot? [duplicate]如何在spring boot中使用使用spring security的remember Me()功能创建的cookie? [复制]
【发布时间】:2019-06-10 22:11:26
【问题描述】:

我正在学习 Spring Boot 并创建一个简单的基于 Web 的项目,其中我使用 Spring 安全性的记住我功能创建了一个 cookie,但不知道如何使用该 cookie 来提取会话信息。

例如在页面上显示这个 - “hello {user}”,用户从一个页面传递到另一个页面。 {user} 值应与其他信息一起来自 cookie。

我们可以走这条路吗?如果是,如何从cookie中提取用户信息或者有没有其他方法可以实现特定功能?

我试图对此进行搜索,但找不到具体的解决方案。

这是部分代码。

.rememberMe()
        .alwaysRemember(true)
        .tokenValiditySeconds(30)
        .rememberMeCookieName("saurabh")
        .key("somesecret");

【问题讨论】:

  • 为了实现该解决方案,提出问题的人正在使用 spring-security XML。他在 XML 文件中添加所有标签,然后在 JSP 中打印相同的标签
  • 不,无论是问题还是任何答案都没有使用 XML,这都是关于 JSP 的。您在哪里看到 XML?
  • @dur 我已经添加了ans的图片以供参考。您可以看到提出问题的 Jeremiah 正在使用 XML 标记实现解决方案。
  • 这只是一个评论。它与问题或任何答案无关。

标签: java spring-boot cookies spring-security


【解决方案1】:

所有这些都已经在RememberMeAuthenticationFilter 中为您处理好了

该过滤器被插入,并执行以下操作

  1. 检查是否有任何可用信息
Authentication rememberMeAuth = 
    rememberMeServices.autoLogin(request, response);
  1. 如果有信息,它会给身份验证管理器一个机会来拒绝它或正确地对用户进行身份验证
rememberMeAuth = authenticationManager.authenticate(rememberMeAuth);
  1. 将信息存储在 SecurityContext 中(您现在已通过身份验证)
 SecurityContextHolder.getContext().setAuthentication(rememberMeAuth);
  1. 调用 No-Op 方法
 onSuccessfulAuthentication(request, response, rememberMeAuth);
  1. 触发事件
 eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(...));
  1. 最后,如果已配置,则触发成功处理程序
 successHandler.onAuthenticationSuccess(request, response,
                            rememberMeAuth);

如果您想拦截这一系列事件,请在您的AuthenticationManager 中进行。第 2 步,Spring 会将信息传递给您。

【讨论】:

    猜你喜欢
    • 2012-04-21
    • 2013-12-23
    • 1970-01-01
    • 2010-11-04
    • 2021-11-06
    • 1970-01-01
    • 2017-09-13
    • 2020-05-29
    • 2014-08-02
    相关资源
    最近更新 更多