【问题标题】:Spring Security SAML: Extract Attributes from a saml2p:Response as user attributesSpring Security SAML:从 saml2p:Response 中提取属性作为用户属性
【发布时间】:2020-09-24 08:31:25
【问题描述】:

我昨天一直在深入研究 Spring Security yaml,以使其与 Okta SAML 一起使用。登录有效,但响应 XML 包含显然无法自动提取到属性映射中的用户属性。响应包含这样的字段

<saml2:Attribute Name="user.lastName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified">
  <saml2:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">
    Surname
  </saml2:AttributeValue>
</saml2:Attribute>

一旦认证成功,我想把它们放在认证信息中。通过 github/oauth 登录时,OAuth2AuthenticatedPrincipal 类有一个属性映射,而 Saml2AuthenticatedPrincipal 只具有一个名称。

解决这个问题的正确方法是什么?

现在我正在考虑一个自定义AuthenticationSuccessHandler,它通过第二次解析提供的XML响应(通过.getDetails())填充一个自定义Saml2AuthenticatedPrincipalWithAttributes类包含所有属性(或将它们放入会话中) .

我有一种预感,这可能不是春季做事的方式,并且很想获得第二意见。当你在谷歌上搜索时,主要是找到 spring security saml 的例子,在它被合并到 spring security 之前,它处理的事情似乎有点不同,因为提到的类已经不存在了。

感谢大家的帮助!

【问题讨论】:

  • 有同样的问题......这家伙决定手动解析响应stackoverflow.com/q/58400571/10479742 有趣的是,这就是 OpenSamlAuthenticationProvider 解析和验证响应的方式,然后丢弃断言。请分享您选择的方法
  • 到目前为止,我已经完成了我在帖子中提到的操作,因为该帖子中提到的UserDetailsService 仅在旧版本中可用。这也会手动解析响应并丰富属性映射。然后我设置一个新的SecurityContext,其中包含带有属性映射的Principal
  • 好的。顺便说一句,我指的是上述问题中的更新,他还决定自己解析响应。我已经打开了一个 GitHub 问题,要求增强/澄清github.com/spring-projects/spring-security/issues/8661

标签: java spring spring-security


【解决方案1】:

在 Spring Security (5.4.0) 的下一个版本中,您应该能够执行类似 this 的操作:

@GetMapping("/")
public String index(Model model,
    @AuthenticationPrincipal Saml2AuthenticatedPrincipal principal) {
    String emailAddress = principal.getFirstAttribute("emailAddress");
    model.addAttribute("emailAddress", emailAddress);
    model.addAttribute("userAttributes", principal.getAttributes());
    return "index";
}

目前,我不知道比你更好的解决方法。

【讨论】:

  • 我想添加重定向并将标头中的一些值发送到重定向 url ...请提供任何建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 2021-03-24
  • 2013-05-03
相关资源
最近更新 更多