[英]How to read value of SAML attribute received from the IdP?
我使用的是 Spring Security SAML 1.0.1,我想知道名称为“eduPersonAffiliation”的 SAML 属性的值。 我编写了一个实现org.springframework.security.saml.userdetails.SAMLUserDetailsService
接口和loadUserBySAML
方法的类,我这样做:
@Override
public Object loadUserBySAML(SAMLCredential credential) throws UsernameNotFoundException {
String eduPersonAffiliationAttributeName = "";
// We need to use the "name" of the attribute to retrieve the value (not the friendly name)
for (Attribute attribute : credential.getAttributes()) {
if ("eduPersonAffiliation".equals(attribute.getFriendlyName())) {
eduPersonAffiliationAttributeName = attribute.getName();
}
}
Person user = usersService.getUser(
credential.getAttribute(eduPersonAffiliationAttributeName).WHAT_TO_CALL_HERE?);
return loadUserByUser(user);
}
getUser
方法需要一个字符串,它应该是连接用户的登录名。 这个问题听起来很愚蠢,但我如何访问给定属性名称的属性值? 我看到一个org.opensaml.saml2.core.getAttributeValues
方法返回一个List<XMLObject>
。 如何使用它?
谢谢!
XmlObject需要一些解包才能使用:
private String getAttributeValue(XMLObject attributeValue)
{
return attributeValue == null ?
null :
attributeValue instanceof XSString ?
getStringAttributeValue((XSString) attributeValue) :
attributeValue instanceof XSAnyImpl ?
getAnyAttributeValue((XSAnyImpl) attributeValue) :
attributeValue.toString();
}
private String getStringAttributeValue(XSString attributeValue)
{
return attributeValue.getValue();
}
private String getAnyAttributeValue(XSAnyImpl attributeValue)
{
return attributeValue.getTextContent();
}
您可以遍历List<XmlObject>
直到找到您需要的属性,然后调用上面的getAttributeValue(XmlObject)方法。
根据这些XmlObject
的真正含义( Attribute 、 AttributeValue等),您可能需要此算法的某些部分来完全解压缩它们:
private final static String USERNAME_ATTRIBUTE_NAME = "urn:oid:0.9.2342.19200300.100.1.3"
private String getUsername(Assertion assertion)
{
for (AttributeStatement attributeStatement : assertion.getAttributeStatements())
{
for (Attribute attribute : attributeStatement.getAttributes())
{
if (USERNAME_ATTRIBUTE_NAME.equals(attribute.getName()))
{
List<XMLObject> attributeValues = attribute.getAttributeValues();
if (!attributeValues.isEmpty())
{
return getAttributeValue(attributeValues.get(0));
}
}
}
}
throw new IllegalArgumentException("no username attribute found");
}
在这种情况下,我使用标准 OID 作为电子邮件地址。 实际上,这必须是可配置的,因为各种 IdP 使用不同的命名策略。 这适用于 Shibboleth IdP 3。
@StefanRasmusson 的 OpenSAML 指南使我克服了获得 SAML 概念和能够实现我自己的 SP 之间的障碍。
Scott Cantor 在 shibboleth-users 邮件列表中对我的帮助也非常大,从配置差距到高级安全架构问题。 OpenSAML 社区(包括 Shibboleth)非常乐于助人和自以为是,我喜欢这一点。
访问属性值的另一种解决方案是通过 SAMLCredential.getAttributeAsString(String name)
credential.getAttributeAsString(attribute.getName())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.