spring security预认证用户登录

Posted

技术标签:

【中文标题】spring security预认证用户登录【英文标题】:spring security pre authenticated user login 【发布时间】:2016-07-14 03:49:36 【问题描述】:

我需要将用户手动登录到受 Spring Security 保护的应用程序中。 我的场景是

    用户登录到应用程序 1。 我的 spring 应用程序的链接将可用。 当用户单击链接时,应用程序 1 会将加密数据发布到我的 spring 应用程序中的不安全 url。 现在我的控制器将接收加密数据并调用 Web 服务 在应用程序 1 中,加密数据将在 Web 服务中传递,我会收到包含用户名的响应。 一旦我得到用户名。我不想重新认证。我只想设置用户主体/身份验证对象并加载仪表板。

第 1 步到第 4 步有效。

在我不安全的控制器中(我得到加密数据的地方) 我有以下几行,但这再次调用我的身份验证管理器,我不想在这种情况下再次进行身份验证。我只想创建会话并设置用户主体。

String username = application1.webservice(encoded data);
    Authentication authentication = new UsernamePasswordAuthenticationToken(username,password);
     SecurityContextHolder.getContext().setAuthentication(authentication);
     return "redirect:/app";

注意:我确实有基于表单的 LDAP 身份验证工作(如果用户直接登录到我的应用程序,将使用此功能) 我正在使用 spring security 3.2 / Java Config

【问题讨论】:

【参考方案1】:

SecurityContextPersistenceFilter 负责在 Web 请求的开头在SecurityContextHolder 中设置SecurityContext。并且在请求处理期间对SecurityContext的任何更改都将在Web请求结束时复制到HttpSession

因此,当您更新SecurityContext 中的Authentication 时,此过滤器将在身份验证请求结束时更新HttpSession

您能否确认在身份验证响应中返回的JSESSIONID 与在所有后续请求中传递的相同?

【讨论】:

您好,感谢您的回答。我添加了提供并解决的自定义身份验证。

以上是关于spring security预认证用户登录的主要内容,如果未能解决你的问题,请参考以下文章

使用 Spring Security 无需授权的预认证

Spring Security 预认证账户锁定检查

显示登录用户spring security

Spring Security 实现自定义登录和认证:使用自定义的用户进行认证

Spring Security认证简介

Spring Security自定义登录认证与数据持久化